Skip to content

Commit 3a80c20

Browse files
committed
Fix Jackson Serialization of additionalProperties on java and jaxrs-spec
1 parent 2c7efda commit 3a80c20

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{#additionalPropertiesType}}
2+
/**
3+
* Set the additional (undeclared) property with the specified name and value.
4+
* Creates the property if it does not already exist, otherwise replaces it.
5+
* @param key the name of the property
6+
* @param value the value of the property
7+
* @return self reference
8+
*/
9+
@JsonAnySetter
10+
public {{classname}} putAdditionalProperty(String key, {{{.}}} value) {
11+
this.put(key, value);
12+
return this;
13+
}
14+
15+
/**
16+
* Return the additional (undeclared) properties.
17+
* @return the additional (undeclared) properties
18+
*/
19+
@JsonAnyGetter
20+
public Map<String, {{{.}}}> getAdditionalProperties() {
21+
return this;
22+
}
23+
24+
/**
25+
* Return the additional (undeclared) property with the specified name.
26+
* @param key the name of the property
27+
* @return the additional (undeclared) property with the specified name
28+
*/
29+
public {{{.}}} getAdditionalProperty(String key) {
30+
return this.get(key);
31+
}
32+
{{/additionalPropertiesType}}

modules/openapi-generator/src/main/resources/Java/model.mustache

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ import java.io.Serializable;
1818
{{#jackson}}
1919
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
2020
import com.fasterxml.jackson.annotation.JsonTypeName;
21+
{{#models}}
22+
{{#model}}
23+
{{#additionalPropertiesType}}
24+
import java.util.Map;
25+
import java.util.HashMap;
26+
import com.fasterxml.jackson.annotation.JsonAnySetter;
27+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
28+
import com.fasterxml.jackson.annotation.JsonFormat;
29+
{{/additionalPropertiesType}}
30+
{{/model}}
31+
{{/models}}
2132
{{#withXml}}
2233
import com.fasterxml.jackson.dataformat.xml.annotation.*;
2334
{{/withXml}}

modules/openapi-generator/src/main/resources/Java/pojo.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
@JsonTypeName("{{name}}")
2525
{{/hasDiscriminatorWithNonEmptyMapping}}
2626
{{/isClassnameSanitized}}
27+
{{#additionalPropertiesType}}
28+
@JsonFormat(shape=JsonFormat.Shape.OBJECT)
29+
{{/additionalPropertiesType}}
2730
{{/jackson}}
2831
{{>additionalModelTypeAnnotations}}{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
2932
{{#vendorExtensions.x-class-extra-annotation}}
@@ -281,6 +284,8 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
281284
{{/isReadOnly}}
282285
283286
{{/vars}}
287+
{{>additional_properties}}
288+
284289
{{#parent}}
285290
{{#readWriteVars}}
286291
{{#isOverridden}}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{#additionalProperties}}
2+
/**
3+
* Set the additional (undeclared) property with the specified name and value.
4+
* Creates the property if it does not already exist, otherwise replaces it.
5+
* @param key the name of the property
6+
* @param value the value of the property
7+
* @return self reference
8+
*/
9+
@JsonAnySetter
10+
public {{classname}} putAdditionalProperty(String key, {{{datatypeWithEnum}}} value) {
11+
this.put(key, value);
12+
return this;
13+
}
14+
15+
/**
16+
* Return the additional (undeclared) properties.
17+
* @return the additional (undeclared) properties
18+
*/
19+
@JsonAnyGetter
20+
public Map<String, {{{datatypeWithEnum}}}> getAdditionalProperties() {
21+
return this;
22+
}
23+
24+
/**
25+
* Return the additional (undeclared) property with the specified name.
26+
* @param key the name of the property
27+
* @return the additional (undeclared) property with the specified name
28+
*/
29+
public {{{datatypeWithEnum}}} getAdditionalProperty(String key) {
30+
return this.get(key);
31+
}
32+
{{/additionalProperties}}

modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import com.fasterxml.jackson.annotation.JsonProperty;
1010
import com.fasterxml.jackson.annotation.JsonCreator;
1111
import com.fasterxml.jackson.annotation.JsonValue;
1212
import com.fasterxml.jackson.annotation.JsonTypeName;
13+
{{#additionalProperties}}
14+
import java.util.Map;
15+
import java.util.HashMap;
16+
import com.fasterxml.jackson.annotation.JsonAnySetter;
17+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
18+
import com.fasterxml.jackson.annotation.JsonFormat;
19+
{{/additionalProperties}}
1320
{{/jackson}}
1421
{{#withXml}}
1522
import {{javaxPackage}}.xml.bind.annotation.XmlElement;
@@ -27,7 +34,12 @@ import {{javaxPackage}}.xml.bind.annotation.XmlEnumValue;
2734
{{#useSwaggerAnnotations}}{{#description}}@ApiModel(description = "{{{.}}}"){{/description}}{{/useSwaggerAnnotations}}{{#useSwaggerV3Annotations}}
2835
@Schema({{#title}}title="{{{.}}}", {{/title}}{{#description}}description="{{{.}}}"{{/description}}{{^description}}description=""{{/description}}){{/useSwaggerV3Annotations}}{{#useMicroProfileOpenAPIAnnotations}}
2936
@org.eclipse.microprofile.openapi.annotations.media.Schema({{#title}}title="{{{.}}}", {{/title}}{{#description}}description="{{{.}}}"{{/description}}{{^description}}description=""{{/description}}){{/useMicroProfileOpenAPIAnnotations}}
30-
{{#jackson}}@JsonTypeName("{{name}}"){{/jackson}}
37+
{{#jackson}}
38+
@JsonTypeName("{{name}}")
39+
{{#additionalProperties}}
40+
@JsonFormat(shape=JsonFormat.Shape.OBJECT)
41+
{{/additionalProperties}}
42+
{{/jackson}}
3143
{{>generatedAnnotation}}{{>additionalModelTypeAnnotations}}{{>xmlPojoAnnotation}}
3244
{{#vendorExtensions.x-class-extra-annotation}}
3345
{{{vendorExtensions.x-class-extra-annotation}}}
@@ -166,6 +178,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
166178
}
167179
{{/isMap}}
168180
{{/vars}}
181+
{{>additional_properties}}
169182

170183
@Override
171184
public boolean equals(Object o) {

0 commit comments

Comments
 (0)