Skip to content

Commit 311233d

Browse files
authored
[Java][MicroProfile] Support additionalProperties with Jackson (#21451)
Refs #20853 Refs #20947
1 parent b444de2 commit 311233d

File tree

227 files changed

+5685
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+5685
-15
lines changed

.github/workflows/samples-java-client-jdk11.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jobs:
7171
- samples/client/petstore/java/microprofile-rest-client-mutiny
7272
- samples/client/petstore/java/microprofile-rest-client-3.0
7373
- samples/client/petstore/java/microprofile-rest-client-3.0-jackson
74+
- samples/client/petstore/java/microprofile-rest-client-3.0-jackson-mutiny
7475
- samples/client/petstore/java/microprofile-rest-client-3.0-jackson-with-xml
7576
- samples/client/petstore/java/microprofile-rest-client-3.0-mutiny
7677
- samples/client/petstore/java/microprofile-rest-client-with-useSingleRequestParameter
@@ -133,4 +134,4 @@ jobs:
133134
- name: Build with Gradle
134135
working-directory: ${{ matrix.sample }}
135136
if: ${{ hashFiles('./gradlew') != '' }}
136-
run: ./gradlew build -x test
137+
run: ./gradlew build -x test
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: java
2+
outputDir: samples/client/petstore/java/microprofile-rest-client-3.0-jackson-mutiny
3+
library: microprofile
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/Java
6+
additionalProperties:
7+
serializationLibrary: jackson
8+
artifactId: microprofile-rest-client-3-jackson-mutiny
9+
configKey: petstore
10+
microprofileRestClientVersion: "3.0"
11+
microprofileMutiny: true
12+
hideGenerationTimestamp: true

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,15 @@ public void processOpts() {
709709
additionalProperties.put("jsonbPolymorphism", true);
710710
}
711711
}
712+
713+
if (getSerializationLibrary().equals(SERIALIZATION_LIBRARY_JACKSON)) {
714+
// Composed schemas can have the 'additionalProperties' keyword, as specified in JSON schema.
715+
// In principle, this should be enabled by default for all code generators. However due to limitations
716+
// in other code generators, support needs to be enabled on a case-by-case basis.
717+
// The flag below should be set for all Java libraries, but the templates need to be ported
718+
// one by one for each library.
719+
supportsAdditionalPropertiesWithComposedSchema = true;
720+
}
712721
} else if (libApache) {
713722
forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON);
714723
} else {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{{#additionalPropertiesType}}
2+
/**
3+
* A container for additional, undeclared properties.
4+
* This is a holder for any undeclared properties as specified with
5+
* the 'additionalProperties' keyword in the OAS document.
6+
*/
7+
private Map<String, {{{.}}}> additionalProperties;
8+
9+
/**
10+
* Set the additional (undeclared) property with the specified name and value.
11+
* If the property does not already exist, create it otherwise replace it.
12+
* @param key the name of the property
13+
* @param value the value of the property
14+
* @return self reference
15+
*/
16+
@com.fasterxml.jackson.annotation.JsonAnySetter
17+
public {{classname}} putAdditionalProperty(String key, {{{.}}} value) {
18+
if (this.additionalProperties == null) {
19+
this.additionalProperties = new HashMap<String, {{{.}}}>();
20+
}
21+
this.additionalProperties.put(key, value);
22+
return this;
23+
}
24+
25+
/**
26+
* Return the additional (undeclared) properties.
27+
* @return the additional (undeclared) properties
28+
*/
29+
@com.fasterxml.jackson.annotation.JsonAnyGetter
30+
public Map<String, {{{.}}}> getAdditionalProperties() {
31+
return additionalProperties;
32+
}
33+
34+
/**
35+
* Return the additional (undeclared) property with the specified name.
36+
* @param key the name of the property
37+
* @return the additional (undeclared) property with the specified name
38+
*/
39+
public {{{.}}} getAdditionalProperty(String key) {
40+
if (this.additionalProperties == null) {
41+
return null;
42+
}
43+
return this.additionalProperties.get(key);
44+
}
45+
{{/additionalPropertiesType}}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
77
{{/useReflectionEqualsHashCode}}
88
import java.util.Objects;
99
import java.util.Arrays;
10+
import java.util.Map;
11+
import java.util.HashMap;
1012
{{#imports}}import {{import}};
1113
{{/imports}}
1214
{{#serializableModel}}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}}{{#vendorExtensi
5757
private {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
5858
{{/isContainer}}
5959
{{/vars}}
60+
{{>additional_properties}}
6061
{{#vendorExtensions.x-has-readonly-properties}}{{^withXml}}
6162
public {{classname}}() {
6263
}

modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojoOverrides.mustache

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
}{{#hasVars}}
1313
{{classname}} {{classVarName}} = ({{classname}}) o;
1414
return {{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}equalsNullable(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{#isByteArray}}Arrays{{/isByteArray}}{{^isByteArray}}Objects{{/isByteArray}}.equals(this.{{name}}, {{classVarName}}.{{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}} &&
15-
{{/-last}}{{/vars}}{{#parent}} &&
15+
{{/-last}}{{/vars}}{{#additionalPropertiesType}} &&
16+
Objects.equals(this.additionalProperties, {{classVarName}}.additionalProperties){{/additionalPropertiesType}}{{#parent}} &&
1617
super.equals(o){{/parent}};{{/hasVars}}{{^hasVars}}
1718
return {{#parent}}super.equals(o){{/parent}}{{^parent}}true{{/parent}};{{/hasVars}}
1819
{{/useReflectionEqualsHashCode}}
@@ -28,7 +29,7 @@
2829
return HashCodeBuilder.reflectionHashCode(this);
2930
{{/useReflectionEqualsHashCode}}
3031
{{^useReflectionEqualsHashCode}}
31-
return Objects.hash({{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}hashCodeNullable({{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}});
32+
return Objects.hash({{#vars}}{{#vendorExtensions.x-is-jackson-optional-nullable}}hashCodeNullable({{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{^isByteArray}}{{name}}{{/isByteArray}}{{#isByteArray}}Arrays.hashCode({{name}}){{/isByteArray}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{^-last}}, {{/-last}}{{/vars}}{{#parent}}{{#hasVars}}, {{/hasVars}}super.hashCode(){{/parent}}{{#additionalPropertiesType}}, additionalProperties{{/additionalPropertiesType}});
3233
{{/useReflectionEqualsHashCode}}
3334
}{{#vendorExtensions.x-jackson-optional-nullable-helpers}}
3435

@@ -47,8 +48,13 @@
4748
StringBuilder sb = new StringBuilder();
4849
sb.append("class {{classname}} {\n");
4950
{{#parent}}sb.append(" ").append(toIndentedString(super.toString())).append("\n");{{/parent}}
50-
{{#vars}}sb.append(" {{name}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{name}}){{/isPassword}}).append("\n");
51-
{{/vars}}sb.append("}");
51+
{{#vars}}
52+
sb.append(" {{name}}: ").append({{#isPassword}}"*"{{/isPassword}}{{^isPassword}}toIndentedString({{name}}){{/isPassword}}).append("\n");
53+
{{/vars}}
54+
{{#additionalPropertiesType}}
55+
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
56+
{{/additionalPropertiesType}}
57+
sb.append("}");
5258
return sb.toString();
5359
}
5460

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
README.md
2+
docs/Category.md
3+
docs/ModelApiResponse.md
4+
docs/Order.md
5+
docs/Pet.md
6+
docs/PetApi.md
7+
docs/StoreApi.md
8+
docs/Tag.md
9+
docs/User.md
10+
docs/UserApi.md
11+
pom.xml
12+
src/main/java/org/openapitools/client/RFC3339DateFormat.java
13+
src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java
14+
src/main/java/org/openapitools/client/RFC3339JavaTimeModule.java
15+
src/main/java/org/openapitools/client/api/ApiException.java
16+
src/main/java/org/openapitools/client/api/ApiExceptionMapper.java
17+
src/main/java/org/openapitools/client/api/PetApi.java
18+
src/main/java/org/openapitools/client/api/StoreApi.java
19+
src/main/java/org/openapitools/client/api/UserApi.java
20+
src/main/java/org/openapitools/client/model/Category.java
21+
src/main/java/org/openapitools/client/model/ModelApiResponse.java
22+
src/main/java/org/openapitools/client/model/Order.java
23+
src/main/java/org/openapitools/client/model/Pet.java
24+
src/main/java/org/openapitools/client/model/Tag.java
25+
src/main/java/org/openapitools/client/model/User.java
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.15.0-SNAPSHOT

0 commit comments

Comments
 (0)