Skip to content

Commit 8683985

Browse files
committed
Remove matching params before constructing exploded params
1 parent f14596b commit 8683985

File tree

6 files changed

+24
-34
lines changed

6 files changed

+24
-34
lines changed

examples/plug_app/.tool-versions

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
elixir 1.11.4-otp-22
2+
erlang 22.3.4.26

examples/plug_app/lib/plug_app/user_handler.ex

+1-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ defmodule PlugApp.UserHandler do
103103
end
104104
end
105105

106-
# def show(conn = %Plug.Conn{assigns: %{user: user}}, _opts) do
107-
def show(conn, _opts) do
108-
IO.inspect(conn.params)
109-
106+
def show(conn = %Plug.Conn{assigns: %{user: user}}, _opts) do
110107
user = Accounts.get_user!(conn.params.id)
111108

112109
conn

examples/plug_app/mix.exs

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ defmodule PlugApp.Mixfile do
2929
{:plug, "~> 1.0"},
3030
{:ecto, "~> 2.2"},
3131
{:sqlite_ecto2, "~> 2.4"},
32-
{:jason, "~> 1.0"},
33-
{:cors_plug, "~> 3.0"}
32+
{:jason, "~> 1.0"}
3433
# {:dep_from_hexpm, "~> 0.3.0"},
3534
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
3635
]

examples/plug_app/mix.lock

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
%{
22
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
3-
"cors_plug": {:hex, :cors_plug, "3.0.3", "7c3ac52b39624bc616db2e937c282f3f623f25f8d550068b6710e58d04a0e330", [:mix], [{:plug, "~> 1.13", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "3f2d759e8c272ed3835fab2ef11b46bddab8c1ab9528167bd463b6452edf830d"},
43
"cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"},
54
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
65
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},

lib/open_api_spex/cast_parameters.ex

+20-22
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ defmodule OpenApiSpex.CastParameters do
6969
properties: parameters |> Map.new(fn p -> {p.name, Parameter.schema(p)} end),
7070
required: parameters |> Enum.filter(& &1.required) |> Enum.map(& &1.name)
7171
}
72-
# |> maybe_combine_oneOfs(parameters, components)
7372
|> maybe_add_additional_properties(components),
7473
parameters_contexts(parameters)
7574
}
@@ -141,7 +140,23 @@ defmodule OpenApiSpex.CastParameters do
141140
# fields in parameters and combine the parameters in a single struct
142141
# so that the casting can do further work
143142
defp maybe_combine_params(%{} = parameters, %{} = schema, %{} = parameters_contexts) do
144-
Enum.reduce(parameters_contexts, parameters, fn
143+
# first filter out from parameters fields that match non exploding properties.
144+
# we do this because we want to keep the original parameters struct intact
145+
# and not remove fields that are not part of the exploding property
146+
147+
non_exploding_matches = Enum.reduce(parameters, Map.new(), fn {key, value}, acc ->
148+
case Map.get(parameters_contexts, key, %{}) do
149+
%{explode: false} ->
150+
Map.put(acc, key, value)
151+
152+
_ ->
153+
acc
154+
end
155+
end)
156+
157+
possible_exploding_matches = Enum.reject(parameters, &Enum.member?(non_exploding_matches, &1)) |> Map.new()
158+
159+
combined_params = Enum.reduce(parameters_contexts, possible_exploding_matches, fn
145160
{key, %{explode: true}}, parameters ->
146161
# we have exploding property, we need to search for it's possible fields
147162
# and add them under the key into the parameters struct.
@@ -153,18 +168,15 @@ defmodule OpenApiSpex.CastParameters do
153168
Schema.possible_properties(schema_of_exploding_property)
154169

155170
{struct_params, found_keys} =
156-
Enum.reduce(fields, {Map.new(), []}, fn {field_key, _}, {struct_params, found_keys} ->
171+
Enum.reduce(fields, {Map.new(), []}, fn {field_key, _default}, {struct_params, found_keys} ->
157172
param_field_key = field_key |> Atom.to_string()
158173
val = Map.get(parameters, param_field_key)
159174

160-
{new_params, new_found_keys} =
161175
unless is_nil(val) do
162176
{Map.put(struct_params, param_field_key, val), [param_field_key | found_keys]}
163177
else
164178
{struct_params, found_keys}
165179
end
166-
167-
{new_params, new_found_keys}
168180
end)
169181

170182
parameters
@@ -174,6 +186,8 @@ defmodule OpenApiSpex.CastParameters do
174186
_, parameters ->
175187
parameters
176188
end)
189+
190+
Map.merge(non_exploding_matches, combined_params)
177191
end
178192

179193
defp pre_parse_parameters(%{} = parameters, %{} = parameters_context, parsers) do
@@ -250,20 +264,4 @@ defmodule OpenApiSpex.CastParameters do
250264
_ -> schema
251265
end
252266
end
253-
254-
defp maybe_combine_oneOfs(schema, parameters, components) do
255-
# check if any params have explode,
256-
# if so add the properties of it's schema to the top level
257-
# and remove the key for that
258-
%{}
259-
end
260-
261-
defp create_one_of_schemas(parameters) do
262-
if Enum.any?(parameters, fn p ->
263-
p.explode == true and is_list(Parameter.schema(p).oneOf)
264-
end) do
265-
# in this case we need to create multiple schemas. Each of the schemas
266-
# has to have properties defined in other parameters + add required properties
267-
end
268-
end
269267
end

test/cast_parameters_test.exs

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule OpenApiSpex.CastParametersTest do
22
use ExUnit.Case
33

4-
require IEx
5-
64
alias OpenApiSpex.{
75
CastParameters,
86
Components,
@@ -205,9 +203,6 @@ defmodule OpenApiSpex.CastParametersTest do
205203
|> Plug.Conn.put_req_header("content-type", "application/json")
206204
|> Plug.Conn.fetch_query_params()
207205

208-
# require IEx
209-
# IEx.pry()
210-
211206
assert {:ok, _} = CastParameters.cast(conn, operation, spec)
212207
end
213208
end

0 commit comments

Comments
 (0)