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

OpenApiSpex.Schema.example/1 handles allOf/anyOf/oneOf too simplistically #640

Open
xxdavid opened this issue Oct 31, 2024 · 0 comments
Open

Comments

@xxdavid
Copy link

xxdavid commented Oct 31, 2024

Hi, first of all, thanks for this great library!

I've found issues with OpenApiSpex.Schema.example/1 when allOf/anyOf/oneOf fields are involved. Produced examples do not even pass validation/casting.

Looking at source code the source code of example/1, if it inputs a schema with one of these fields, it handles just one of them and ignores all other fields.

Here are two examples.

alias OpenApiSpex.Schema
schema =
  %Schema{
    type: :object,
    allOf: [
      %Schema{
        type: :object,
        properties: %{a: %Schema{type: :integer}},
        required: [:a]
      },
      %Schema{
        type: :object,
        properties: %{b: %Schema{type: :integer}},
        required: [:b]
      }
    ],
    oneOf: [
      %Schema{
        type: :object,
        properties: %{c: %Schema{type: :integer}},
        required: [:c]
      },
      %Schema{
        type: :object,
        properties: %{d: %Schema{type: :integer}},
        required: [:d]
      }
    ],
    properties: %{e: %Schema{type: :integer}},
    required: [:e]
  }

If I understand it correctly, this schema should describe an object that has both a and b and either c or d. But example/1 return just a map with c.

iex> example = OpenApiSpex.Schema.example(schema)
%{c: 0}
iex> Schema.cast(schema, example, %{})
{:error,
 [
   %OpenApiSpex.Cast.Error{
     reason: :all_of,
     value: %{c: 0},
     format: nil,
     type: nil,
     name: nil,
     path: [],
     length: 0,
     meta: %{invalid_schema: "object"}
   },
   %OpenApiSpex.Cast.Error{
     reason: :missing_field,
     value: %{c: 0},
     format: nil,
     type: nil,
     name: :a,
     path: [:a],
     length: 0,
     meta: %{}
   }
 ]}

Similar goes for the second example.

schema =
  %Schema{
    type: :object,
    allOf: [
      %Schema{
        type: :object,
        properties: %{a: %Schema{type: :integer}},
        required: [:a]
      },
      %Schema{
        type: :object,
        properties: %{b: %Schema{type: :integer}},
        required: [:b]
      }
    ],
    properties: %{c: %Schema{type: :integer}},
    required: [:c]
  }

This schema should describe an object with all three keys a, b, and c. However, example/1 return just a map with a and b.

iex> example = OpenApiSpex.Schema.example(schema)
%{a: 0, b: 0}
iex> Schema.cast(schema, example, %{})
{:error,
 [
   %OpenApiSpex.Cast.Error{
     reason: :missing_field,
     value: %{a: 0, b: 0},
     format: nil,
     type: nil,
     name: :c,
     path: [:c],
     length: 0,
     meta: %{}
   }
 ]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant