Skip to content

Commit

Permalink
Raise meaningful error message when SchemaResolver.resolve_schema_mod…
Browse files Browse the repository at this point in the history
…ules_from_schema failed to pattern match (#541)

Improve error message when invalid schema property type is provided

Co-authored-by: Dimitris Zorbas <dimitrisplusplus@gmail.com>
  • Loading branch information
yuchunc and zorbash committed May 29, 2023
1 parent 94813ba commit 2ca69e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/open_api_spex/schema_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ defmodule OpenApiSpex.SchemaResolver do

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

defp resolve_schema_modules_from_schema(_, _) do
error_message = """
Cannot resolve schema, must be one of:
- schema module, or schema struct
- list of schema modules, or schema structs
- boolean
- nil
- reference
"""

raise error_message
end

defp resolve_schema_modules_from_schema_properties(nil, schemas), do: {nil, schemas}

defp resolve_schema_modules_from_schema_properties(properties, schemas)
Expand Down
39 changes: 39 additions & 0 deletions test/schema_resolver_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,45 @@ defmodule OpenApiSpex.SchemaResolverTest do
end
end

test "raises error when schema cannot be resolved" do
spec = %OpenApi{
info: %Info{
title: "Test",
version: "1.0.0"
},
paths: %{
"/api/users" => %PathItem{
get: %Operation{
responses: %{
200 => %Response{
description: "Success",
content: %{
"application/json" => %MediaType{
schema: %{}
}
}
}
}
}
}
}
}

error_message = """
Cannot resolve schema, must be one of:
- schema module, or schema struct
- list of schema modules, or schema structs
- boolean
- nil
- reference
"""

assert_raise RuntimeError, error_message, fn ->
OpenApiSpex.resolve_schema_modules(spec)
end
end

describe "add_schemas/2" do
@empty_spec %OpenApi{
info: %Info{
Expand Down

0 comments on commit 2ca69e0

Please sign in to comment.