Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add header support in swagger_doc #17

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
43 changes: 37 additions & 6 deletions lib/maru_swagger/params_extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 ->
Expand Down
3 changes: 2 additions & 1 deletion lib/maru_swagger/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
20 changes: 18 additions & 2 deletions lib/maru_swagger/response_formatter.ex
Original file line number Diff line number Diff line change
@@ -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 || %{}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -23,15 +23,15 @@ defmodule MaruSwagger.Mixfile do
end

defp deps do
[ { :maru, "~> 0.11.3" },
[ { :maru, github: "Skipper1992/maru" },
{ :ex_doc, "~> 0.14", only: :docs },
]
end

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
16 changes: 9 additions & 7 deletions test/maru_swagger_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 10 additions & 8 deletions test/params_extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down