diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..bcdcacf Binary files /dev/null and b/.DS_Store differ diff --git a/lib/maru_swagger/params_extractor.ex b/lib/maru_swagger/params_extractor.ex index 754bdfb..743174e 100644 --- a/lib/maru_swagger/params_extractor.ex +++ b/lib/maru_swagger/params_extractor.ex @@ -3,11 +3,27 @@ defmodule MaruSwagger.ParamsExtractor do alias Maru.Struct.Dependent.Information, as: DI defmodule NonGetBodyParamsGenerator do - def generate(param_list, path) do + def generate(param_list, path, headers) do {path_param_list, body_param_list} = param_list |> MaruSwagger.ParamsExtractor.filter_information |> Enum.partition(&(&1.attr_name in path)) - [ format_body_params(body_param_list) | + + + if (is_nil(headers)) do + [ format_body_params(body_param_list) | format_path_params(path_param_list) ] + else + headers + |> Enum.map(fn(head) -> %{ + description: head[:description] || "", + name: head[:attr_name], + type: head[:type], + in: "header", + required: true + } end) + Enum.concat headers, [ format_body_params(body_param_list) | + format_path_params(path_param_list) + ] + end end defp default_body do @@ -78,8 +94,8 @@ defmodule MaruSwagger.ParamsExtractor do end defmodule NonGetFormDataParamsGenerator do - def generate(param_list, path) do - param_list + def generate(param_list, path, headers) do + param_list = param_list |> MaruSwagger.ParamsExtractor.filter_information |> Enum.map(fn param -> %{ name: param.param_key, @@ -89,6 +105,19 @@ defmodule MaruSwagger.ParamsExtractor do in: param.attr_name in path && "path" || "formData", } end) + if (is_nil(headers)) do + param_list + else + headers = headers + |> Enum.map(fn(head) -> %{ + description: head[:description] || "", + name: head[:attr_name], + type: head[:type], + in: "header", + required: true + } end) + Enum.concat headers, param_list + end end end @@ -110,7 +139,7 @@ defmodule MaruSwagger.ParamsExtractor do def extract_params(%Route{method: "GET"}, _config), do: [] def extract_params(%Route{parameters: []}, _config), do: [] - def extract_params(%Route{parameters: param_list, path: path}, config) do + def extract_params(%Route{parameters: param_list, path: path, desc: desc}, config) do param_list = filter_information(param_list) generator = if config.force_json do @@ -121,7 +150,8 @@ defmodule MaruSwagger.ParamsExtractor do :form_data -> NonGetFormDataParamsGenerator end end - generator.generate(param_list, path) + params = generator.generate(param_list, path, desc[:headers]) + params end defp judge_adapter([]), do: :form_data @@ -138,6 +168,7 @@ defmodule MaruSwagger.ParamsExtractor do end) |> flatten_dependents end + def flatten_dependents(param_list, force_optional \\ false) do Enum.reduce(param_list, [], fn %PI{}=i, acc when force_optional -> diff --git a/lib/maru_swagger/plug.ex b/lib/maru_swagger/plug.ex index 72ed59e..2837f41 100644 --- a/lib/maru_swagger/plug.ex +++ b/lib/maru_swagger/plug.ex @@ -2,7 +2,7 @@ defmodule MaruSwagger.Plug do use Maru.Middleware alias MaruSwagger.ConfigStruct alias Plug.Conn - + def init(opts) do ConfigStruct.from_opts(opts) end @@ -42,6 +42,7 @@ defmodule MaruSwagger.Plug do defp extract_route(ep, adapter, config) do params = MaruSwagger.ParamsExtractor.extract_params(ep, config) + path = adapter.path_for_params(ep.path, ep.version) method = case ep.method do {:_, [], nil} -> "MATCH" diff --git a/lib/maru_swagger/response_formatter.ex b/lib/maru_swagger/response_formatter.ex index 5452482..3b951da 100644 --- a/lib/maru_swagger/response_formatter.ex +++ b/lib/maru_swagger/response_formatter.ex @@ -1,6 +1,6 @@ defmodule MaruSwagger.ResponseFormatter do alias MaruSwagger.ConfigStruct - + def format(routes, tags, config=%ConfigStruct{}) do paths = routes |> List.foldr(%{}, fn (%{desc: desc, method: method, path: url_list, params: params, tag: tag}, result) -> desc = desc || %{} @@ -15,7 +15,23 @@ defmodule MaruSwagger.ResponseFormatter do tags: [tag], description: desc[:detail] || "", summary: desc[:summary] || "", - parameters: params, + parameters: if is_nil(desc[:headers]) do + params + else + case length(desc[:headers]) do + 0 -> params + _ -> + headers = Enum.map(desc[:headers], fn(head) -> %{ + description: head[:description], + name: head[:attr_name], + type: head[:type], + in: "header", + required: true + } end) + params = Enum.concat headers, params + params + end + end, responses: for r <- responses, into: %{} do {to_string(r.code), %{description: r.description}} end diff --git a/mix.exs b/mix.exs index e55a0a3..2cf5bca 100644 --- a/mix.exs +++ b/mix.exs @@ -9,7 +9,7 @@ defmodule MaruSwagger.Mixfile do start_permanent: Mix.env == :prod, deps: deps(), description: "Add swagger compliant documentation to your maru API", - source_url: "https://github.com/elixir-maru/maru_swagger", + source_url: "https://github.com/Skipper1992/maru_swagger", package: package(), docs: [ extras: ["README.md"], @@ -23,7 +23,7 @@ defmodule MaruSwagger.Mixfile do end defp deps do - [ { :maru, "~> 0.11.3" }, + [ { :maru, github: "Skipper1992/maru" }, { :ex_doc, "~> 0.14", only: :docs }, ] end @@ -31,7 +31,7 @@ defmodule MaruSwagger.Mixfile do defp package do %{ maintainers: ["Xiangrong Hao", "Roman Heinrich", "Cifer"], licenses: ["BSD 3-Clause"], - links: %{"Github" => "https://github.com/elixir-maru/maru_swagger"} + links: %{"Github" => "https://github.com/Skipper1992/maru_swagger"} } end end diff --git a/test/maru_swagger_plug_test.exs b/test/maru_swagger_plug_test.exs index e0a3620..264b047 100644 --- a/test/maru_swagger_plug_test.exs +++ b/test/maru_swagger_plug_test.exs @@ -10,13 +10,15 @@ defmodule MaruSwagger.PlugTest do use Maru.Router @test false - desc "hello world action" - params do - requires :id, type: Integer - end - get "/" do - _ = params - conn |> json(%{ hello: :world }) + desc "hello world action" do + + params do + requires :id, type: Integer + end + get "/" do + _ = params + conn |> json(%{ hello: :world }) + end end end diff --git a/test/params_extractor_test.exs b/test/params_extractor_test.exs index 4a148eb..5bf45c0 100644 --- a/test/params_extractor_test.exs +++ b/test/params_extractor_test.exs @@ -3,22 +3,24 @@ defmodule MaruSwagger.ParamsExtractorTest do doctest MaruSwagger.ParamsExtractor import TestHelper - describe "POST" do defmodule BasicPostApi do use Maru.Router - desc "res1 create" - params do - requires :name, type: :string, source: "user_name" - requires :email, type: :string - end - post "/res1" do - conn |> json(params) + desc "res1 create" do + + params do + requires :name, type: :string, source: "user_name" + requires :email, type: :string + end + post "/res1" do + conn |> json(params) + end end end test "works with basic POST params" do route_info = route_from_module(BasicPostApi, "POST", ["res1"]) + assert [ %{description: "", in: "formData", name: "user_name", required: true, type: "string"}, %{description: "", in: "formData", name: "email", required: true, type: "string"},