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

Fix crash with read-only polymorphic sub-serializer. #542

Merged
merged 1 commit into from
Oct 1, 2021
Merged
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
6 changes: 4 additions & 2 deletions drf_spectacular/contrib/rest_polymorphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ def map_serializer(self, auto_schema, direction):
sub_serializer = serializer._get_serializer_from_model_or_instance(sub_model)
sub_serializer.partial = serializer.partial
resource_type = serializer.to_resource_type(sub_model)
ref = auto_schema.resolve_serializer(sub_serializer, direction).ref
sub_components.append((resource_type, ref))
component = auto_schema.resolve_serializer(sub_serializer, direction)
if not component:
continue
sub_components.append((resource_type, component.ref))

if not resource_type:
warn(
Expand Down
35 changes: 35 additions & 0 deletions tests/contrib/test_rest_polymorphic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# type: ignore
from unittest import mock

import pytest
from django.db import models
from rest_framework import serializers, viewsets
Expand Down Expand Up @@ -33,10 +35,17 @@ class NaturalPerson(Person):
supervisor = models.ForeignKey('NaturalPerson', blank=True, null=True, on_delete=models.CASCADE)


class NomadicPerson(Person):
@property
def address(self):
return 'Anywhere!'


class PersonSerializer(PolymorphicSerializer):
model_serializer_mapping = {
LegalPerson: lazy_serializer('tests.contrib.test_rest_polymorphic.LegalPersonSerializer'),
NaturalPerson: lazy_serializer('tests.contrib.test_rest_polymorphic.NaturalPersonSerializer'),
NomadicPerson: lazy_serializer('tests.contrib.test_rest_polymorphic.NomadicPersonSerializer'),
}

def to_resource_type(self, model_or_instance):
Expand Down Expand Up @@ -64,6 +73,15 @@ class Meta:
fields = ('id', 'first_name', 'last_name', 'address', 'supervisor_id')


class NomadicPersonSerializer(serializers.ModelSerializer):
# special case: all fields are read-only.
address = serializers.CharField(max_length=30, read_only=True)

class Meta:
model = NomadicPerson
fields = ('id', 'address')


class PersonViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
Expand All @@ -77,6 +95,23 @@ def test_rest_polymorphic(no_warnings):
)


@pytest.mark.contrib('polymorphic', 'rest_polymorphic')
@mock.patch('drf_spectacular.settings.spectacular_settings.COMPONENT_SPLIT_REQUEST', True)
def test_rest_polymorphic_split_request(no_warnings):
schema = generate_schema('persons', PersonViewSet)
names = set(schema['components']['schemas'])
assert 'LegalPersonRequest' in names
assert 'NaturalPersonRequest' in names
assert 'NomadicPersonRequest' not in names # All fields were read-only.
assert 'PatchedLegalPersonRequest' in names
assert 'PatchedNaturalPersonRequest' in names
assert 'PatchedNomadicPersonRequest' not in names # All fields were read-only.
assert_schema(
generate_schema('persons', PersonViewSet),
'tests/contrib/test_rest_polymorphic_split_request.yml',
)


@pytest.mark.contrib('polymorphic', 'rest_polymorphic')
@pytest.mark.django_db
def test_model_setup_is_valid():
Expand Down
27 changes: 27 additions & 0 deletions tests/contrib/test_rest_polymorphic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ components:
- id
- last_name
- supervisor_id
NomadicPerson:
type: object
properties:
id:
type: integer
readOnly: true
address:
type: string
readOnly: true
maxLength: 30
required:
- address
- id
PatchedLegalPerson:
type: object
properties:
Expand Down Expand Up @@ -237,24 +250,38 @@ components:
supervisor_id:
type: integer
nullable: true
PatchedNomadicPerson:
type: object
properties:
id:
type: integer
readOnly: true
address:
type: string
readOnly: true
maxLength: 30
PatchedPerson:
oneOf:
- $ref: '#/components/schemas/PatchedLegalPerson'
- $ref: '#/components/schemas/PatchedNaturalPerson'
- $ref: '#/components/schemas/PatchedNomadicPerson'
discriminator:
propertyName: resourcetype
mapping:
legal: '#/components/schemas/PatchedLegalPerson'
natural: '#/components/schemas/PatchedNaturalPerson'
nomadic: '#/components/schemas/PatchedNomadicPerson'
Person:
oneOf:
- $ref: '#/components/schemas/LegalPerson'
- $ref: '#/components/schemas/NaturalPerson'
- $ref: '#/components/schemas/NomadicPerson'
discriminator:
propertyName: resourcetype
mapping:
legal: '#/components/schemas/LegalPerson'
natural: '#/components/schemas/NaturalPerson'
nomadic: '#/components/schemas/NomadicPerson'
securitySchemes:
basicAuth:
type: http
Expand Down
Loading