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

Treat parameters without explicit style field as with style: :form #642

Open
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion lib/open_api_spex/cast_parameters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ defmodule OpenApiSpex.CastParameters do
end)
end

defp pre_parse_parameter(parameter, %{explode: false, style: :form} = _context, _parsers) do
defp pre_parse_parameter(parameter, %{explode: false, style: form} = _context, _parsers)
# allow nil as `style: :form` should be the default behaviour according to OpenAPI
when form in [:form, nil] do
# e.g. sizes=S,L,M
# This does not take care of cases where the value may contain a comma itself
{:ok, String.split(parameter, ",")}
Expand Down
89 changes: 46 additions & 43 deletions test/cast_parameters_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,57 +55,60 @@ defmodule OpenApiSpex.CastParametersTest do
assert %{params: %{"Content-Type": "application/json"}} = conn
end

test "cast style: form, explode: false query parameters" do
size_schema = %Schema{
type: :string,
enum: [
"XS",
"S",
"M",
"L",
"XL"
]
}
for style <- [:form, nil] do
@tag style: style
test "cast style: #{inspect(style)}, explode: false query parameters", %{style: style} do
size_schema = %Schema{
type: :string,
enum: [
"XS",
"S",
"M",
"L",
"XL"
]
}

schema = %Schema{
title: "SizeParams",
type: :array,
uniqueItems: true,
minItems: 2,
items: size_schema
}
schema = %Schema{
title: "SizeParams",
type: :array,
uniqueItems: true,
minItems: 2,
items: size_schema
}

parameter = %Parameter{
in: :query,
name: :sizes,
required: true,
style: :form,
explode: false,
schema: schema
}
parameter = %Parameter{
in: :query,
name: :sizes,
required: true,
style: style,
explode: false,
schema: schema
}

operation = %Operation{
parameters: [parameter],
responses: %{
200 => %Schema{type: :object}
operation = %Operation{
parameters: [parameter],
responses: %{
200 => %Schema{type: :object}
}
}
}

spec =
spec_with_components(%Components{
schemas: %{"SizeParams" => schema}
})
spec =
spec_with_components(%Components{
schemas: %{"SizeParams" => schema}
})

sizes_param = "S,M,L"
sizes_param = "S,M,L"

conn =
:get
|> Plug.Test.conn("/api/t-shirts?sizes=#{sizes_param}")
|> Plug.Conn.put_req_header("content-type", "application/json")
|> Plug.Conn.fetch_query_params()
conn =
:get
|> Plug.Test.conn("/api/t-shirts?sizes=#{sizes_param}")
|> Plug.Conn.put_req_header("content-type", "application/json")
|> Plug.Conn.fetch_query_params()

assert {:ok, conn} = CastParameters.cast(conn, operation, spec)
assert %{params: %{sizes: ["S", "M", "L"]}} = conn
assert {:ok, conn} = CastParameters.cast(conn, operation, spec)
assert %{params: %{sizes: ["S", "M", "L"]}} = conn
end
end

test "cast json query params with default parser" do
Expand Down