elixir-maru / maru_swagger

Add swagger compliant documentation to your maru API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generated swagger assumes formData input type instead of JSON

nii236 opened this issue · comments

I have written a basic todo app:

My module code:

defmodule TodoElixir.API do
  use Maru.Router
  use MaruSwagger
  swagger at: "/swagger",
    swagger_inject: [
      host: "localhost:8080",
      basePath: "/",
      schemes:  [ "http" ],
      consumes: [ "application/json" ],
      produces: [ "application/json" ]
    ]

  version("v1")

  plug Plug.Parsers,
    pass: ["*/*"],
    json_decoder: Poison,
    parsers: [:urlencoded, :json, :multipart]

  alias TodoElixir.Worker, as: Store
  plug Plug.Logger

  namespace(:tasks) do

    desc "get all tasks" do
      get do
        conn
        |> json(Store.get)
      end
    end

    desc "inserts a new task" do
      params do
        requires :description, type: String
        requires :completed, type: Boolean, default: false
      end
      post do
        IO.puts("params")
        IO.inspect(params)
        conn
        |> json(Store.insert(params))
      end
    end

    desc "updates an existing task" do
      params do
        requires :id, type: String
        optional :description, type: String
        optional :completed, type: Boolean, default: false
        at_least_one_of [:completed, :description]
      end
      put do
        conn
        |> json(Store.update(params))
      end
    end

    desc "deletes a task" do
      params do
        requires :id, type: String
      end
      delete do
        conn
        |> json(Store.delete(params))
      end
    end

  end

  rescue_from :all, as: e do
    conn
    |> put_status(500)
    |> json(e)
  end
end

Generated Swagger JSON:

{
    "tags": [{
        "name": "Version: v1"
    }],
    "swagger": "2.0",
    "schemes": [
        "http"
    ],
    "produces": [
        "application/json"
    ],
    "paths": {
        "/tasks": {
            "put": {
                "tags": [
                    "Version: v1"
                ],
                "summary": "updates an existing task",
                "responses": {
                    "200": {
                        "description": "ok"
                    }
                },
                "parameters": [{
                        "type": "string",
                        "required": true,
                        "name": "id",
                        "in": "formData",
                        "description": ""
                    },
                    {
                        "type": "string",
                        "required": false,
                        "name": "description",
                        "in": "formData",
                        "description": ""
                    },
                    {
                        "type": "boolean",
                        "required": false,
                        "name": "completed",
                        "in": "formData",
                        "description": ""
                    }
                ],
                "description": ""
            },
            "post": {
                "tags": [
                    "Version: v1"
                ],
                "summary": "inserts a new task",
                "responses": {
                    "200": {
                        "description": "ok"
                    }
                },
                "parameters": [{
                        "type": "string",
                        "required": true,
                        "name": "description",
                        "in": "formData",
                        "description": ""
                    },
                    {
                        "type": "boolean",
                        "required": true,
                        "name": "completed",
                        "in": "formData",
                        "description": ""
                    }
                ],
                "description": ""
            },
            "get": {
                "tags": [
                    "Version: v1"
                ],
                "summary": "get all tasks",
                "responses": {
                    "200": {
                        "description": "ok"
                    }
                },
                "parameters": [],
                "description": ""
            },
            "delete": {
                "tags": [
                    "Version: v1"
                ],
                "summary": "deletes a task",
                "responses": {
                    "200": {
                        "description": "ok"
                    }
                },
                "parameters": [{
                    "type": "string",
                    "required": true,
                    "name": "id",
                    "in": "formData",
                    "description": ""
                }],
                "description": ""
            }
        }
    },
    "info": {
        "title": "Swagger API for TodoElixir.API"
    },
    "host": "localhost:8080",
    "consumes": [
        "application/json"
    ],
    "basePath": "/"
}

By default, formData is used for flat params and JSON is used for nested ones.
There's no option to custom it now, but I can add one for you 🙂
Do you need it?

Yes, I would appreciate it as all of my routes use JSON!

commented

Hello, that would be a great improvement :)

Just released maru_swagger v0.8.2 for elixir 1.4.0 with this feature 🙂

Awesome!