Skip to content

Commit

Permalink
Fix 'AllOf cast returns a map, but I expected a struct' (open-api-spe…
Browse files Browse the repository at this point in the history
…x#592)

* Add failing test

* Cast result of AllOf cast into a struct

* Shorter module name
  • Loading branch information
angelikatyborska authored and Kurt committed Apr 18, 2024
1 parent f41f895 commit 45e8436
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/open_api_spex/cast/all_of.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ defmodule OpenApiSpex.Cast.AllOf do
cast_all_of(%{ctx | schema: %{schema | allOf: [nested_schema | remaining]}}, result)
end

defp cast_all_of(%{schema: %{allOf: []}, errors: []} = ctx, acc) do
defp cast_all_of(%{schema: %{allOf: [], "x-struct": module}, errors: []} = ctx, acc)
when not is_nil(module) do
with :ok <- Utils.check_required_fields(ctx, acc) do
{:ok, acc}
{:ok, struct(module, acc)}
end
end

defp cast_all_of(%{schema: %{allOf: [], errors: [], "x-struct": module}} = ctx, acc)
when not is_nil(module) do
defp cast_all_of(%{schema: %{allOf: []}, errors: []} = ctx, acc) do
with :ok <- Utils.check_required_fields(ctx, acc) do
{:ok, acc}
end
Expand Down
3 changes: 2 additions & 1 deletion test/cast/all_of_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ defmodule OpenApiSpex.CastAllOfTest do

test "with schema having x-type" do
value = %{fur: true, meow: true}
assert {:ok, _} = cast(value: value, schema: CatSchema.schema())

assert {:ok, %CatSchema{fur: true, meow: true}} = cast(value: value, schema: CatSchema.schema())
end
end

0 comments on commit 45e8436

Please sign in to comment.