[Java][Spring] Use discriminator value for @JsonTypeName instead of schema name (#23997)#24003
Open
seonwooj0810 wants to merge 1 commit into
Conversation
…chema name (OpenAPITools#23997) For the java and spring generators, child models honor an explicit x-discriminator-value vendor extension when rendering the Jackson @JsonTypeName annotation, falling back to the schema/model name when no discriminator value is available. Previously the schema/model name was always used, which broke Jackson polymorphic serialization whenever the discriminator value differed from the model name (e.g. when modelNameSuffix is used or an explicit mapping is declared). Mirrors the prior jaxrs-spec fix (OpenAPITools#23509). The template change is a no-op when no x-discriminator-value is present, so existing samples are unchanged.
Contributor
There was a problem hiding this comment.
1 issue found across 12 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache:17">
P2: `x-discriminator-value` is interpolated raw into a Java string literal via triple-brace Mustache, risking uncompilable generated code if the extension contains quotes, backslashes, or newlines.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| {{#isClassnameSanitized}} | ||
| {{^hasDiscriminatorWithNonEmptyMapping}} | ||
| @JsonTypeName("{{name}}") | ||
| @JsonTypeName("{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}{{^vendorExtensions.x-discriminator-value}}{{name}}{{/vendorExtensions.x-discriminator-value}}") |
Contributor
There was a problem hiding this comment.
P2: x-discriminator-value is interpolated raw into a Java string literal via triple-brace Mustache, risking uncompilable generated code if the extension contains quotes, backslashes, or newlines.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache, line 17:
<comment>`x-discriminator-value` is interpolated raw into a Java string literal via triple-brace Mustache, risking uncompilable generated code if the extension contains quotes, backslashes, or newlines.</comment>
<file context>
@@ -14,7 +14,7 @@
{{#isClassnameSanitized}}
{{^hasDiscriminatorWithNonEmptyMapping}}
-@JsonTypeName("{{name}}")
+@JsonTypeName("{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}{{^vendorExtensions.x-discriminator-value}}{{name}}{{/vendorExtensions.x-discriminator-value}}")
{{/hasDiscriminatorWithNonEmptyMapping}}
{{/isClassnameSanitized}}
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #23997
Problem
For the
javaandspringgenerators, generated child models of a discriminator can receive a Jackson@JsonTypeNameannotation using the schema/model name instead of the discriminator value declared in the OpenAPI contract:This breaks Jackson polymorphic serialization whenever the discriminator value differs from the schema name — most visibly when
modelNameSuffix/modelNamePrefixis used, or when an explicitx-discriminator-valueis declared on the child schema. The parent's@JsonSubTypesalready uses the correct value, so serialization and deserialization disagree.Fix
Render
@JsonTypeNamefromvendorExtensions.x-discriminator-valuewhen present, falling back to the schema/model name otherwise — exactly the approach suggested in the issue and already used for thejaxrs-specgenerator (#23509):@JsonTypeName("{{#vendorExtensions.x-discriminator-value}}{{{vendorExtensions.x-discriminator-value}}}{{/vendorExtensions.x-discriminator-value}}{{^vendorExtensions.x-discriminator-value}}{{name}}{{/vendorExtensions.x-discriminator-value}}")Applied to the base
Java/JavaSpringpojo.mustacheand the affected Java client libraries (restclient, resttemplate, webclient, feign, jersey2, jersey3, microprofile) so the behavior is consistent across the Java/Spring generators.The change is a no-op when no
x-discriminator-valueis present (the conditional renders exactly{{name}}as before), so no existing samples change.Test evidence
Added two focused tests reproducing the issue's spec (child schemas extend a non-discriminator base and carry an explicit
x-discriminator-value, generated with a model-name suffix):SpringCodegenTest#testDiscriminatorValueUsedInJsonTypeName_issue23997JavaClientCodegenTest#testDiscriminatorValueUsedInJsonTypeName_issue23997Both assert child models emit
@JsonTypeName("USER")/@JsonTypeName("COMPONENT")rather than the suffixed schema name. Both pass:Regenerating an existing discriminator-bearing sample (
bin/configs/java-restclient.yaml, petstore) produced zero diff, confirming the template change does not affect output whenx-discriminator-valueis absent.Verification done
gh pr listsearch), no linked PRs/branches on the issue.mustachetemplates + Java tests, not docsmaster(templates emitted@JsonTypeName("{{name}}")); reproduced via the added testsJsonTypeNameGeneration WithallOfandoneOfCombination to Match Key Instead of Schema Name #23509 deliberately did not cover java/springcc @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jimschubert (Java technical committee) and @cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @banlangzhi @sahilgarg17 (Spring technical committee)
Summary by cubic
Use the discriminator value for Jackson
@JsonTypeNamein Java and Spring generators to fix mismatched polymorphic serialization when it differs from the schema name. Falls back to the model name when nox-discriminator-valueis set; no behavior change otherwise. Fixes #23997.@JsonTypeNamefromvendorExtensions.x-discriminator-value, otherwise use{{name}}.Java/JavaSpringpojo.mustacheand Java client libs:restclient,resttemplate,webclient,feign,jersey2,jersey3,microprofile.@JsonTypeName("USER" | "COMPONENT")is emitted; existing samples show zero diff.Written for commit 91b6b9c. Summary will update on new commits.