Skip to content

Commit

Permalink
Further improve error messages of #541
Browse files Browse the repository at this point in the history
  • Loading branch information
zorbash committed May 30, 2023
1 parent 4805411 commit a332a06
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/open_api_spex/schema_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,11 @@ defmodule OpenApiSpex.SchemaResolver do

defp resolve_schema_modules_from_schema(ref = %Reference{}, schemas), do: {ref, schemas}

defp resolve_schema_modules_from_schema(_, _) do
defp resolve_schema_modules_from_schema(schema, _schemas) do
error_message = """
Cannot resolve schema, must be one of:
Cannot resolve schema #{inspect(schema)}.
Must be one of:
- schema module, or schema struct
- list of schema modules, or schema structs
Expand Down Expand Up @@ -286,6 +288,7 @@ defmodule OpenApiSpex.SchemaResolver do
Enum.map_reduce(mapping, schemas, fn
{key, module}, schemas when is_atom(module) ->
{%Reference{"$ref": path}, schemas} = resolve_schema_modules_from_schema(module, schemas)

{{key, path}, schemas}

{key, path}, schemas ->
Expand All @@ -295,5 +298,6 @@ defmodule OpenApiSpex.SchemaResolver do
{%{discriminator | mapping: Map.new(mapping)}, schemas}
end

defp resolve_schema_modules_from_discriminator(disciminator, schemas), do: {disciminator, schemas}
defp resolve_schema_modules_from_discriminator(disciminator, schemas),
do: {disciminator, schemas}
end
12 changes: 9 additions & 3 deletions test/schema_resolver_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,14 @@ defmodule OpenApiSpex.SchemaResolverTest do
resolved.paths["/api/users"].post.requestBody.content["application/json"].schema

assert "#/components/schemas/TrainingAppointment" =
resolved.components.schemas["PetAppointmentRequest"].discriminator.mapping["training"]
resolved.components.schemas["PetAppointmentRequest"].discriminator.mapping[
"training"
]

assert "#/components/schemas/GroomingAppointment" =
resolved.components.schemas["PetAppointmentRequest"].discriminator.mapping["grooming"]
resolved.components.schemas["PetAppointmentRequest"].discriminator.mapping[
"grooming"
]

assert %{
"UserRequest" => %Schema{},
Expand Down Expand Up @@ -250,7 +254,9 @@ defmodule OpenApiSpex.SchemaResolverTest do
}

error_message = """
Cannot resolve schema, must be one of:
Cannot resolve schema %{}.
Must be one of:
- schema module, or schema struct
- list of schema modules, or schema structs
Expand Down

0 comments on commit a332a06

Please sign in to comment.