Skip to content

Commit

Permalink
Merge pull request #1033 from tfranzel/fix_poly_ro_serializer
Browse files Browse the repository at this point in the history
fix django-polymorphic empty serializer case #1029 #542
  • Loading branch information
tfranzel committed Jul 22, 2023
2 parents b7f79b7 + 1481111 commit bc9910c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
29 changes: 17 additions & 12 deletions drf_spectacular/contrib/rest_polymorphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def map_serializer(self, auto_schema, direction):
resource_type = serializer.to_resource_type(sub_model)
component = auto_schema.resolve_serializer(sub_serializer, direction)
if not component:
continue
# rebuild a virtual schema-less component to model empty serializers
component = ResolvedComponent(
name=auto_schema._get_serializer_name(sub_serializer, direction),
type=ResolvedComponent.SCHEMA,
object='virtual'
)
typed_component = self.build_typed_component(
auto_schema=auto_schema,
component=component,
Expand Down Expand Up @@ -49,21 +54,21 @@ def build_typed_component(self, auto_schema, component, resource_type_field_name
else:
typed_component_name = f'{component.name}Typed'

resource_type_schema = build_object_type(
properties={resource_type_field_name: build_basic_type(OpenApiTypes.STR)},
required=None if patched else [resource_type_field_name]
)
# if sub-serializer has an empty schema, only expose the resource_type field part
if component.schema:
schema = {'allOf': [resource_type_schema, component.ref]}
else:
schema = resource_type_schema

component_typed = ResolvedComponent(
name=typed_component_name,
type=ResolvedComponent.SCHEMA,
object=component.object,
schema={
'allOf': [
build_object_type(
properties={
resource_type_field_name: build_basic_type(OpenApiTypes.STR)
},
required=None if patched else [resource_type_field_name]
),
component.ref,
]
}
schema=schema,
)
auto_schema.registry.register_on_missing(component_typed)
return component_typed
7 changes: 7 additions & 0 deletions tests/contrib/test_rest_polymorphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,17 @@ def test_rest_polymorphic_split_request_with_ro_serializer(no_warnings):
assert components['PersonRequest']['oneOf'] == [
{'$ref': '#/components/schemas/LegalPersonTypedRequest'},
{'$ref': '#/components/schemas/NaturalPersonTypedRequest'},
{'$ref': '#/components/schemas/NomadicPersonTypedRequest'},
]
assert components['PersonRequest']['discriminator']['mapping'] == {
'legal': '#/components/schemas/LegalPersonTypedRequest',
'natural': '#/components/schemas/NaturalPersonTypedRequest',
'nomadic': '#/components/schemas/NomadicPersonTypedRequest',
}
assert components['NomadicPersonTypedRequest'] == {
'properties': {'resourcetype': {'type': 'string'}},
'required': ['resourcetype'],
'type': 'object',
}


Expand Down

0 comments on commit bc9910c

Please sign in to comment.