Skip to content

fix: duplicated discriminator serialisation issue#1220

Merged
jjjasper merged 4 commits intomainfrom
fix/dublicated-discriminator-issue
Mar 5, 2026
Merged

fix: duplicated discriminator serialisation issue#1220
jjjasper merged 4 commits intomainfrom
fix/dublicated-discriminator-issue

Conversation

@YuriiBatkovych
Copy link
Collaborator

This PR addresses OpenAPI-Generator Issue #3796, where the discriminator property is serialized twice in Java Jackson projects.

The Problem

When using a discriminator with Jackson, the generator currently includes allowGetters = true within the @JsonIgnoreProperties annotation. This configuration inadvertently tells Jackson that even though the property is "ignored," it is still allowed to be accessed via its getter during the serialization process.

Because @JsonTypeInfo is also configured to include the discriminator (via visible = true and include = JsonTypeInfo.As.PROPERTY), Jackson ends up writing the field twice:

  • Once from the explicit class property/getter.
  • Once from the internal Jackson polymorphic handling.

The Fix

We have modified typeInfoAnnotation.mustache to remove allowGetters = true.

  • Removal of allowGetters = true: This ensures that Jackson ignores the manual getter during serialization, preventing the duplicate field.

  • Retention of allowSetters = true: This remains necessary to ensure that the discriminator can still be mapped to the class field during deserialization.

@YuriiBatkovych YuriiBatkovych changed the title fix: duplicated discriminator serialisation issue fix: duplicated discriminator serialisation issue [Work In progress] Mar 3, 2026
@YuriiBatkovych YuriiBatkovych force-pushed the fix/dublicated-discriminator-issue branch from a332607 to 9819d31 Compare March 4, 2026 14:45
@YuriiBatkovych YuriiBatkovych changed the title fix: duplicated discriminator serialisation issue [Work In progress] fix: duplicated discriminator serialisation issue Mar 4, 2026
@YuriiBatkovych YuriiBatkovych requested a review from jjjasper March 4, 2026 15:37
@jjjasper jjjasper force-pushed the fix/dublicated-discriminator-issue branch from e017b83 to 2da0757 Compare March 5, 2026 08:22
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 5, 2026

@jjjasper jjjasper merged commit 7623902 into main Mar 5, 2026
4 checks passed
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

Successfully merging this pull request may close these issues.

2 participants