Skip to content

[BUG] Explicit discriminator mappings do not override defaults #20938

Open
@TheMrMilchmann

Description

@TheMrMilchmann
Description

Given an object with oneOf and a discriminator mapping, the generator erroneously still allows the schema names as aliases during de-/serialization (even if an explicit mapping for a respective referenced schema exists).

The spec says that only the explicit mappings should be usable. (See https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/#discriminator)

For example:

    Fruit:
      discriminator:
        mapping:
          APPLE: '#/components/schemas/Apple'
          BANANA: '#/components/schemas/Banana'
        propertyName: fruitType
      oneOf:
      - $ref: '#/components/schemas/Apple'
      - $ref: '#/components/schemas/Banana'
      properties:
        fruitType:
          $ref: '#/components/schemas/FruitType'
      required:
      - fruitType
      type: object
      x-one-of-name: Fruit
    Apple:
      properties:
        seeds:
          type: integer
      required:
      - seeds
      type: object
    Banana:
      properties:
        length:
          type: integer
      required:
      - length
      type: object
@JsonSubTypes({
  @JsonSubTypes.Type(value = Apple.class, name = "APPLE"),
  @JsonSubTypes.Type(value = Banana.class, name = "BANANA"),
  @JsonSubTypes.Type(value = Apple.class, name = "Apple"),    // <-- Wrong
  @JsonSubTypes.Type(value = Banana.class, name = "Banana")   // <-- Wrong
})

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.13.0-SNAPSHOT")
public interface Fruit {
openapi-generator version

This is present on master and affects at least 7.11.0 and 7.12.0. After digging into the code and git history, I have no reason to believe that there are unaffected versions.

OpenAPI declaration file content or URL

https://github.com/OpenAPITools/openapi-generator/blob/f39675b41af49851f05056009ccd4a823770a881/samples/openapi3/server/petstore/spring-boot-oneof/src/main/resources/openapi.yaml

Generation Details

No special set up required. Just use the sample.

Steps to reproduce

Just use the sample.

Related issues/PRs
Suggest a fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions