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

Regression: Swagger CodeGen generates duplicate fields in child DTOs #12409

Open
andrewfg opened this issue May 30, 2024 · 2 comments
Open

Regression: Swagger CodeGen generates duplicate fields in child DTOs #12409

andrewfg opened this issue May 30, 2024 · 2 comments

Comments

@andrewfg
Copy link

andrewfg commented May 30, 2024

Regression v2.3.1 to v2.4.41

Problem

  • In v2.3.1 when Swagger CodeGen generates a child Java DTO class that inherits the fields of a parent class, the child class does not contain new fields. Instead it re-uses the existing fields inherited from the parent. This is the correct behaviour.

  • In v2.4.41 when Swagger CodeGen generates a child Java DTO class that inherits the fields of a parent class, the child class contains extra fields that duplicate the existing fields of the parent. This is incorrect behaviour. It causes JSON (de-)serializers to throw an exception due to duplicate fields having the same name.

Background

In OpenHAB one of its 450 odd third party integrations uses Swagger CodeGen to convert an API YAML file to respective Java API DTOs. We were using CodeGen v2.3.1 but we recently attempted to upgrade to CodeGen v2.4.41 -- but failed.

The API and the respective YAML have API fields name 'type' and CodeGen v2.3.1 creates them them in the DTO and applies the @SerializedName("type") annotation to them. When attemting to use CodeGen v2.4.41 we started seeing GSON exceptions that the DTO "declares multiple JSON fields named 'type'".

I am wondering what happened, and how to fix it?

See this openhab/openhab-addons#16779 for more..

@andrewfg
Copy link
Author

andrewfg commented May 31, 2024

The issue occurs when a YAML DTO extends another DTO as follows where TadoModeTerminationCondition implements allOf the fields of OverlayTerminationCondition. CodeGen v2.3.1 generates Java code in which the child DTO does NOT include the same fields as the parent DTO (since they are already inherently in the parent). By contrast CodeGen v2.4.41 generates code where the child DTO has fields that DUPLICATE the same fields in the parent. Which is what causes the GSON JSON deserializer to fall over..

================ YAML Definition ================

OverlayTerminationCondition:
    type: object
    discriminator: type
    properties:
      type:
        $ref: "#/definitions/OverlayTerminationConditionType"
      projectedExpiry:
        description: |
          [ISO8601 datetime](https://en.wikipedia.org/wiki/ISO_8601). E.g. `2015-09-28T15:03:20Z` with second precision.
          Only relevant when receiving an overlay, ignored when overlay is sent to the server. Indicates the expected time of
          termination for this overlay, if no app user moves. `null` means that the overlay never expires (by itself, unless manully removed).
        type: string
        format: date-time
        readOnly: true
    required:
    - type

TadoModeTerminationCondition:
    description: The overlay terminates when the tado mode changes or when the setting (power, temperature, ...) of the underlying block schedule changes (or when removed manually).
    x-discriminator-value: TADO_MODE
    allOf:
    - $ref: "#/definitions/OverlayTerminationCondition"

================ Output of Swagger CodeGen v2.3.1 ================

public class OverlayTerminationCondition  {
  @SerializedName("type")
  private OverlayTerminationConditionType type = null;

  @SerializedName("projectedExpiry")
  private OffsetDateTime projectedExpiry = null;

  ... methods
}


public class TadoModeTerminationCondition extends OverlayTerminationCondition  {

  .. inherited methods all annotated with @Override
}

================ Output of Swagger CodeGen v2.4.41 ================

public class OverlayTerminationCondition  {
  @SerializedName("type")
  private OverlayTerminationConditionType type = null;

  @SerializedName("projectedExpiry")
  private OffsetDateTime projectedExpiry = null;

  ... methods
}


public class TadoModeTerminationCondition extends OverlayTerminationCondition  {
  @SerializedName("type")
  private OverlayTerminationConditionType type = null;

  @SerializedName("projectedExpiry")
  private OffsetDateTime projectedExpiry = null;

  .. inherited methods all annotated with @Override
}

@andrewfg andrewfg changed the title Swagger CodeGen declares multiple JSON fields named 'type' Regression: Swagger CodeGen generates duplicate fields in child DTOs May 31, 2024
@andrewfg
Copy link
Author

^
From deep Googling one hypothesis is the issue may be related to supportsInheritance. => Does anyone have thought on this? And is so, how would I set a different value of that property for the Swagger Maven plugin in the OpenHAB pom.xml file?

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

No branches or pull requests

1 participant