fixes(spring) add JsonProperty on setter#22978
Open
antechrestos wants to merge 1 commit intoOpenAPITools:masterfrom
Open
fixes(spring) add JsonProperty on setter#22978antechrestos wants to merge 1 commit intoOpenAPITools:masterfrom
antechrestos wants to merge 1 commit intoOpenAPITools:masterfrom
Conversation
* extract jacksonAnnotation partial template * extract xmkAccessorAnnotation partial template * apply jacksonAnnotation partial template on both getter and setter Fixes OpenAPITools#22757
81ab5b7 to
e397869
Compare
Contributor
Author
|
@cubic-dev-ai rerun a review |
Contributor
@antechrestos I have started the AI code review. It will take a few minutes to complete. |
Contributor
Author
|
@wing328 here's the fix I was waiting to push after the merge of #22854 As suggested in the issue #22757 , I applied the fix that already is present on java side; also, I used partial so as not to repeat myself. There are tons of modified files yet difference consist in thee diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/jackson_annotations.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/jackson_annotations.mustache
new file mode 100644
index 00000000000..0668f40785c
--- /dev/null
+++ b/modules/openapi-generator/src/main/resources/JavaSpring/jackson_annotations.mustache
@@ -0,0 +1,7 @@
+ @JsonProperty("{{baseName}}")
+{{#withXml}}
+ @JacksonXmlProperty(localName = "{{items.xmlName}}{{^items.xmlName}}{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}{{/items.xmlName}}"{{#isXmlAttribute}}, isAttribute = true{{/isXmlAttribute}}{{#xmlNamespace}}, namespace = "{{.}}"{{/xmlNamespace}})
+ {{#isContainer}}
+ @JacksonXmlElementWrapper({{#isXmlWrapped}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}", {{#xmlNamespace}}namespace = "{{.}}", {{/xmlNamespace}}{{/isXmlWrapped}}useWrapping = {{isXmlWrapped}})
+ {{/isContainer}}
+{{/withXml}}
diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache
index 82a0a6eb00c..8a93fb054f1 100644
--- a/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache
+++ b/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache
@@ -226,25 +226,10 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}
{{#swagger1AnnotationLibrary}}
@ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}")
{{/swagger1AnnotationLibrary}}
- {{#jackson}}
- @JsonProperty("{{baseName}}")
- {{#withXml}}
- @JacksonXmlProperty(localName = "{{items.xmlName}}{{^items.xmlName}}{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}{{/items.xmlName}}"{{#isXmlAttribute}}, isAttribute = true{{/isXmlAttribute}}{{#xmlNamespace}}, namespace = "{{.}}"{{/xmlNamespace}})
- {{#isContainer}}
- @JacksonXmlElementWrapper({{#isXmlWrapped}}localName = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}", {{#xmlNamespace}}namespace = "{{.}}", {{/xmlNamespace}}{{/isXmlWrapped}}useWrapping = {{isXmlWrapped}})
- {{/isContainer}}
- {{/withXml}}
- {{/jackson}}
- {{#withXml}}
- @Xml{{#isXmlAttribute}}Attribute{{/isXmlAttribute}}{{^isXmlAttribute}}Element{{/isXmlAttribute}}(name = "{{items.xmlName}}{{^items.xmlName}}{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}{{/items.xmlName}}"{{#xmlNamespace}}, namespace = "{{.}}"{{/xmlNamespace}})
- {{#isXmlWrapped}}
- @XmlElementWrapper(name = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}"{{#xmlNamespace}}, namespace = "{{.}}"{{/xmlNamespace}})
- {{/isXmlWrapped}}
- {{/withXml}}
{{#deprecated}}
@Deprecated
{{/deprecated}}
- public {{>nullableAnnotation}}{{>nullableDataTypeBeanValidation}} {{getter}}() {
+{{#jackson}}{{>jackson_annotations}}{{/jackson}}{{#withXml}}{{>xmlAccessorAnnotation}}{{/withXml}} public {{>nullableAnnotation}}{{>nullableDataTypeBeanValidation}} {{getter}}() {
return {{name}};
}
{{/lombok.Getter}}
@@ -261,7 +246,7 @@ public {{>sealed}}class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}
{{#deprecated}}
@Deprecated
{{/deprecated}}
- public void {{setter}}({{>nullableAnnotation}}{{>nullableDataType}} {{name}}) {
+{{#jackson}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{>jackson_annotations}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{/jackson}} public void {{setter}}({{>nullableAnnotation}}{{>nullableDataType}} {{name}}) {
this.{{name}} = {{name}};
}
{{/lombok.Setter}}
diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/xmlAccessorAnnotation.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/xmlAccessorAnnotation.mustache
new file mode 100644
index 00000000000..ee5195c0eb5
--- /dev/null
+++ b/modules/openapi-generator/src/main/resources/JavaSpring/xmlAccessorAnnotation.mustache
@@ -0,0 +1,4 @@
+ @Xml{{#isXmlAttribute}}Attribute{{/isXmlAttribute}}{{^isXmlAttribute}}Element{{/isXmlAttribute}}(name = "{{items.xmlName}}{{^items.xmlName}}{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}{{/items.xmlName}}"{{#xmlNamespace}}, namespace = "{{.}}"{{/xmlNamespace}})
+{{#isXmlWrapped}}
+ @XmlElementWrapper(name = "{{xmlName}}{{^xmlName}}{{baseName}}{{/xmlName}}"{{#xmlNamespace}}, namespace = "{{.}}"{{/xmlNamespace}})
+{{/isXmlWrapped}} |
Member
|
Thanks for the PR cc @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) |
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.
In order to fix #22757, I applied same logic used on java side by #9041
jacksonAnnotationpartial templatexmlAccessorAnnotationpartial templatejacksonAnnotationandxmlAccessorAnnotationpartial templates on both getter and setterSummary by cubic
Add @JsonProperty to generated setter methods in Java Spring models so Jackson maps JSON names correctly, especially for custom or non-standard field names. Regenerates Spring samples to reflect the change and resolve deserialization mismatches (fixes #22757).
Written for commit 81ab5b7. Summary will update on new commits.