Skip to content

Commit 88204b2

Browse files
authored
[Java] Sync jersey2 jersey3 template (#18281)
* sync jersey2 and 3 templates * add deprecated * update samples
1 parent 2934f5a commit 88204b2

27 files changed

Lines changed: 268 additions & 390 deletions

File tree

modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
990990
File file = (File) param.getValue();
991991
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
992992
.fileName(file.getName()).size(file.length()).build();
993-
993+
994994
// Attempt to probe the content type for the file so that the form part is more correctly
995995
// and precisely identified, but fall back to application/octet-stream if that fails.
996996
MediaType type;
@@ -999,7 +999,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
999999
} catch (IOException | IllegalArgumentException e) {
10001000
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
10011001
}
1002-
1002+
10031003
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
10041004
} else {
10051005
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,22 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
8080
{{/vendorExtensions.x-field-extra-annotation}}
8181
{{#vendorExtensions.x-is-jackson-optional-nullable}}
8282
{{#isContainer}}
83+
{{#deprecated}}
84+
@Deprecated
85+
{{/deprecated}}
8386
private JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>undefined();
8487
{{/isContainer}}
8588
{{^isContainer}}
89+
{{#deprecated}}
90+
@Deprecated
91+
{{/deprecated}}
8692
private JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>{{#defaultValue}}of({{{.}}}){{/defaultValue}}{{^defaultValue}}undefined(){{/defaultValue}};
8793
{{/isContainer}}
8894
{{/vendorExtensions.x-is-jackson-optional-nullable}}
8995
{{^vendorExtensions.x-is-jackson-optional-nullable}}
96+
{{#deprecated}}
97+
@Deprecated
98+
{{/deprecated}}
9099
private {{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
91100
{{/vendorExtensions.x-is-jackson-optional-nullable}}
92101

@@ -116,6 +125,9 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
116125
));
117126

118127
{{/vendorExtensions.x-enum-as-string}}
128+
{{#deprecated}}
129+
@Deprecated
130+
{{/deprecated}}
119131
public {{classname}} {{name}}({{{datatypeWithEnum}}} {{name}}) {
120132
{{#vendorExtensions.x-enum-as-string}}
121133
if (!{{{nameInSnakeCase}}}_VALUES.contains({{name}})) {
@@ -256,6 +268,9 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
256268
{{/vendorExtensions.x-is-jackson-optional-nullable}}
257269
258270
{{^isReadOnly}}
271+
{{#deprecated}}
272+
@Deprecated
273+
{{/deprecated}}
259274
{{#vendorExtensions.x-setter-extra-annotation}} {{{vendorExtensions.x-setter-extra-annotation}}}
260275
{{/vendorExtensions.x-setter-extra-annotation}}{{#jackson}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{> jackson_annotations}}{{/vendorExtensions.x-is-jackson-optional-nullable}}{{/jackson}} public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
261276
{{#vendorExtensions.x-enum-as-string}}

modules/openapi-generator/src/main/resources/Java/libraries/jersey3/ApiClient.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
119119
protected Integer serverIndex = 0;
120120
protected Map<String, String> serverVariables = null;
121121
{{^hasOperationServers}}
122-
protected Map<String, List<ServerConfiguration>> operationServers = new LinkedHashMap<>();
122+
protected Map<String, List<ServerConfiguration>> operationServers = new HashMap<>();
123123
{{/hasOperationServers}}
124124
{{#hasOperationServers}}
125125
protected Map<String, List<ServerConfiguration>> operationServers;
126126

127127
{
128-
Map<String, List<ServerConfiguration>> operationServers = new LinkedHashMap<>();
128+
Map<String, List<ServerConfiguration>> operationServers = new HashMap<>();
129129
{{#apiInfo}}
130130
{{#apis}}
131131
{{#operations}}
@@ -992,14 +992,14 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
992992
.fileName(file.getName()).size(file.length()).build();
993993
994994
// Attempt to probe the content type for the file so that the form part is more correctly
995-
// and precisely identified, but fall back to application/octet-stream if that fails.
995+
// and precisely identified, but fall back to application/octet-stream if that fails.
996996
MediaType type;
997997
try {
998998
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
999999
} catch (IOException | IllegalArgumentException e) {
10001000
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
10011001
}
1002-
1002+
10031003
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
10041004
} else {
10051005
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();

modules/openapi-generator/src/main/resources/Java/libraries/jersey3/oneof_model.mustache

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
6060
{{#useOneOfDiscriminatorLookup}}
6161
{{#discriminator}}
6262
{{classname}} new{{classname}} = new {{classname}}();
63-
Map<String,Object> result2 = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<Map<String, Object>>() {});
63+
Map<String, Object> result2 = tree.traverse(jp.getCodec()).readValueAs(new TypeReference<Map<String, Object>>() {});
6464
String discriminatorValue = (String)result2.get("{{{propertyBaseName}}}");
6565
switch (discriminatorValue) {
6666
{{#mappedModels}}
@@ -78,37 +78,74 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
7878
boolean typeCoercion = ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS);
7979
int match = 0;
8080
JsonToken token = tree.traverse(jp.getCodec()).nextToken();
81+
{{#composedSchemas}}
8182
{{#oneOf}}
82-
// deserialize {{{.}}}
83+
// deserialize {{{dataType}}}{{#isNullable}} (nullable){{/isNullable}}
8384
try {
85+
{{^isArray}}
8486
boolean attemptParsing = true;
85-
// ensure that we respect type coercion as set on the client ObjectMapper
86-
if ({{{.}}}.class.equals(Integer.class) || {{{.}}}.class.equals(Long.class) || {{{.}}}.class.equals(Float.class) || {{{.}}}.class.equals(Double.class) || {{{.}}}.class.equals(Boolean.class) || {{{.}}}.class.equals(String.class)) {
87-
attemptParsing = typeCoercion;
88-
if (!attemptParsing) {
89-
attemptParsing |= (({{{.}}}.class.equals(Integer.class) || {{{.}}}.class.equals(Long.class)) && token == JsonToken.VALUE_NUMBER_INT);
90-
attemptParsing |= (({{{.}}}.class.equals(Float.class) || {{{.}}}.class.equals(Double.class)) && token == JsonToken.VALUE_NUMBER_FLOAT);
91-
attemptParsing |= ({{{.}}}.class.equals(Boolean.class) && (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE));
92-
attemptParsing |= ({{{.}}}.class.equals(String.class) && token == JsonToken.VALUE_STRING);
93-
{{#isNullable}}
94-
attemptParsing |= (token == JsonToken.VALUE_NULL);
95-
{{/isNullable}}
96-
}
87+
{{#isPrimitiveType}}
88+
attemptParsing = typeCoercion; //respect type coercion setting
89+
if (!attemptParsing) {
90+
{{#isString}}
91+
attemptParsing |= (token == JsonToken.VALUE_STRING);
92+
{{/isString}}
93+
{{#isInteger}}
94+
attemptParsing |= (token == JsonToken.VALUE_NUMBER_INT);
95+
{{/isInteger}}
96+
{{#isLong}}
97+
attemptParsing |= (token == JsonToken.VALUE_NUMBER_INT);
98+
{{/isLong}}
99+
{{#isShort}}
100+
attemptParsing |= (token == JsonToken.VALUE_NUMBER_INT);
101+
{{/isShort}}
102+
{{#isFloat}}
103+
attemptParsing |= (token == JsonToken.VALUE_NUMBER_FLOAT);
104+
{{/isFloat}}
105+
{{#isDouble}}
106+
attemptParsing |= (token == JsonToken.VALUE_NUMBER_FLOAT);
107+
{{/isDouble}}
108+
{{#isNumber}}
109+
attemptParsing |= (token == JsonToken.VALUE_NUMBER_FLOAT);
110+
{{/isNumber}}
111+
{{#isDecimal}}
112+
attemptParsing |= (token == JsonToken.VALUE_NUMBER_FLOAT);
113+
{{/isDecimal}}
114+
{{#isBoolean}}
115+
attemptParsing |= (token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE));
116+
{{/isBoolean}}
117+
{{#isNullable}}
118+
attemptParsing |= (token == JsonToken.VALUE_NULL);
119+
{{/isNullable}}
97120
}
121+
{{/isPrimitiveType}}
98122
if (attemptParsing) {
99-
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{.}}}.class);
123+
deserialized = tree.traverse(jp.getCodec()).readValueAs({{{dataType}}}.class);
100124
// TODO: there is no validation against JSON schema constraints
101125
// (min, max, enum, pattern...), this does not perform a strict JSON
102126
// validation, which means the 'match' count may be higher than it should be.
103127
match++;
104-
log.log(Level.FINER, "Input data matches schema '{{{.}}}'");
128+
log.log(Level.FINER, "Input data matches schema '{{{dataType}}}'");
105129
}
130+
{{/isArray}}
131+
{{#isArray}}
132+
if (token == JsonToken.START_ARRAY) {
133+
final TypeReference<{{{dataType}}}> ref = new TypeReference<{{{dataType}}}>(){};
134+
deserialized = tree.traverse(jp.getCodec()).readValueAs(ref);
135+
// TODO: there is no validation against JSON schema constraints
136+
// (min, max, enum, pattern...), this does not perform a strict JSON
137+
// validation, which means the 'match' count may be higher than it should be.
138+
match++;
139+
log.log(Level.FINER, "Input data matches schema '{{{dataType}}}'");
140+
}
141+
{{/isArray}}
106142
} catch (Exception e) {
107143
// deserialization failed, continue
108-
log.log(Level.FINER, "Input data does not match schema '{{{.}}}'", e);
144+
log.log(Level.FINER, "Input data does not match schema '{{{dataType}}}'", e);
109145
}
110146

111147
{{/oneOf}}
148+
{{/composedSchemas}}
112149
if (match == 1) {
113150
{{classname}} ret = new {{classname}}();
114151
ret.setActualInstance(deserialized);
@@ -152,13 +189,17 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
152189
return Objects.hash(getActualInstance(), isNullable(), getSchemaType(), additionalProperties);
153190
}
154191
{{/additionalPropertiesType}}
192+
{{#composedSchemas}}
155193
{{#oneOf}}
156-
public {{classname}}({{{.}}} o) {
194+
{{^vendorExtensions.x-duplicated-data-type}}
195+
public {{classname}}({{{baseType}}} o) {
157196
super("oneOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
158197
setActualInstance(o);
159198
}
199+
{{/vendorExtensions.x-duplicated-data-type}}
160200

161201
{{/oneOf}}
202+
{{/composedSchemas}}
162203
static {
163204
{{#oneOf}}
164205
schemas.put("{{{.}}}", new GenericType<{{{.}}}>() {
@@ -198,13 +239,17 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
198239
}
199240

200241
{{/isNullable}}
242+
{{#composedSchemas}}
201243
{{#oneOf}}
202-
if (JSON.isInstanceOf({{{.}}}.class, instance, new HashSet<>())) {
244+
{{^vendorExtensions.x-duplicated-data-type}}
245+
if (JSON.isInstanceOf({{{baseType}}}.class, instance, new HashSet<>())) {
203246
super.setActualInstance(instance);
204247
return;
205248
}
206249

250+
{{/vendorExtensions.x-duplicated-data-type}}
207251
{{/oneOf}}
252+
{{/composedSchemas}}
208253
throw new RuntimeException("Invalid instance type. Must be {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}");
209254
}
210255

@@ -219,17 +264,26 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
219264
return super.getActualInstance();
220265
}
221266

267+
{{#composedSchemas}}
222268
{{#oneOf}}
223269
/**
224-
* Get the actual instance of `{{{.}}}`. If the actual instance is not `{{{.}}}`,
270+
* Get the actual instance of `{{{dataType}}}`. If the actual instance is not `{{{dataType}}}`,
225271
* the ClassCastException will be thrown.
226272
*
227-
* @return The actual instance of `{{{.}}}`
228-
* @throws ClassCastException if the instance is not `{{{.}}}`
273+
* @return The actual instance of `{{{dataType}}}`
274+
* @throws ClassCastException if the instance is not `{{{dataType}}}`
229275
*/
230-
public {{{.}}} get{{{.}}}() throws ClassCastException {
231-
return ({{{.}}})super.getActualInstance();
276+
{{^isArray}}
277+
public {{{dataType}}} get{{{dataType}}}() throws ClassCastException {
278+
return ({{{dataType}}})super.getActualInstance();
232279
}
280+
{{/isArray}}
281+
{{#isArray}}
282+
public {{{dataType}}} get{{#sanitizeGeneric}}{{{dataType}}}{{/sanitizeGeneric}}() throws ClassCastException {
283+
return ({{{dataType}}})super.getActualInstance();
284+
}
285+
{{/isArray}}
233286

234287
{{/oneOf}}
288+
{{/composedSchemas}}
235289
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
113113
) {
114114
this();
115115
{{#readOnlyVars}}
116-
this.{{name}} = {{name}};
116+
this.{{name}} = {{#vendorExtensions.x-is-jackson-optional-nullable}}{{name}} == null ? JsonNullable.<{{{datatypeWithEnum}}}>undefined() : JsonNullable.of({{name}}){{/vendorExtensions.x-is-jackson-optional-nullable}}{{^vendorExtensions.x-is-jackson-optional-nullable}}{{name}}{{/vendorExtensions.x-is-jackson-optional-nullable}};
117117
{{/readOnlyVars}}
118118
}{{/jackson}}{{/withXml}}{{/vendorExtensions.x-has-readonly-properties}}
119119
{{#vars}}

samples/client/others/java/jersey2-oneOf-Mixed/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
758758
File file = (File) param.getValue();
759759
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
760760
.fileName(file.getName()).size(file.length()).build();
761-
761+
762762
// Attempt to probe the content type for the file so that the form part is more correctly
763763
// and precisely identified, but fall back to application/octet-stream if that fails.
764764
MediaType type;
@@ -767,7 +767,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
767767
} catch (IOException | IllegalArgumentException e) {
768768
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
769769
}
770-
770+
771771
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
772772
} else {
773773
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();

samples/client/others/java/jersey2-oneOf-duplicates/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
758758
File file = (File) param.getValue();
759759
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
760760
.fileName(file.getName()).size(file.length()).build();
761-
761+
762762
// Attempt to probe the content type for the file so that the form part is more correctly
763763
// and precisely identified, but fall back to application/octet-stream if that fails.
764764
MediaType type;
@@ -767,7 +767,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
767767
} catch (IOException | IllegalArgumentException e) {
768768
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
769769
}
770-
770+
771771
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
772772
} else {
773773
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();

samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
899899
File file = (File) param.getValue();
900900
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
901901
.fileName(file.getName()).size(file.length()).build();
902-
902+
903903
// Attempt to probe the content type for the file so that the form part is more correctly
904904
// and precisely identified, but fall back to application/octet-stream if that fails.
905905
MediaType type;
@@ -908,7 +908,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
908908
} catch (IOException | IllegalArgumentException e) {
909909
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
910910
}
911-
911+
912912
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
913913
} else {
914914
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();

samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
899899
File file = (File) param.getValue();
900900
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey())
901901
.fileName(file.getName()).size(file.length()).build();
902-
902+
903903
// Attempt to probe the content type for the file so that the form part is more correctly
904904
// and precisely identified, but fall back to application/octet-stream if that fails.
905905
MediaType type;
@@ -908,7 +908,7 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
908908
} catch (IOException | IllegalArgumentException e) {
909909
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
910910
}
911-
911+
912912
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
913913
} else {
914914
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();

samples/client/petstore/java/jersey3/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public class ApiClient extends JavaTimeFormatter {
132132
protected Map<String, List<ServerConfiguration>> operationServers;
133133

134134
{
135-
Map<String, List<ServerConfiguration>> operationServers = new LinkedHashMap<>();
135+
Map<String, List<ServerConfiguration>> operationServers = new HashMap<>();
136136
operationServers.put("PetApi.addPet", new ArrayList<>(Arrays.asList(
137137
new ServerConfiguration(
138138
"http://petstore.swagger.io/v2",
@@ -983,14 +983,14 @@ public Entity<?> serialize(Object obj, Map<String, Object> formParams, String co
983983
.fileName(file.getName()).size(file.length()).build();
984984

985985
// Attempt to probe the content type for the file so that the form part is more correctly
986-
// and precisely identified, but fall back to application/octet-stream if that fails.
986+
// and precisely identified, but fall back to application/octet-stream if that fails.
987987
MediaType type;
988988
try {
989989
type = MediaType.valueOf(Files.probeContentType(file.toPath()));
990990
} catch (IOException | IllegalArgumentException e) {
991991
type = MediaType.APPLICATION_OCTET_STREAM_TYPE;
992992
}
993-
993+
994994
multiPart.bodyPart(new FormDataBodyPart(contentDisp, file, type));
995995
} else {
996996
FormDataContentDisposition contentDisp = FormDataContentDisposition.name(param.getKey()).build();

0 commit comments

Comments
 (0)