Skip to content

Commit 808d106

Browse files
authored
[jaxrs] Support jackson option (true by default) (#22169)
* allow configuring jackson, remove jackson import if disabled * do not generate root resource or application file if generating interface * change template files to remove jackson if disabled * updated samples and docs as required by PR checklist
1 parent 9d810e0 commit 808d106

File tree

328 files changed

+108
-392
lines changed

Some content is hidden

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

328 files changed

+108
-392
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public void processOpts() {
119119
convertPropertyToStringAndWriteBack(CodegenConstants.IMPL_FOLDER, this::setImplFolder);
120120
convertPropertyToBooleanAndWriteBack(USE_BEANVALIDATION, this::setUseBeanValidation);
121121
convertPropertyToBooleanAndWriteBack(USE_TAGS, this::setUseTags);
122+
convertPropertyToBooleanAndWriteBack(JACKSON, this::setJackson);
122123
}
123124

124125
@Override

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,22 @@ public void processOpts() {
187187
supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen
188188
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")
189189
.doNotOverwrite());
190-
supportingFiles.add(new SupportingFile("RestResourceRoot.mustache",
191-
(sourceFolder + '/' + invokerPackage).replace(".", "/"), "RestResourceRoot.java")
192-
.doNotOverwrite());
190+
191+
if (!interfaceOnly) {
192+
supportingFiles.add(new SupportingFile("RestResourceRoot.mustache",
193+
(sourceFolder + '/' + invokerPackage).replace(".", "/"), "RestResourceRoot.java")
194+
.doNotOverwrite());
195+
196+
supportingFiles.add(new SupportingFile("RestApplication.mustache",
197+
(sourceFolder + '/' + invokerPackage).replace(".", "/"), "RestApplication.java")
198+
.doNotOverwrite());
199+
}
193200

194201
if (generatePom) {
195202
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")
196203
.doNotOverwrite());
197204
}
198205

