Skip to content

Commit 439fadc

Browse files
authored
Document schema resolver duplicate titles behaviour (#656)
1 parent 4a3f7a3 commit 439fadc

File tree

3 files changed

+281
-193
lines changed

3 files changed

+281
-193
lines changed

lib/open_api_spex.ex

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ defmodule OpenApiSpex do
2929
Then the `UserResponse.schema()` function will be called to load the schema, and
3030
a `Reference` to the loaded schema will be used in the operation response.
3131
32-
See `OpenApiSpex.schema` macro for a convenient syntax for defining schema modules.
32+
See `OpenApiSpex.schema/2` macro for a convenient syntax for defining schema modules.
33+
34+
> #### Known Issues {: .info}
35+
>
36+
> Resolving schemas expects the schema title to be unique for the generated references to be unique.
37+
>
38+
> For schemas defined with the `OpenApiSpex.schema/2` macro, the title is automatically set
39+
> to the last part of module name. For example `MyAppWeb.Schemas.User` will have the title `"User"`,
40+
> and `MyAppWeb.OtherSchemas.User` **will also** have the title `"User"` which can lead to conflicts.
41+
>
42+
> The recommendation is to set the title explicitly in the schema definition.
3343
"""
3444
@spec resolve_schema_modules(OpenApi.t()) :: OpenApi.t()
3545
def resolve_schema_modules(spec = %OpenApi{}) do

lib/open_api_spex/schema_resolver.ex

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule OpenApiSpex.SchemaResolver do
22
@moduledoc """
33
Internal module used to resolve `OpenApiSpex.Schema` structs from atoms.
44
"""
5+
56
alias OpenApiSpex.Discriminator
67

78
alias OpenApiSpex.{
@@ -29,7 +30,17 @@ defmodule OpenApiSpex.SchemaResolver do
2930
Then the `UserResponse.schema()` function will be called to load the schema, and
3031
a `Reference` to the loaded schema will be used in the operation response.
3132
32-
See `OpenApiSpex.schema` macro for a convenient syntax for defining schema modules.
33+
See `OpenApiSpex.schema/2` macro for a convenient syntax for defining schema modules.
34+
35+
> #### Known Issues {: .info}
36+
>
37+
> Resolving schemas expects the schema title to be unique for the generated references to be unique.
38+
>
39+
> For schemas defined with the `OpenApiSpex.schema/2` macro, the title is automatically set
40+
> to the last part of module name. For example `MyAppWeb.Schemas.User` will have the title `"User"`,
41+
> and `MyAppWeb.OtherSchemas.User` **will also** have the title `"User"` which can lead to conflicts.
42+
>
43+
> The recommendation is to set the title explicitly in the schema definition.
3344
"""
3445
@spec resolve_schema_modules(OpenApi.t()) :: OpenApi.t()
3546
def resolve_schema_modules(spec = %OpenApi{}) do
@@ -197,14 +208,15 @@ defmodule OpenApiSpex.SchemaResolver do
197208
Enum.map_reduce(schema_list, schemas, &resolve_schema_modules_from_schema/2)
198209
end
199210

200-
defp resolve_schema_modules_from_schema(schema, schemas) when is_atom(schema) do
201-
title = schema.schema().title
211+
defp resolve_schema_modules_from_schema(schema_module, schemas) when is_atom(schema_module) do
212+
schema = schema_module.schema()
213+
title = schema.title
202214

203215
new_schemas =
204216
if Map.has_key?(schemas, title) do
205217
schemas
206218
else
207-
{new_schema, schemas} = resolve_schema_modules_from_schema(schema.schema(), schemas)
219+
{new_schema, schemas} = resolve_schema_modules_from_schema(schema, schemas)
208220
Map.put(schemas, title, new_schema)
209221
end
210222

0 commit comments

Comments
 (0)