swagger-api / swagger-api/swagger-codegen

Polymorphism issue

Open
#12,083 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When using polymorphism, the JsonSubType is duplicated, like on this example:

@JsonIgnoreProperties(
  value = "type", // ignore manually set type, it will be automatically generated by Jackson during serialization
  allowSetters = true // allows the type to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = CurrencyContextValue.class, name = "CurrencyContextValue"),
  @JsonSubTypes.Type(value = DateTimeContextValue.class, name = "DateTimeContextValue"),
  @JsonSubTypes.Type(value = CurrencyContextValue.class, name = "currency"),
  @JsonSubTypes.Type(value = DateTimeContextValue.class, name = "date_time"),
})

In this example the JsonSubTypes.Type annotation is duplicated.
If I remove the allOf on CurrencyContextValue and on DateTimeContextValue I have no issue.
It seem that the allOf don't use the discriminator field. I can't find any solution.

Swagger-codegen version

I use the gradle plugin org.openapitools:openapi-generator-gradle-plugin:6.4.0

Swagger declaration file content or url
CurrencyContextValue:
      required:
        - currency
      type: object
      example:
        type: currency
        currency: EUR
      allOf:
        - $ref: '#/components/schemas/WhatsAppContextValue'
        - type: object
          properties:
            currency:
              type: string
    DateTimeContextValue:
      required:
        - dateTime
      type: object
      example:
        type: date_time
        dateTime: '2022-10-10T10:10:10.000'
      allOf:
        - $ref: '#/components/schemas/WhatsAppContextValue'
        - type: object
          properties:
            dateTime:
              type: string
              format: date-time
    WhatsAppContext:
      type: object
      properties:
        header:
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/CurrencyContextValue'
              - $ref: '#/components/schemas/DateTimeContextValue'
            discriminator:
              propertyName: type
      description: The context is used to fill template variables.
    WhatsAppContextValue:
      required:
        - type
      type: object
      properties:
        type:
          type: string
      discriminator:
        propertyName: type
        mapping:
          currency: '#/components/schemas/CurrencyContextValue'
          date_time: '#/components/schemas/DateTimeContextValue'

This swagger is generated from java annotation using these class:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    property = "type"
)
@JsonSubTypes({
    @JsonSubTypes.Type(value = WhatsAppContextValue.TextContextValue.class, name = "text"),
    @JsonSubTypes.Type(value = WhatsAppContextValue.CurrencyContextValue.class, name = "currency"),
    @JsonSubTypes.Type(value = WhatsAppContextValue.DateTimeContextValue.class, name = "date_time")
})
@Schema(
    name = "WhatsAppContextValue",
    discriminatorProperty = "type",
    discriminatorMapping = {
        @DiscriminatorMapping(value = "text", schema = WhatsAppContextValue.TextContextValue.class),
        @DiscriminatorMapping(value = "currency", schema = WhatsAppContextValue.CurrencyContextValue.class),
        @DiscriminatorMapping(value = "date_time", schema = WhatsAppContextValue.DateTimeContextValue.class)
    }
)
public sealed interface WhatsAppContextValue 

Thanks for you help

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the generated Java annotations with the openapi-generator-gradle-plugin at version 6.4.0 using the supplied schemas and Java annotations. Trace how allOf and discriminator mappings are processed during Java model generation, then compare the generated JsonSubTypes entries with the currency and date_time mappings. Done means the subtype annotations are not duplicated.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.