From 3a80c205dd351a4372b5758f831009a53f3f0043 Mon Sep 17 00:00:00 2001 From: blaghed <17042053+blaghed@users.noreply.github.com> Date: Mon, 17 Nov 2025 15:17:35 +0100 Subject: [PATCH] Fix Jackson Serialization of additionalProperties on java and jaxrs-spec --- .../Java/additional_properties.mustache | 32 +++++++++++++++++++ .../src/main/resources/Java/model.mustache | 11 +++++++ .../src/main/resources/Java/pojo.mustache | 5 +++ .../spec/additional_properties.mustache | 32 +++++++++++++++++++ .../resources/JavaJaxRS/spec/pojo.mustache | 15 ++++++++- 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/main/resources/Java/additional_properties.mustache create mode 100644 modules/openapi-generator/src/main/resources/JavaJaxRS/spec/additional_properties.mustache diff --git a/modules/openapi-generator/src/main/resources/Java/additional_properties.mustache b/modules/openapi-generator/src/main/resources/Java/additional_properties.mustache new file mode 100644 index 000000000000..81af70c79ee0 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/additional_properties.mustache @@ -0,0 +1,32 @@ +{{#additionalPropertiesType}} + /** + * Set the additional (undeclared) property with the specified name and value. + * Creates the property if it does not already exist, otherwise replaces it. + * @param key the name of the property + * @param value the value of the property + * @return self reference + */ + @JsonAnySetter + public {{classname}} putAdditionalProperty(String key, {{{.}}} value) { + this.put(key, value); + return this; + } + + /** + * Return the additional (undeclared) properties. + * @return the additional (undeclared) properties + */ + @JsonAnyGetter + public Map getAdditionalProperties() { + return this; + } + + /** + * Return the additional (undeclared) property with the specified name. + * @param key the name of the property + * @return the additional (undeclared) property with the specified name + */ + public {{{.}}} getAdditionalProperty(String key) { + return this.get(key); + } +{{/additionalPropertiesType}} diff --git a/modules/openapi-generator/src/main/resources/Java/model.mustache b/modules/openapi-generator/src/main/resources/Java/model.mustache index 8c8d3c2f4150..9cc53e077bcc 100644 --- a/modules/openapi-generator/src/main/resources/Java/model.mustache +++ b/modules/openapi-generator/src/main/resources/Java/model.mustache @@ -18,6 +18,17 @@ import java.io.Serializable; {{#jackson}} import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonTypeName; +{{#models}} +{{#model}} +{{#additionalPropertiesType}} +import java.util.Map; +import java.util.HashMap; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonFormat; +{{/additionalPropertiesType}} +{{/model}} +{{/models}} {{#withXml}} import com.fasterxml.jackson.dataformat.xml.annotation.*; {{/withXml}} diff --git a/modules/openapi-generator/src/main/resources/Java/pojo.mustache b/modules/openapi-generator/src/main/resources/Java/pojo.mustache index f0fecc192b96..0781cff67725 100644 --- a/modules/openapi-generator/src/main/resources/Java/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/Java/pojo.mustache @@ -24,6 +24,9 @@ @JsonTypeName("{{name}}") {{/hasDiscriminatorWithNonEmptyMapping}} {{/isClassnameSanitized}} +{{#additionalPropertiesType}} +@JsonFormat(shape=JsonFormat.Shape.OBJECT) +{{/additionalPropertiesType}} {{/jackson}} {{>additionalModelTypeAnnotations}}{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}} {{#vendorExtensions.x-class-extra-annotation}} @@ -281,6 +284,8 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens {{/isReadOnly}} {{/vars}} +{{>additional_properties}} + {{#parent}} {{#readWriteVars}} {{#isOverridden}} diff --git a/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/additional_properties.mustache b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/additional_properties.mustache new file mode 100644 index 000000000000..7f59262156f1 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/additional_properties.mustache @@ -0,0 +1,32 @@ +{{#additionalProperties}} + /** + * Set the additional (undeclared) property with the specified name and value. + * Creates the property if it does not already exist, otherwise replaces it. + * @param key the name of the property + * @param value the value of the property + * @return self reference + */ + @JsonAnySetter + public {{classname}} putAdditionalProperty(String key, {{{datatypeWithEnum}}} value) { + this.put(key, value); + return this; + } + + /** + * Return the additional (undeclared) properties. + * @return the additional (undeclared) properties + */ + @JsonAnyGetter + public Map getAdditionalProperties() { + return this; + } + + /** + * Return the additional (undeclared) property with the specified name. + * @param key the name of the property + * @return the additional (undeclared) property with the specified name + */ + public {{{datatypeWithEnum}}} getAdditionalProperty(String key) { + return this.get(key); + } +{{/additionalProperties}} diff --git a/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache index 419f8b37ba03..632ba7bb9934 100644 --- a/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache @@ -10,6 +10,13 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.annotation.JsonTypeName; +{{#additionalProperties}} +import java.util.Map; +import java.util.HashMap; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonFormat; +{{/additionalProperties}} {{/jackson}} {{#withXml}} import {{javaxPackage}}.xml.bind.annotation.XmlElement; @@ -27,7 +34,12 @@ import {{javaxPackage}}.xml.bind.annotation.XmlEnumValue; {{#useSwaggerAnnotations}}{{#description}}@ApiModel(description = "{{{.}}}"){{/description}}{{/useSwaggerAnnotations}}{{#useSwaggerV3Annotations}} @Schema({{#title}}title="{{{.}}}", {{/title}}{{#description}}description="{{{.}}}"{{/description}}{{^description}}description=""{{/description}}){{/useSwaggerV3Annotations}}{{#useMicroProfileOpenAPIAnnotations}} @org.eclipse.microprofile.openapi.annotations.media.Schema({{#title}}title="{{{.}}}", {{/title}}{{#description}}description="{{{.}}}"{{/description}}{{^description}}description=""{{/description}}){{/useMicroProfileOpenAPIAnnotations}} -{{#jackson}}@JsonTypeName("{{name}}"){{/jackson}} +{{#jackson}} +@JsonTypeName("{{name}}") +{{#additionalProperties}} +@JsonFormat(shape=JsonFormat.Shape.OBJECT) +{{/additionalProperties}} +{{/jackson}} {{>generatedAnnotation}}{{>additionalModelTypeAnnotations}}{{>xmlPojoAnnotation}} {{#vendorExtensions.x-class-extra-annotation}} {{{vendorExtensions.x-class-extra-annotation}}} @@ -166,6 +178,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens } {{/isMap}} {{/vars}} +{{>additional_properties}} @Override public boolean equals(Object o) {