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

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

Open
TheMrMilchmann opened this issue Mar 21, 2025 · 2 comments
Open

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

TheMrMilchmann opened this issue Mar 21, 2025 · 2 comments

Comments

@TheMrMilchmann
Copy link

TheMrMilchmann commented Mar 21, 2025

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
@TheMrMilchmann TheMrMilchmann changed the title [BUG] Description [BUG] Explicit discriminator mappings do not override defaults Mar 21, 2025
@Mattias-Sehlstedt
Copy link
Contributor

Hi, could you clarify what exact sentence(s) from https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/#discriminator that state

The spec says that only the explicit mappings should be usable.

I am unable to read that from the descriptions present on that page.

@TheMrMilchmann
Copy link
Author

Sorry, wrong anchor. The example in https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/#mapping-type-names is pretty clear.

In this example, the obj1 value is mapped to the Object1 model that is defined in the same spec, obj2 – to Object2, and the value system matches the sysObject model that is located in an external file. All these objects must have the objectType property with the value "obj1", "obj2" or "system", respectively.

(Emphasis mine)

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

No branches or pull requests

2 participants