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

Accept List[String] as get parameter type #35

Closed
wants to merge 1 commit into from
Closed
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
37 changes: 33 additions & 4 deletions lib/maru_swagger/params_extractor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ defmodule MaruSwagger.ParamsExtractor do
defmodule NonGetBodyParamsGenerator do
def generate(param_list, path) do
{path_param_list, body_param_list} =
param_list |> MaruSwagger.ParamsExtractor.filter_information()
param_list
|> MaruSwagger.ParamsExtractor.filter_information()
|> Enum.split_with(&(&1.attr_name in path))

[
Expand Down Expand Up @@ -93,10 +94,24 @@ defmodule MaruSwagger.ParamsExtractor do
end
end

alias Maru.Router
defmodule GetParamsGenerator do
def generate(param_list, path) do
for %PI{} = param <- param_list do
format_param(param, path)
end
end

def extract_params(%Router{method: :get, path: path, parameters: parameters}, _config) do
for %PI{} = param <- parameters do
defp format_param(%{type: {:list, _}} = param, _) do
%{
name: param.param_key,
description: param.desc || "",
required: param.required,
schema: do_format_param(param.type, param),
in: "query"
}
end

defp format_param(param, path) do
%{
name: param.param_key,
description: param.desc || "",
Expand All @@ -105,6 +120,20 @@ defmodule MaruSwagger.ParamsExtractor do
in: (param.attr_name in path && "path") || "query"
}
end

defp do_format_param({:list, type}, param) do
%{type: "array", items: do_format_param(type, param)}
end

defp do_format_param(type, param) do
%{description: param.desc || "", type: type, required: param.required}
end
end

alias Maru.Router

def extract_params(%Router{method: :get, path: path, parameters: parameters}, _config) do
GetParamsGenerator.generate(parameters, path)
end

def extract_params(%Router{method: :get}, _config), do: []
Expand Down
37 changes: 37 additions & 0 deletions test/params_extractor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,43 @@ defmodule MaruSwagger.ParamsExtractorTest do
end
end

describe "get one-line nested list test" do
defmodule GetOneLineNestedList do
use Maru.Router

desc "one-line nested list test"

params do
requires :foo, type: List[String]
end

get "/path" do
conn |> json(params)
end
end

test "get one-line nested list test" do
route_info = route_from_module(GetOneLineNestedList, :get, ["path"])

assert [
%{
description: "",
in: "query",
name: "foo",
required: true,
schema: %{
type: "array",
items: %{
description: "",
required: true,
type: "string"
}
}
}
] = extract_params(route_info)
end
end

describe "validation in parameters test" do
defmodule ValidationInParametes do
use Maru.Router
Expand Down