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

Fix anyOf cast crash when Cast.cast return a struct #469

Merged
merged 1 commit into from
Jul 5, 2022
Merged
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
3 changes: 3 additions & 0 deletions lib/open_api_spex/cast/any_of.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ defmodule OpenApiSpex.Cast.AnyOf do
new_ctx = put_in(ctx.schema.anyOf, remaining)

case Cast.cast(%{ctx | schema: relaxed_schema}) do
{:ok, value} when is_struct(value) ->
{:ok, value}

{:ok, value} when is_map(value) ->
acc =
value
Expand Down
14 changes: 14 additions & 0 deletions test/cast/any_of_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,19 @@ defmodule OpenApiSpex.CastAnyOfTest do

assert Error.message(error) == "Failed to cast value using any of: [] (no schemas provided)"
end

test "cast date and date-time" do
schema = %Schema{
anyOf: [
%Schema{type: :string, format: :"date-time"},
%Schema{type: :string, format: :date}
]
}

assert {:ok, ~D[2021-01-01]} = cast(value: "2021-01-01", schema: schema)

assert {:ok, ~U[2022-04-05 16:28:53.168653Z]} =
cast(value: "2022-04-05T16:28:53.168653Z", schema: schema)
end
end
end