|
| 1 | +from random import choice |
1 | 2 | from unittest import mock |
2 | 3 |
|
3 | 4 | import pytest |
@@ -324,6 +325,45 @@ def view_func(request, format=None): |
324 | 325 | } |
325 | 326 |
|
326 | 327 |
|
| 328 | +def test_polymorphic_with_default_serializer(no_warnings): |
| 329 | + class DefaultPersonSerializer(serializers.ModelSerializer): |
| 330 | + type = serializers.SerializerMethodField() |
| 331 | + |
| 332 | + class Meta: |
| 333 | + model = NaturalPerson2 |
| 334 | + fields = ('id', 'type') |
| 335 | + |
| 336 | + def get_type(self, obj) -> str: |
| 337 | + return choice(['basic', 'simple']) |
| 338 | + |
| 339 | + class XViewSet(viewsets.GenericViewSet): |
| 340 | + @extend_schema( |
| 341 | + responses=PolymorphicProxySerializer( |
| 342 | + component_name='MetaPerson', |
| 343 | + serializers={ |
| 344 | + 'natural': NaturalPersonSerializer, |
| 345 | + 'basic': DefaultPersonSerializer, |
| 346 | + 'simple': DefaultPersonSerializer, |
| 347 | + }, |
| 348 | + resource_type_field_name='type', |
| 349 | + ) |
| 350 | + ) |
| 351 | + def list(self, request, *args, **kwargs): |
| 352 | + return Response({}) # pragma: no cover |
| 353 | + |
| 354 | + schema = generate_schema('x', XViewSet) |
| 355 | + components = schema['components']['schemas'] |
| 356 | + assert components['MetaPerson']['oneOf'] == [ |
| 357 | + {'$ref': '#/components/schemas/NaturalPerson'}, |
| 358 | + {'$ref': '#/components/schemas/DefaultPerson'} |
| 359 | + ] |
| 360 | + assert components['MetaPerson']['discriminator']['mapping'] == { |
| 361 | + 'natural': '#/components/schemas/NaturalPerson', |
| 362 | + 'basic': '#/components/schemas/DefaultPerson', |
| 363 | + 'simple': '#/components/schemas/DefaultPerson' |
| 364 | + } |
| 365 | + |
| 366 | + |
327 | 367 | def test_polymorphic_forced_many_false(no_warnings): |
328 | 368 | class XViewSet(viewsets.GenericViewSet): |
329 | 369 | @extend_schema( |
|
0 commit comments