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

Handle the case where Schema.type is an array #659

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
112 changes: 46 additions & 66 deletions lib/open_api_spex/open_api/decode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@
end
end

# In some cases, e.g. Schema type we must convert values that are strings to atoms,
# for example Schema.type should be one of:
#
# :string | :number | :integer | :boolean | :array | :object
# In some cases, e.g. Schema type we must convert values that are strings to atoms.
#
# This function, ensures that if the map has the key — it'll convert the corresponding value
# to an atom.
Expand All @@ -127,11 +124,25 @@
update_map_if_key_present(map, key, &Enum.map(&1, map_fn))
end

# In some cases, e.g. Schema type we must convert values that are either strings or list of strings to a atoms or list of atoms,

Check warning on line 127 in lib/open_api_spex/open_api/decode.ex

View workflow job for this annotation

GitHub Actions / Lint (OTP 26 / Elixir 1.16)

Line is too long (max is 120, was 130).
#
# This function, ensures that if the map has the key — it'll convert the corresponding value
# to an atom or list of atoms.
defp convert_value_to_atom_or_list_of_atoms_if_present(map, key) do
update_fn = fn
nil -> nil
l when is_list(l) -> Enum.map(l, &String.to_atom/1)
s -> String.to_atom(s)
end

update_map_if_key_present(map, key, update_fn)
end

# The Schema.type and Schema.required keys require some special treatment since their
# values should be converted to atoms
defp prepare_schema(map) do
map
|> convert_value_to_atom_if_present("type")
|> convert_value_to_atom_or_list_of_atoms_if_present("type")
|> convert_value_to_atom_if_present("format")
|> convert_value_to_atom_if_present("x-struct")
|> convert_value_to_list_of_atoms_if_present("required")
Expand Down Expand Up @@ -206,69 +217,38 @@

defp to_struct(%{"$ref" => _} = map, Schema), do: struct_from_map(Reference, map)

defp to_struct(%{"type" => type} = map, Schema)
when type in ~w(number integer boolean string) do
map
|> prepare_schema()
|> (&struct_from_map(Schema, &1)).()
|> prop_to_struct(:xml, Xml)
|> add_extensions(map)
end

defp to_struct(%{"type" => "array"} = map, Schema) do
map
|> prepare_schema()
|> (&struct_from_map(Schema, &1)).()
|> prop_to_struct(:items, Schema)
|> prop_to_struct(:xml, Xml)
|> add_extensions(map)
end

defp to_struct(%{"anyOf" => _valid_schemas} = map, Schema) do
Schema
|> struct_from_map(prepare_schema(map))
|> prop_to_struct(:anyOf, Schemas)
|> prop_to_struct(:discriminator, Discriminator)
|> add_extensions(map)
end

defp to_struct(%{"oneOf" => _valid_schemas} = map, Schema) do
Schema
|> struct_from_map(prepare_schema(map))
|> prop_to_struct(:oneOf, Schemas)
|> prop_to_struct(:discriminator, Discriminator)
|> add_extensions(map)
end

defp to_struct(%{"allOf" => _valid_schemas} = map, Schema) do
Schema
|> struct_from_map(prepare_schema(map))
|> prop_to_struct(:allOf, Schemas)
|> prop_to_struct(:discriminator, Discriminator)
|> add_extensions(map)
end

defp to_struct(%{"type" => "object"} = map, Schema) do
map
|> Map.update("properties", %{}, fn v ->
v
|> Map.new(fn {k, v} ->
{String.to_atom(k), v}
defp to_struct(map, Schema) do
a =
map
|> update_map_if_key_present("properties", fn v ->
v
|> Map.new(fn {k, v} ->
{String.to_atom(k), v}
end)
end)
end)
|> prepare_schema()
|> (&struct_from_map(Schema, &1)).()
|> prop_to_struct(:properties, Schemas)
|> manage_additional_properties()
|> prop_to_struct(:externalDocs, ExternalDocumentation)
|> add_extensions(map)
end
|> prepare_schema()
|> (&struct_from_map(Schema, &1)).()
|> prop_to_struct(:items, Schema)
|> prop_to_struct(:xml, Xml)
|> prop_to_struct(:anyOf, Schemas)
|> prop_to_struct(:oneOf, Schemas)
|> prop_to_struct(:allOf, Schemas)
|> prop_to_struct(:discriminator, Discriminator)
|> prop_to_struct(:properties, Schemas)
|> manage_additional_properties()
|> prop_to_struct(:externalDocs, ExternalDocumentation)
|> prop_to_struct(:not, Schema)
|> add_extensions(map)
|> dbg

Check warning on line 242 in lib/open_api_spex/open_api/decode.ex

View workflow job for this annotation

GitHub Actions / Lint (OTP 26 / Elixir 1.16)

There should be no calls to `dbg/1`.

if a.properties == %{} do
dbg(a.properties)

Check warning on line 245 in lib/open_api_spex/open_api/decode.ex

View workflow job for this annotation

GitHub Actions / Lint (OTP 26 / Elixir 1.16)

There should be no calls to `dbg/1`.
dbg(map["properties"])

Check warning on line 246 in lib/open_api_spex/open_api/decode.ex

View workflow job for this annotation

GitHub Actions / Lint (OTP 26 / Elixir 1.16)

There should be no calls to `dbg/1`.
raise map
# dbg(map)
end

defp to_struct(%{"not" => _valid_schemas} = map, Schema) do
Schema
|> struct_from_map(map)
|> prop_to_struct(:not, Schema)
|> add_extensions(map)
a
end

defp to_struct(map, Schemas) when is_map(map), do: embedded_ref_or_struct(map, Schema)
Expand Down
2 changes: 1 addition & 1 deletion test/support/encoded_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@
"required": [
"data"
],
"type": "object"
"type": ["object", "null"]
}
}
}
Expand Down
Loading