Skip to content

OpenApiSpex.cast bug with all_of schema and date times #663

@caioaao

Description

@caioaao

If you try to cast a datetime inside an allOf schema, it breaks. Here's an example script and the resulting output:

datetime_schema =
  %OpenApiSpex.Schema{
    type: :string,
    format: :"date-time"
  }

# Making sure the issue is not in the date time schema
IO.inspect(OpenApiSpex.cast_value(~U[2024-01-02T22:23:00Z], datetime_schema))

parent_schema =
  %OpenApiSpex.Schema{
    allOf: [datetime_schema]
  }

# This should be considered valid, but breaks
OpenApiSpex.cast_value(~U[2024-01-02T22:23:00Z], parent_schema)

The output:

$ mix run cast_test.exs
{:ok, ~U[2024-01-02 22:23:00Z]}
** (Protocol.UndefinedError) protocol Enumerable not implemented for type DateTime (a struct). This protocol is implemented for the following type(s): CircularBuffer, DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, Jason.OrderedObject, List, Map, MapSet, Phoenix.LiveView.LiveStream, Postgrex.Stream, Range, Req.Response.Async, Stream

Got value:

    ~U[2024-01-02 22:23:00Z]

    (elixir 1.18.1) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir 1.18.1) lib/enum.ex:166: Enumerable.reduce/3
    (elixir 1.18.1) lib/enum.ex:4511: Enum.reduce/3
    (open_api_spex 3.21.2) lib/open_api_spex/cast/all_of.ex:15: OpenApiSpex.Cast.AllOf.cast_all_of/2
    cast_test.exs:14: (file)
    (elixir 1.18.1) lib/code.ex:1525: Code.require_file/2
    (mix 1.18.1) lib/mix/tasks/run.ex:146: Mix.Tasks.Run.run/5
    (mix 1.18.1) lib/mix/tasks/run.ex:85: Mix.Tasks.Run.run/1
    (mix 1.18.1) lib/mix/task.ex:495: anonymous fn/3 in Mix.Task.run_task/5
    (mix 1.18.1) lib/mix/cli.ex:107: Mix.CLI.run_task/2

Apparently this fails because of a check in all_of here, but I'm not sure how to fix it, as this should be handled like the next clause but I'm not sure if there's an easy way of checking that it's a primitive in the spec sense

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions