Skip to content

[REQ] [Java] Allow excluding discriminator property from generated models #24769

Description

@Jonaythaz

Is your feature request related to a problem? Please describe.

When I have a schema with a union type like in the following example:

components:
  schemas:
    Pet:
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
      discriminator:
        propertyName: type
        mapping:
          CAT: '#/components/schemas/Cat'
          DOG: '#/components/schemas/Dog'
    Cat:
      description: A representation of a cat
      type: object
      properties:
        type:
          type: string
          description: The type of the Pet.
          const: CAT
        huntingSkill:
          type: string
          description: The measured skill for hunting
          enum:
          - clueless
          - lazy
          - adventurous
          - aggressive
      required:
      - type
      - huntingSkill
    Dog:
      description: A representation of a dog
      type: object
      properties:
        type:
          type: string
          description: The type of the Pet.
          const: DOG
        packSize:
          type: integer
          format: int32
          description: the size of the pack the dog is from
          default: 0
          minimum: 0
      required:
      - type
      - packSize

I would want it to generate something like the following:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = Cat.class, name = "CAT"),
  @JsonSubTypes.Type(value = Dog.class, name = "DOG"),
})
public sealed interface Pet permits Cat, Dog {
}

public class Cat implements Pet {
    public HuntingSkill getHuntingSkill() {...}
}

public class Dog implements Pet {
    public Integer getPackSize() {...}
}

but it doesn't, instead it generates something like:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = Cat.class, name = "CAT"),
  @JsonSubTypes.Type(value = Dog.class, name = "DOG"),
})
public sealed interface Pet permits Cat, Dog {
    public String getType();
}

public class Cat implements Pet {
    public TypeEnum getType() {...}
    public HuntingSkill getHuntingSkill() {...}
}

public class Dog implements Pet {
    public TypeEnum getType() {...}
    public Integer getPackSize() {...}
}

which doesn't compile.

A workaround does exist, by making the discriminator property, here type, an enum and having Pet, Cat, and Dog use that enum as its type. But this workaround makes the schema incorrect, since e.g. Dog should only ever have the type DOG, which creates problems for consumers using client generation in other languages like TypeScript.

Describe the solution you'd like

I would personally like an option to just not include the discriminator property in the generated models, since its value is encoded in the class instance, or at the very least not have it in the interface such that it can compile.

Describe alternatives you've considered

Additional context

To verify the behaviour I have used v7.25.0 of the CLI through npx @openapitools/openapi-generator-cli, with the following config file:

{
  "serializationLibrary": "jackson",
  "additionalProperties": {
    "useOneOfInterfaces": true,
    "useSealedOneOfInterfaces": true,
    "library": "microprofile"
  }
}

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