Skip to content

discriminator property not added to components under oneOf with django-rest-polymorphic #885

@Numerlor

Description

@Numerlor

Describe the bug
When the schema for a view using the PolymorphicSerializer from django-rest-polymorphic is generated, it uses the serializer's resource_type_field_name for the discriminator but the property under the same name is not added to the components generated from the sub-serializers.
This causes it to not be included in the schema in swagger/redoc and a mismatch between what the api expects.

To Reproduce
run generation on

from django.db import models
from polymorphic.models import PolymorphicModel
from rest_framework.generics import CreateAPIView
from rest_framework.serializers import ModelSerializer
from rest_polymorphic.serializers import PolymorphicSerializer


class ExampleModel(PolymorphicModel):
    ...

class SubModel1(ExampleModel):
    field1 = models.CharField()

class SubModel2(ExampleModel):
    field2 = models.CharField()


class SubModel1Serializer(ModelSerializer):
    class Meta:
        model = SubModel1
        exclude = ("polymorphic_ctype",)

class SubModel2Serializer(ModelSerializer):
    class Meta:
        model = SubModel2
        exclude = ("polymorphic_ctype",)

class ExampleModelSerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        SubModel1: SubModel1Serializer,
        SubModel2: SubModel2Serializer,
    }

class ExampleView(CreateAPIView):
    queryset = ExampleModel.objects.all()
    serializer_class = ExampleModelSerializer

Expected behavior
string property under the same name as resource_type_field_name added to components generator for polymorphic serializer so tooling picks it up

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfix confirmation pendingissue has been fixed and confirmation from issue reporter is pending

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions