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

Casting of arrays into specified Schemas #615

Closed

Conversation

sofakingworld
Copy link

Hello dear maintainers!

I'm having errors when converting a structure with a list of substructures, like this:
image

That's why I made this fix with a test that reproduces a similar situation.

It would be great if you could accept a PR or give me some tips on how to use the library correctly (if I'm doing something wrong)

@@ -207,6 +207,10 @@ defmodule OpenApiSpex.Cast do
def cast(%__MODULE__{schema: %{type: _other}} = ctx),
do: error(ctx, {:invalid_schema_type})

def cast(%__MODULE__{value: struct, schema: schema_module} = ctx)
when is_struct(struct) and is_atom(schema_module),
do: cast(ctx.schema.schema(), ctx.value)
Copy link
Collaborator

@mbuhot mbuhot Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We avoid calling the schema() function like this during casting as it can have performance impacts.
The full API schema should be resolved before casting using OpenApiSpex.resolve_schema_modules

Once resolved, the schema_module will be replaced by a %Reference{} struct which will be handled by an earlier clause.

type: :object,
properties: %{
email: %Schema{type: :string},
permissions: %Schema{type: :array, items: PermissionSchema}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

items should be an explicit %Reference{}, or use OpenApiSpex.resolve_schema_modules to convert it to one automatically.

@mbuhot
Copy link
Collaborator

mbuhot commented Jul 7, 2024

Closing this one. If you're still experiencing errors after applying resolve_schema_modules then please re-open 👍

@mbuhot mbuhot closed this Jul 7, 2024
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 this pull request may close these issues.

2 participants