199-
supportingFiles.add(new SupportingFile("RestApplication.mustache",
200-
(sourceFolder + '/' + invokerPackage).replace(".", "/"), "RestApplication.java")
201-
.doNotOverwrite());
202-
203206
if (StringUtils.isNotEmpty(openApiSpecFileLocation)) {
204207
int index = openApiSpecFileLocation.lastIndexOf('/');
205208
String fileFolder;
@@ -276,6 +279,7 @@ public CodegenModel fromModel(String name, Schema model) {
276279
if (!jackson) {
277280
codegenModel.imports.remove("JsonValue");
278281
codegenModel.imports.remove("JsonProperty");
282+
codegenModel.imports.remove("JsonTypeName");
279283
}
280284
return codegenModel;
281285
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {{javaxPackage}}.validation.Valid;{{/useBeanValidation}}
2323
/**
2424
* Represents a collection of functions to interact with the API endpoints.
2525
*/
26-
@Path("{{commonPath}}"){{#useSwaggerAnnotations}}
26+
{{^interfaceOnly}}
27+
@Path("{{commonPath}}")
28+
{{/interfaceOnly}}
29+
{{#useSwaggerAnnotations}}
2730
@Api(description = "the {{{baseName}}} API"){{/useSwaggerAnnotations}}{{#hasConsumes}}
2831
@Consumes({ {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }){{/hasConsumes}}{{#hasProduces}}
2932
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }){{/hasProduces}}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* @return {{{message}}}
99
{{/responses}}
1010
*/
11-
@{{httpMethod}}{{#subresourceOperation}}
12-
@Path("{{{path}}}"){{/subresourceOperation}}{{#hasConsumes}}
11+
@{{httpMethod}}
12+
@Path("{{commonPath}}{{{path}}}"){{#hasConsumes}}
1313
@Consumes({ {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }){{/hasConsumes}}{{#hasProduces}}
1414
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }){{/hasProduces}}{{#useSwaggerAnnotations}}
1515
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}"{{#hasAuthMethods}}, authorizations = {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
}
2121

2222
@Override
23+
{{#jackson}}
2324
@JsonValue
25+
{{/jackson}}
2426
public String toString() {
2527
return String.valueOf(value);
2628
}
@@ -40,6 +42,7 @@
4042
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected string value '" + s + "'");{{/isNullable}}
4143
}
4244

45+
{{#jackson}}
4346
@JsonCreator
4447
public static {{datatypeWithEnum}} fromValue({{dataType}} value) {
4548
for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
@@ -49,4 +52,5 @@
4952
}
5053
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}}
5154
}
55+
{{/jackson}}
5256
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ import com.fasterxml.jackson.annotation.JsonValue;
4141
}
4242

4343
@Override
44+
{{#jackson}}
4445
@JsonValue
46+
{{/jackson}}
4547
public String toString() {
4648
return String.valueOf(value);
4749
}
4850

51+
{{#jackson}}
4952
@JsonCreator
5053
public static {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{dataType}}} value) {
5154
for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
@@ -55,4 +58,5 @@ import com.fasterxml.jackson.annotation.JsonValue;
5558
}
5659
{{#isNullable}}return null;{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}{{#allowableValues}}{{#enumVars}}{{#-last}}return {{{name}}};{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/enumUnknownDefaultCase}}{{/isNullable}}
5760
}
61+
{{/jackson}}
5862
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import io.swagger.annotations.*;
33
{{/useSwaggerAnnotations}}
44
import java.util.Objects;
5+
{{#jackson}}
56
import com.fasterxml.jackson.annotation.JsonProperty;
67
import com.fasterxml.jackson.annotation.JsonCreator;
78
import com.fasterxml.jackson.annotation.JsonValue;
89
import com.fasterxml.jackson.annotation.JsonTypeName;
9-
10+
{{/jackson}}
1011
{{#withXml}}
1112
import {{javaxPackage}}.xml.bind.annotation.XmlElement;
1213
import {{javaxPackage}}.xml.bind.annotation.XmlRootElement;
@@ -22,7 +23,7 @@ import {{javaxPackage}}.xml.bind.annotation.XmlEnumValue;
2223
**/{{/description}}
2324
{{#useSwaggerAnnotations}}{{#description}}@ApiModel(description = "{{{.}}}"){{/description}}{{/useSwaggerAnnotations}}{{#useMicroProfileOpenAPIAnnotations}}
2425
@org.eclipse.microprofile.openapi.annotations.media.Schema({{#title}}title="{{{.}}}", {{/title}}{{#description}}description="{{{.}}}"{{/description}}{{^description}}description=""{{/description}}){{/useMicroProfileOpenAPIAnnotations}}
25-
@JsonTypeName("{{name}}")
26+
{{#jackson}}@JsonTypeName("{{name}}"){{/jackson}}
2627
{{>generatedAnnotation}}{{>additionalModelTypeAnnotations}}{{>xmlPojoAnnotation}}
2728
{{#vendorExtensions.x-class-extra-annotation}}
2829
{{{vendorExtensions.x-class-extra-annotation}}}
@@ -61,6 +62,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
6162
public {{classname}}() {
6263
}
6364

65+
{{#jackson}}
6466
{{#generateJsonCreator}}
6567
{{#hasRequired}}
6668
@JsonCreator
@@ -85,6 +87,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
8587

8688
{{/hasRequired}}
8789
{{/generateJsonCreator}}
90+
{{/jackson}}
8891
{{#vars}}
8992
/**
9093
{{#description}}
@@ -108,12 +111,14 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
108111
{{#vendorExtensions.x-extra-annotation}}{{{vendorExtensions.x-extra-annotation}}}{{/vendorExtensions.x-extra-annotation}}{{#useSwaggerAnnotations}}
109112
@ApiModelProperty({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}value = "{{{description}}}"){{/useSwaggerAnnotations}}{{#useMicroProfileOpenAPIAnnotations}}
110113
@org.eclipse.microprofile.openapi.annotations.media.Schema({{#example}}example = "{{{.}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}description = "{{{description}}}"){{/useMicroProfileOpenAPIAnnotations}}
111-
@JsonProperty({{#required}}required = {{required}}, value = {{/required}}"{{baseName}}")
114+
{{#jackson}}@JsonProperty({{#required}}required = {{required}}, value = {{/required}}"{{baseName}}"){{/jackson}}
112115
{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}}public {{>beanValidatedType}} {{getter}}() {
113116
return {{name}};
114117
}
115118

119+
{{#jackson}}
116120
@JsonProperty({{#required}}required = {{required}}, value = {{/required}}"{{baseName}}")
121+
{{/jackson}}
117122
{{#vendorExtensions.x-setter-extra-annotation}}{{{vendorExtensions.x-setter-extra-annotation}}}
118123
{{/vendorExtensions.x-setter-extra-annotation}}public void {{setter}}({{{datatypeWithEnum}}} {{name}}) {
119124
this.{{name}} = {{name}};

samples/server/petstore/jaxrs-spec-interface-response/.openapi-generator/FILES

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ src/gen/java/org/openapitools/api/AnotherFakeApi.java
44
src/gen/java/org/openapitools/api/FakeApi.java
55
src/gen/java/org/openapitools/api/FakeClassnameTestApi.java
66
src/gen/java/org/openapitools/api/PetApi.java
7-
src/gen/java/org/openapitools/api/RestApplication.java
8-
src/gen/java/org/openapitools/api/RestResourceRoot.java
97
src/gen/java/org/openapitools/api/StoreApi.java
108
src/gen/java/org/openapitools/api/UserApi.java
119
src/gen/java/org/openapitools/model/AdditionalPropertiesAnyType.java

samples/server/petstore/jaxrs-spec-interface-response/src/gen/java/org/openapitools/api/AnotherFakeApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
/**
1818
* Represents a collection of functions to interact with the API endpoints.
1919
*/
20-
@Path("/another-fake/dummy")
2120
@Api(description = "the another-fake API")
2221
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen", comments = "Generator version: 7.17.0-SNAPSHOT")
2322
public interface AnotherFakeApi {
@@ -30,6 +29,7 @@ public interface AnotherFakeApi {
3029
* @return successful operation
3130
*/
3231
@PATCH
32+
@Path("/another-fake/dummy")
3333
@Consumes({ "application/json" })
3434
@Produces({ "application/json" })
3535
@ApiOperation(value = "To test special tags", notes = "To test special tags and operation ID starting with number", tags={ "$another-fake?" })

samples/server/petstore/jaxrs-spec-interface-response/src/gen/java/org/openapitools/api/FakeApi.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
/**
2727
* Represents a collection of functions to interact with the API endpoints.
2828
*/
29-
@Path("/fake")
3029
@Api(description = "the fake API")
3130
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen", comments = "Generator version: 7.17.0-SNAPSHOT")
3231
public interface FakeApi {
@@ -38,7 +37,7 @@ public interface FakeApi {
3837
* @return successful operation
3938
*/
4039
@POST
41-
@Path("/create_xml_item")
40+
@Path("/fake/create_xml_item")
4241
@Consumes({ "application/xml", "application/xml; charset=utf-8", "application/xml; charset=utf-16", "text/xml", "text/xml; charset=utf-8", "text/xml; charset=utf-16" })
4342
@ApiOperation(value = "creates an XmlItem", notes = "this route creates an XmlItem", tags={ "fake" })
4443
@ApiResponses(value = {
@@ -53,7 +52,7 @@ public interface FakeApi {
5352
* @return Output boolean
5453
*/
5554
@POST
56-
@Path("/outer/boolean")
55+
@Path("/fake/outer/boolean")
5756
@Produces({ "*/*" })
5857
@ApiOperation(value = "", notes = "Test serialization of outer boolean types", tags={ "fake" })
5958
@ApiResponses(value = {
@@ -68,7 +67,7 @@ public interface FakeApi {
6867
* @return Output composite
6968
*/
7069
@POST
71-
@Path("/outer/composite")
70+
@Path("/fake/outer/composite")
7271
@Produces({ "*/*" })
7372
@ApiOperation(value = "", notes = "Test serialization of object with outer number type", tags={ "fake" })
7473
@ApiResponses(value = {
@@ -83,7 +82,7 @@ public interface FakeApi {
8382
* @return Output number
8483
*/
8584
@POST
86-
@Path("/outer/number")
85+
@Path("/fake/outer/number")
8786
@Produces({ "*/*" })
8887
@ApiOperation(value = "", notes = "Test serialization of outer number types", tags={ "fake" })
8988
@ApiResponses(value = {
@@ -98,7 +97,7 @@ public interface FakeApi {
9897
* @return Output string
9998
*/
10099
@POST
101-
@Path("/outer/string")
100+
@Path("/fake/outer/string")
102101
@Produces({ "*/*" })
103102
@ApiOperation(value = "", notes = "Test serialization of outer string types", tags={ "fake" })
104103
@ApiResponses(value = {
@@ -113,7 +112,7 @@ public interface FakeApi {
113112
* @return Success
114113
*/
115114
@PUT
116-
@Path("/body-with-file-schema")
115+
@Path("/fake/body-with-file-schema")
117116
@Consumes({ "application/json" })
118117
@ApiOperation(value = "", notes = "For this test, the body for this request much reference a schema named `File`.", tags={ "fake" })
119118
@ApiResponses(value = {
@@ -129,7 +128,7 @@ public interface FakeApi {
129128
* @return Success
130129
*/
131130
@PUT
132-
@Path("/body-with-query-params")
131+
@Path("/fake/body-with-query-params")
133132
@Consumes({ "application/json" })
134133
@ApiOperation(value = "", notes = "", tags={ "fake" })
135134
@ApiResponses(value = {
@@ -144,6 +143,7 @@ public interface FakeApi {
144143
* @return successful operation
145144
*/
146145
@PATCH
146+
@Path("/fake")
147147
@Consumes({ "application/json" })
148148
@Produces({ "application/json" })
149149
@ApiOperation(value = "To test \"client\" model", notes = "To test \"client\" model", tags={ "fake" })
@@ -173,6 +173,7 @@ public interface FakeApi {
173173
* @return User not found
174174
*/
175175
@POST
176+
@Path("/fake")
176177
@Consumes({ "application/x-www-form-urlencoded" })
177178
@ApiOperation(value = "Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트", notes = "Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트", authorizations = {
178179

@@ -199,6 +200,7 @@ public interface FakeApi {
199200
* @return Not found
200201
*/
201202
@GET
203+
@Path("/fake")
202204
@Consumes({ "application/x-www-form-urlencoded" })
203205
@ApiOperation(value = "To test enum parameters", notes = "To test enum parameters", tags={ "fake" })
204206
@ApiResponses(value = {
@@ -219,6 +221,7 @@ public interface FakeApi {
219221
* @return Something wrong
220222
*/
221223
@DELETE
224+
@Path("/fake")
222225
@ApiOperation(value = "Fake endpoint to test group parameters (optional)", notes = "Fake endpoint to test group parameters (optional)", tags={ "fake" })
223226
@ApiResponses(value = {
224227
@ApiResponse(code = 400, message = "Something wrong", response = Void.class) })
@@ -232,7 +235,7 @@ public interface FakeApi {
232235
* @return successful operation
233236
*/
234237
@POST
235-
@Path("/inline-additionalProperties")
238+
@Path("/fake/inline-additionalProperties")
236239
@Consumes({ "application/json" })
237240
@ApiOperation(value = "test inline additionalProperties", notes = "", tags={ "fake" })
238241
@ApiResponses(value = {
@@ -248,7 +251,7 @@ public interface FakeApi {
248251
* @return successful operation
249252
*/
250253
@GET
251-
@Path("/jsonFormData")
254+
@Path("/fake/jsonFormData")
252255
@Consumes({ "application/x-www-form-urlencoded" })
253256
@ApiOperation(value = "test json serialization of form data", notes = "", tags={ "fake" })
254257
@ApiResponses(value = {
@@ -267,7 +270,7 @@ public interface FakeApi {
267270
* @return Success
268271
*/
269272
@PUT
270-
@Path("/test-query-parameters")
273+
@Path("/fake/test-query-parameters")
271274
@ApiOperation(value = "", notes = "To test the collection format in query parameters", tags={ "fake" })
272275
@ApiResponses(value = {
273276
@ApiResponse(code = 200, message = "Success", response = Void.class) })
@@ -283,7 +286,7 @@ public interface FakeApi {
283286
* @return successful operation
284287
*/
285288
@POST
286-
@Path("/{petId}/uploadImageWithRequiredFile")
289+
@Path("/fake/{petId}/uploadImageWithRequiredFile")
287290
@Consumes({ "multipart/form-data" })
288291
@Produces({ "application/json" })
289292
@ApiOperation(value = "uploads an image (required)", notes = "", authorizations = {

0 commit comments

Comments
 (0)