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

Problem with nested oneOf in allOf #594

Closed
GregorGrasselli opened this issue Feb 7, 2024 · 2 comments
Closed

Problem with nested oneOf in allOf #594

GregorGrasselli opened this issue Feb 7, 2024 · 2 comments

Comments

@GregorGrasselli
Copy link

There seems to be a bug when creating a struct from a schema that has a nested oneOf inside an allOf

We take a schema that seems to work correctly:

schema = %OpenApiSpex.Schema{
...(7)>   allOf: [
...(7)>     %OpenApiSpex.Schema{
...(7)>       type: :object,
...(7)>       properties: %{p: %OpenApiSpex.Schema{type: :string}},
...(7)>       required: [:p]
...(7)>     },
...(7)>     %OpenApiSpex.Schema{
...(7)>       oneOf: [
...(7)>         %OpenApiSpex.Schema{
...(7)>           type: :object,
...(7)>           properties: %{a: %OpenApiSpex.Schema{type: :string}},
...(7)>           required: [:a]
...(7)>         },
...(7)>         %OpenApiSpex.Schema{
...(7)>           type: :object,
...(7)>           properties: %{b: %OpenApiSpex.Schema{type: :string}},
...(7)>           required: [:b]
...(7)>         }
...(7)>       ]
...(7)>     }
...(7)>   ]
...(7)> }

iex(10)> OpenApiSpex.Cast.cast(schema, %{p: "str", b: "qwe"})
{:ok, %{p: "str", b: "qwe"}}

Using it to define a struct produces a struct without the keys :a and :b:

ex(15)> defmodule Test do
...(15)> OpenApiSpex.schema %{
...(15)>   allOf: [
...(15)>     %OpenApiSpex.Schema{
...(15)>       type: :object,
...(15)>       properties: %{p: %OpenApiSpex.Schema{type: :string}},
...(15)>       required: [:p]
...(15)>     },
...(15)>     %OpenApiSpex.Schema{
...(15)>       oneOf: [
...(15)>         %OpenApiSpex.Schema{
...(15)>           type: :object,
...(15)>           properties: %{a: %OpenApiSpex.Schema{type: :string}},
...(15)>           required: [:a]
...(15)>         },
...(15)>         %OpenApiSpex.Schema{
...(15)>           type: :object,
...(15)>           properties: %{b: %OpenApiSpex.Schema{type: :string}},
...(15)>           required: [:b]
...(15)>         }
...(15)>       ]
...(15)>     }
...(15)>   ]
...(15)> }
...(15)> end
iex(16)> %Test{}
%Test{p: nil}
iex(17)> %Test{p: "asdf", a: "burek"}
** (KeyError) key :a not found
    expanding struct: Test.__struct__/1
    iex:17: (file)
@GregorGrasselli
Copy link
Author

I tried what happens when I add this to an endpoint and send a valid request. Validation succeeds, but the body_params that are created are missing the keys specified under the oneOf part.

@GregorGrasselli
Copy link
Author

Turns out this can be fixed by just not using structs in any of the schemas that have oneOf in their top level. I think it might make sense to change the default value for struct? in such cases.

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

Successfully merging a pull request may close this issue.

1 participant