-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat(kotlin-spring, java-spring): add x-jackson-default-impl / typeInfoDefaultImpls support #23955
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
Draft
Picazsoo
wants to merge
1
commit into
OpenAPITools:master
Choose a base branch
from
Picazsoo:feature/use-deduction-default
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−5
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -123,6 +123,7 @@ public class SpringCodegen extends AbstractJavaCodegen | |||||||
| public static final String GENERATE_PAGEABLE_CONSTRAINT_VALIDATION = "generatePageableConstraintValidation"; | ||||||||
| public static final String SUBSTITUTE_GENERIC_PAGED_MODEL = "substituteGenericPagedModel"; | ||||||||
| public static final String CLIENT_REGISTRATION_ID = "clientRegistrationId"; | ||||||||
| public static final String TYPE_INFO_DEFAULT_IMPLS = "typeInfoDefaultImpls"; | ||||||||
|
|
||||||||
| @Getter | ||||||||
| public enum RequestMappingMode { | ||||||||
|
|
@@ -201,6 +202,7 @@ public enum RequestMappingMode { | |||||||
| protected String clientRegistrationId = null; | ||||||||
| @Setter protected boolean useEnumValueInterface = false; | ||||||||
| private String valuedEnumClassName = "ValuedEnum"; | ||||||||
| private Map<String, String> typeInfoDefaultImpls = new HashMap<>(); | ||||||||
|
|
||||||||
| // Map from schema name to detected paged-model info (populated when substituteGenericPagedModel=true) | ||||||||
| private Map<String, PagedModelScanUtils.DetectedPagedModel> pagedModelRegistry = new HashMap<>(); | ||||||||
|
|
@@ -340,6 +342,7 @@ public SpringCodegen() { | |||||||
| optionalAcceptNullable)); | ||||||||
|
|
||||||||
| cliOptions.add(CliOption.newBoolean(USE_DEDUCTION_FOR_ONE_OF_INTERFACES, USE_DEDUCTION_FOR_ONE_OF_INTERFACES_DESC, useDeductionForOneOfInterfaces)); | ||||||||
| cliOptions.add(new CliOption(TYPE_INFO_DEFAULT_IMPLS, "Map of schema name to default Jackson deserialization class for @JsonTypeInfo(defaultImpl=...). Applies to both deduction-based and discriminator-based oneOf interfaces. Overrides x-jackson-default-impl when both are set for the same schema. Example: yaml `typeInfoDefaultImpls: {PostRegistrationRequest: PostRegistrationBasicRequest}`")); | ||||||||
| cliOptions.add(CliOption.newString(SPRING_API_VERSION, "Value for 'version' attribute in @RequestMapping (for Spring 7 and above).")); | ||||||||
| cliOptions.add(CliOption.newString(USE_HTTP_SERVICE_PROXY_FACTORY_INTERFACES_CONFIGURATOR, | ||||||||
| "Generate HttpInterfacesAbstractConfigurator based on an HttpServiceProxyFactory instance (as opposed to a WebClient instance, when disabled) for generating Spring HTTP interfaces.") | ||||||||
|
|
@@ -572,6 +575,10 @@ public void processOpts() { | |||||||
| convertPropertyToBooleanAndWriteBack(OPTIONAL_ACCEPT_NULLABLE, this::setOptionalAcceptNullable); | ||||||||
| convertPropertyToBooleanAndWriteBack(USE_SPRING_BUILT_IN_VALIDATION, this::setUseSpringBuiltInValidation); | ||||||||
| convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_DEDUCTION_FOR_ONE_OF_INTERFACES, this::setUseDeductionForOneOfInterfaces); | ||||||||
|
|
||||||||
| if (additionalProperties.containsKey(TYPE_INFO_DEFAULT_IMPLS)) { | ||||||||
| typeInfoDefaultImpls.putAll(getPropertyAsStringMap(TYPE_INFO_DEFAULT_IMPLS)); | ||||||||
| } | ||||||||
| convertPropertyToStringAndWriteBack(CLIENT_REGISTRATION_ID, this::setClientRegistrationId); | ||||||||
|
|
||||||||
| additionalProperties.put("springHttpStatus", new SpringHttpStatusLambda()); | ||||||||
|
|
@@ -1421,6 +1428,37 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) | |||||||
| objs = super.postProcessAllModels(objs); | ||||||||
|
|
||||||||
| Map<String, CodegenModel> allModels = getAllModels(objs); | ||||||||
|
|
||||||||
| // Resolve x-jackson-default-impl and typeInfoDefaultImpls into x-jackson-resolved-default-impl | ||||||||
| // on each model. This drives defaultImpl = ... in @JsonTypeInfo for both deduction-based | ||||||||
| // and discriminator-based oneOf interfaces. | ||||||||
| for (CodegenModel cm : allModels.values()) { | ||||||||
| String schemaAnnotation = (String) cm.vendorExtensions.get("x-jackson-default-impl"); | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Unsafe cast of x-jackson-default-impl vendor extension can crash generation with ClassCastException Prompt for AI agents
Suggested change
|
||||||||
| String configValue = typeInfoDefaultImpls.get(cm.schemaName); | ||||||||
| String rawValue; | ||||||||
| if (configValue != null && !configValue.isBlank()) { | ||||||||
| if (schemaAnnotation != null && !schemaAnnotation.isBlank()) { | ||||||||
| LOGGER.warn("typeInfoDefaultImpls overrides x-jackson-default-impl on schema '{}': '{}' → '{}'", | ||||||||
| cm.schemaName, schemaAnnotation, configValue); | ||||||||
| } | ||||||||
| rawValue = configValue; | ||||||||
| } else if (schemaAnnotation != null && !schemaAnnotation.isBlank()) { | ||||||||
| rawValue = schemaAnnotation; | ||||||||
| } else { | ||||||||
| continue; | ||||||||
| } | ||||||||
| String resolved = toModelName(rawValue); | ||||||||
| if (resolved != null && !resolved.isBlank()) { | ||||||||
| cm.vendorExtensions.put("x-jackson-resolved-default-impl", resolved); | ||||||||
| // When a discriminator is present, the typeInfoAnnotation partial is rendered | ||||||||
| // inside {{#discriminator}}, so JMustache resolves 'vendorExtensions' against | ||||||||
| // CodegenDiscriminator (not CodegenModel). Store there too. | ||||||||
| if (cm.discriminator != null) { | ||||||||
| cm.discriminator.getVendorExtensions().put("x-jackson-resolved-default-impl", resolved); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // conditionally force the generation of no args constructor | ||||||||
| for (CodegenModel cm : allModels.values()) { | ||||||||
| boolean hasLombokNoArgsConstructor = lombokAnnotations != null && lombokAnnotations.containsKey("NoArgsConstructor"); | ||||||||
|
|
||||||||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to CodegenConstants ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Picazsoo Could you also add the feature to the java generators?
See
src/main/resources/Java/deductionAnnotation.mustacheand the differenttypeInfoAnnotation.mustacheunder java/librariesUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will take a look at it. And this is early implementation without any polish. I marked temporarily as "ready for review" just to gather early feedback from Cubic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But thanks for the review! I will properly tag you once I think it is ready for wasting human review time (-: