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
Closed
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
4 changes: 4 additions & 0 deletions lib/open_api_spex/cast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.


def cast(%{} = ctx), do: cast(struct(__MODULE__, ctx))
def cast(ctx) when is_list(ctx), do: cast(struct(__MODULE__, ctx))

Expand Down
63 changes: 63 additions & 0 deletions test/cast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,69 @@ defmodule OpenApiSpec.CastTest do

def cast(ctx), do: Cast.cast(ctx)

describe "SubScheme with array" do
defmodule User do
defstruct [:email, :permissions]
end

defmodule Permission do
defstruct [:name, :some_other_field]
end

defmodule UsersSchemas do
alias OpenApiSpex.Schema

defmodule PermissionSchema do
@moduledoc false
require OpenApiSpex

OpenApiSpex.schema(%{
title: "Permission",
type: :object,
properties: %{
name: %Schema{type: :string}
},
required: [:name, :some_other_field]
})
end

defmodule UserSchema do
@moduledoc false
require OpenApiSpex

OpenApiSpex.schema(%{
title: "User",
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.

},
required: [:email, :permissions]
})
end
end

test "Correct casting of sub arrays" do
permissions = [
%Permission{name: "Work", some_other_field: 1},
%Permission{name: "Rest", some_other_field: 2},
%Permission{name: "Smile", some_other_field: 3}
]

user = %User{email: "happy_admin@email.com", permissions: permissions}

assert {:ok,
%UsersSchemas.UserSchema{
email: "happy_admin@email.com",
permissions: [
%UsersSchemas.PermissionSchema{name: "Work"},
%UsersSchemas.PermissionSchema{name: "Rest"},
%UsersSchemas.PermissionSchema{name: "Smile"}
]
}} == OpenApiSpex.cast_value(user, UsersSchemas.UserSchema.schema())
end
end

describe "cast/1" do
test "unknown schema type" do
assert {:error, [error]} = cast(value: "string", schema: %Schema{type: :nope})
Expand Down
Loading