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

No extra items in the oneOf list #1293

Merged
merged 1 commit into from
Oct 3, 2024
Merged

Conversation

DopustimVladimir
Copy link
Contributor

In case we want to map some different values to the same serializer:

class XViewSet(viewsets.GenericViewSet):
    @extend_schema(
        responses=PolymorphicProxySerializer(
            component_name='MetaPerson',
            serializers={
                'natural': NaturalPersonSerializer,
                'basic': DefaultPersonSerializer,
                'simple': DefaultPersonSerializer,
            },
            resource_type_field_name='type',
        )
    )

We don't need extra items in the oneOf section like here:

components:
  schemas:
    MetaPerson:
      oneOf:
      - $ref: '#/components/schemas/NaturalPerson'
      - $ref: '#/components/schemas/DefaultPerson'
      - $ref: '#/components/schemas/DefaultPerson' # extra item
      discriminator:
        propertyName: type
        mapping:
          natural: '#/components/schemas/NaturalPerson'
          basic: '#/components/schemas/DefaultPerson'
          simple: '#/components/schemas/DefaultPerson'

The expected result is:

components:
  schemas:
    MetaPerson:
      oneOf:
      - $ref: '#/components/schemas/NaturalPerson'
      - $ref: '#/components/schemas/DefaultPerson'
      discriminator:
        propertyName: type
        mapping:
          natural: '#/components/schemas/NaturalPerson'
          basic: '#/components/schemas/DefaultPerson'
          simple: '#/components/schemas/DefaultPerson'

@tfranzel
Copy link
Owner

tfranzel commented Oct 3, 2024

Hey @DopustimVladimir,

nice catch. This is of course results in a cleaner schema. I'm just wondering where the duplication became a problem?

@tfranzel tfranzel merged commit 09c5138 into tfranzel:master Oct 3, 2024
32 checks passed
@DopustimVladimir
Copy link
Contributor Author

Hey @DopustimVladimir,

nice catch. This is of course results in a cleaner schema. I'm just wondering where the duplication became a problem?

Oh, actually I can't really call it a problem but Orval takes it as is and gives out weird things like:

export type MetaPerson = NaturalPerson | DefaultPerson | DefaultPerson;

Expected result is:

export type MetaPerson = NaturalPerson | DefaultPerson;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants