Skip to content

[Java] ensure JsonTypeName not generated for class implementing oneOf with discriminator#24024

Open
jpfinne wants to merge 13 commits into
OpenAPITools:masterfrom
jpfinne:hasDiscriminatorWithNonEmptyMapping
Open

[Java] ensure JsonTypeName not generated for class implementing oneOf with discriminator#24024
jpfinne wants to merge 13 commits into
OpenAPITools:masterfrom
jpfinne:hasDiscriminatorWithNonEmptyMapping

Conversation

@jpfinne

@jpfinne jpfinne commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Fixes #23997

For a child that's part of a non-empty discriminator mapping, the parent interface already declares the type id via @JsonSubTypes.Type(name = ...), so emitting @JsonTypeName on the child is redundant (and was the source of the original mismatch). Marking the mapped child models with hasDiscriminatorWithNonEmptyMapping so the existing {{^hasDiscriminatorWithNonEmptyMapping}} guard suppresses @JsonTypeName

The fix applies for all generators using Jackson and CodegenModel.hasDiscriminatorWithNonEmptyMapping

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Java | @bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)

Java Spring | @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)


Summary by cubic

Stop generating @JsonTypeName for child models when a parent oneOf interface uses a discriminator with a non-empty mapping. This removes redundant annotations and avoids type id mismatches with the parent’s @JsonSubTypes and @JsonTypeInfo.

  • Bug Fixes
    • In DefaultCodegen, mark mapped child models with hasDiscriminatorWithNonEmptyMapping (with null-safe handling) so templates skip emitting @JsonTypeName across Jackson-based generators.
    • Expanded issue24003 Spring test and issue_24003.yaml to assert the parent interface has @JsonTypeInfo (including visible = true) and @JsonSubTypes, and child classes do not contain @JsonTypeName.

Written for commit dc0d05b. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

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.

[BUG] [JAVA][SPRING] @JsonTypeName ignores x-discriminator-value and discriminator mappings

1 participant