Skip to content

[BUG] [JAVA][jackson] disableDsicrimniatorFieldIgnore causes double serialization #22923

@dschodits-secunet

Description

@dschodits-secunet

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

This PR introduced the disableDiscriminatorFieldIgnore config.
Due to the fact that the include parameter in the @JsonTypeInfo is still JsonTypeInfo.As.Property you run the risk of having the discriminator serialized twice. Which is not good.

See my comment on the original PR for more context: #22528 (comment)

I will open a PR shortly with a fix

openapi-generator version

Since this PR was merged #22528

OpenAPI declaration file content or url
    Pet:
      type: object
      discriminator:
        propertyName: petIntType
        mapping:
          0: '#/components/schemas/Dog'
          1: '#/components/schemas/Cat'
      properties:
        name:
          type: string
        petIntType:
          type: integer
      required:
        - name
    Cat: 
      description: A representation of a cat
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            huntingSkill:
              type: string
          required:
            - huntingSkill
    Dog: 
      description: A representation of a dog
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            packSize:
              type: integer
              format: int32
          required:
            - packSize
final var response = new Dog().petIntType(0).packSize(10).name("Dogo");
String ser = new ObjectMapper().writeValueAsString(response);
System.out.println("Seriliazied: " + ser)
 Seriliazied: {"petIntType":"0","packSize":10,"name":"Dogo","petIntType":0}
Generation Details
Steps to reproduce

Use the above Specification and Java code. Generate it with disableDiscriminatorFieldIgnore set to true.

Related issues/PRs

#22528

Suggest a fix

The most important part (at least I think so) is that the JsonTypeInfo is updated to EXISTING_PROPERTY
Something like this should do the trick

{{^disableDiscriminatorJsonIgnoreProperties}}
@JsonIgnoreProperties(
  value = "{{{discriminator.propertyBaseName}}}", // ignore manually set {{{discriminator.propertyBaseName}}}, it will be automatically generated by Jackson during serialization
  allowSetters = true // allows the {{{discriminator.propertyBaseName}}} to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "{{{discriminator.propertyBaseName}}}", visible = true)
{{/disableDiscriminatorJsonIgnoreProperties}}

{{#disableDiscriminatorJsonIgnoreProperties}}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "{{{discriminator.propertyBaseName}}}", visible = true)
{{/disableDiscriminatorJsonIgnoreProperties}}

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