Skip to content

Commit b1f2a67

Browse files
authored
[JAVA jaxrs-spec gen] add option for generating swagger V3 annotations (#22300)
* feat: add support for Swagger v3 annotations to jaxrs-spec * test added unittest for jaxrs-spec with swagger3Annotations * test added integrationtest for jaxrs-spec with swagger3Annotations * test added integrationtest for jaxrs-spec with swaggerV2Annotations * documentation update for new option useSwaggerV3Annotations in the jaxrs-spec.md * test added integrationtest for jaxrs-spec with swaggerV3Annotations icm use JakartaEE * update documentation by executing: ./bin/utils/export_docs_generators.sh * ran on wsl: ./bin/generate-samples.sh ./bin/configs/*.yaml
1 parent 48648a4 commit b1f2a67

File tree

220 files changed

+38423
-8
lines changed

Some content is hidden

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

220 files changed

+38423
-8
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
generatorName: jaxrs-spec
2+
outputDir: samples/server/petstore/jaxrs-spec-swagger-annotations
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/jaxrs-spec/petstore-with-fake-endpoints-models-for-testing.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/JavaJaxRS/spec
5+
additionalProperties:
6+
artifactId: jaxrs-spec-petstore-server
7+
serializableModel: "true"
8+
hideGenerationTimestamp: "true"
9+
implicitHeadersRegex: (api_key|enum_header_string)
10+
generateBuilders: "true"
11+
useSwaggerAnnotations: "true"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: jaxrs-spec
2+
outputDir: samples/server/petstore/jaxrs-spec-swagger-v3-annotations-jakarta
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/jaxrs-spec/petstore-with-fake-endpoints-models-for-testing.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/JavaJaxRS/spec
5+
additionalProperties:
6+
artifactId: jaxrs-spec-petstore-server-jakarta-swagger-v3
7+
serializableModel: "true"
8+
hideGenerationTimestamp: "true"
9+
implicitHeadersRegex: (api_key|enum_header_string)
10+
generateBuilders: "true"
11+
useSwaggerV3Annotations: "true"
12+
useJakartaEe: "true"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
generatorName: jaxrs-spec
2+
outputDir: samples/server/petstore/jaxrs-spec-swagger-v3-annotations
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/jaxrs-spec/petstore-with-fake-endpoints-models-for-testing.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/JavaJaxRS/spec
5+
additionalProperties:
6+
artifactId: jaxrs-spec-petstore-server
7+
serializableModel: "true"
8+
hideGenerationTimestamp: "true"
9+
implicitHeadersRegex: (api_key|enum_header_string)
10+
generateBuilders: "true"
11+
useSwaggerV3Annotations: "true"

docs/generators/jaxrs-cxf-cdi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
8484
|useMutiny|Whether to use Smallrye Mutiny instead of CompletionStage for asynchronous computation. Only valid when library is set to quarkus.| |false|
8585
|useOneOfInterfaces|whether to use a java interface to describe a set of oneOf options, where each option is a class that implements the interface| |false|
8686
|useSwaggerAnnotations|Whether to generate Swagger annotations.| |true|
87+
|useSwaggerV3Annotations|Whether to generate Swagger v3 (OpenAPI v3) annotations.| |false|
8788
|useTags|use tags for creating interface and controller classnames| |false|
8889
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
8990

docs/generators/jaxrs-spec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
8585
|useMutiny|Whether to use Smallrye Mutiny instead of CompletionStage for asynchronous computation. Only valid when library is set to quarkus.| |false|
8686
|useOneOfInterfaces|whether to use a java interface to describe a set of oneOf options, where each option is a class that implements the interface| |false|
8787
|useSwaggerAnnotations|Whether to generate Swagger annotations.| |true|
88+
|useSwaggerV3Annotations|Whether to generate Swagger v3 (OpenAPI v3) annotations.| |false|
8889
|useTags|use tags for creating interface and controller classnames| |false|
8990
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
9091

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
4242
public static final String GENERATE_POM = "generatePom";
4343
public static final String USE_SWAGGER_ANNOTATIONS = "useSwaggerAnnotations";
4444
public static final String USE_MICROPROFILE_OPENAPI_ANNOTATIONS = "useMicroProfileOpenAPIAnnotations";
45+
public static final String USE_SWAGGER_V3_ANNOTATIONS = "useSwaggerV3Annotations";
4546
public static final String USE_MUTINY = "useMutiny";
4647
public static final String OPEN_API_SPEC_FILE_LOCATION = "openApiSpecFileLocation";
4748
public static final String GENERATE_JSON_CREATOR = "generateJsonCreator";
@@ -57,6 +58,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
5758
private boolean returnJbossResponse = false;
5859
private boolean generatePom = true;
5960
private boolean useSwaggerAnnotations = true;
61+
private boolean useSwaggerV3Annotations = false;
6062
private boolean useMicroProfileOpenAPIAnnotations = false;
6163
private boolean useMutiny = false;
6264

@@ -133,6 +135,7 @@ public JavaJAXRSSpecServerCodegen() {
133135
cliOptions.add(CliOption.newBoolean(RETURN_RESPONSE, "Whether generate API interface should return javax.ws.rs.core.Response instead of a deserialized entity. Only useful if interfaceOnly is true.").defaultValue(String.valueOf(returnResponse)));
134136
cliOptions.add(CliOption.newBoolean(RETURN_JBOSS_RESPONSE, "Whether generate API interface should return `org.jboss.resteasy.reactive.RestResponse` instead of a deserialized entity. This flag cannot be combined with `returnResponse` flag. It requires the flag `interfaceOnly` and `useJakartaEE` set to true, because `org.jboss.resteasy.reactive.RestResponse` was introduced in Quarkus 2.x").defaultValue(String.valueOf(returnJbossResponse)));
135137
cliOptions.add(CliOption.newBoolean(USE_SWAGGER_ANNOTATIONS, "Whether to generate Swagger annotations.", useSwaggerAnnotations));
138+
cliOptions.add(CliOption.newBoolean(USE_SWAGGER_V3_ANNOTATIONS, "Whether to generate Swagger v3 (OpenAPI v3) annotations.", useSwaggerV3Annotations));
136139
cliOptions.add(CliOption.newBoolean(USE_MICROPROFILE_OPENAPI_ANNOTATIONS, "Whether to generate Microprofile OpenAPI annotations. Only valid when library is set to quarkus.", useMicroProfileOpenAPIAnnotations));
137140
cliOptions.add(CliOption.newString(OPEN_API_SPEC_FILE_LOCATION, "Location where the file containing the spec will be generated in the output folder. No file generated when set to null or empty string."));
138141
cliOptions.add(CliOption.newBoolean(SUPPORT_ASYNC, "Wrap responses in CompletionStage type, allowing asynchronous computation (requires JAX-RS 2.1).", supportAsync));
@@ -149,14 +152,28 @@ public void processOpts() {
149152
convertPropertyToBooleanAndWriteBack(RETURN_JBOSS_RESPONSE, value -> returnJbossResponse = value);
150153
convertPropertyToBooleanAndWriteBack(SUPPORT_ASYNC, this::setSupportAsync);
151154
if (QUARKUS_LIBRARY.equals(library) || THORNTAIL_LIBRARY.equals(library) || HELIDON_LIBRARY.equals(library) || OPEN_LIBERTY_LIBRARY.equals(library) || KUMULUZEE_LIBRARY.equals(library)) {
155+
// disable Swagger v2 annotations in library modes; MicroProfile or Swagger v3 may be used instead
152156
useSwaggerAnnotations = false;
153157
} else {
154158
convertPropertyToBooleanAndWriteBack(USE_SWAGGER_ANNOTATIONS, value -> useSwaggerAnnotations = value);
155159
}
160+
// Swagger v3 can be used regardless of library
161+
convertPropertyToBooleanAndWriteBack(USE_SWAGGER_V3_ANNOTATIONS, value -> useSwaggerV3Annotations = value);
162+
// prefer v3 when requested
163+
if (useSwaggerV3Annotations) {
164+
useSwaggerAnnotations = false;
165+
}
156166
if (KUMULUZEE_LIBRARY.equals(library)) {
157167
super.setSourceFolder("src/main/java");
158168
}
159169

170+
if (useSwaggerAnnotations && useSwaggerV3Annotations) {
171+
throw new IllegalArgumentException("Flags 'useSwaggerAnnotations' (v2) and 'useSwaggerV3Annotations' (v3) are mutually exclusive. Please enable only one.");
172+
}
173+
if (useSwaggerV3Annotations && useMicroProfileOpenAPIAnnotations) {
174+
throw new IllegalArgumentException("Flags 'useSwaggerV3Annotations' and 'useMicroProfileOpenAPIAnnotations' are mutually exclusive. Please enable only one.");
175+
}
176+
160177
if (QUARKUS_LIBRARY.equals(library)) {
161178
convertPropertyToBooleanAndWriteBack(USE_MICROPROFILE_OPENAPI_ANNOTATIONS, value -> useMicroProfileOpenAPIAnnotations = value);
162179
}
@@ -184,6 +201,11 @@ public void processOpts() {
184201

185202
super.processOpts();
186203

204+
// expose flags to templates
205+
additionalProperties.put(USE_SWAGGER_ANNOTATIONS, useSwaggerAnnotations);
206+
additionalProperties.put(USE_SWAGGER_V3_ANNOTATIONS, useSwaggerV3Annotations);
207+
additionalProperties.put(USE_MICROPROFILE_OPENAPI_ANNOTATIONS, useMicroProfileOpenAPIAnnotations);
208+
187209
supportingFiles.clear(); // Don't need extra files provided by AbstractJAX-RS & Java Codegen
188210
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")
189211
.doNotOverwrite());

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import {{javaxPackage}}.ws.rs.core.Response;
99
{{#useSwaggerAnnotations}}
1010
import io.swagger.annotations.*;
1111
{{/useSwaggerAnnotations}}
12+
{{#useSwaggerV3Annotations}}
13+
import io.swagger.v3.oas.annotations.*;
14+
import io.swagger.v3.oas.annotations.media.*;
15+
import io.swagger.v3.oas.annotations.responses.*;
16+
import io.swagger.v3.oas.annotations.tags.Tag;
17+
{{/useSwaggerV3Annotations}}
1218
{{#supportAsync}}
1319
import java.util.concurrent.CompletionStage;
1420
import java.util.concurrent.CompletableFuture;
@@ -27,7 +33,8 @@ import {{javaxPackage}}.validation.Valid;{{/useBeanValidation}}
2733
@Path("{{commonPath}}")
2834
{{/interfaceOnly}}
2935
{{#useSwaggerAnnotations}}
30-
@Api(description = "the {{{baseName}}} API"){{/useSwaggerAnnotations}}{{#hasConsumes}}
36+
@Api(description = "the {{{baseName}}} API"){{/useSwaggerAnnotations}}{{#useSwaggerV3Annotations}}
37+
@Tag(name = "{{{baseName}}}"){{/useSwaggerV3Annotations}}{{#hasConsumes}}
3138
@Consumes({ {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }){{/hasConsumes}}{{#hasProduces}}
3239
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }){{/hasProduces}}
3340
{{>generatedAnnotation}}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
})
2727
{{/implicitHeadersParams.0}}
2828
@ApiResponses(value = { {{#responses}}
29-
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#returnContainer}}, responseContainer = "{{{.}}}"{{/returnContainer}}){{^-last}},{{/-last}}{{/responses}} }){{/useSwaggerAnnotations}}
29+
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#returnContainer}}, responseContainer = "{{{.}}}"{{/returnContainer}}){{^-last}},{{/-last}}{{/responses}} }){{/useSwaggerAnnotations}}{{#useSwaggerV3Annotations}}
30+
@Operation(summary = "{{{summary}}}", description = "{{{notes}}}")
31+
@ApiResponses(value = { {{#responses}}
32+
@ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"){{^-last}},{{/-last}}{{/responses}} }){{/useSwaggerV3Annotations}}
3033
{{#supportAsync}}{{>returnAsyncTypeInterface}}{{/supportAsync}}{{^supportAsync}}{{#returnResponse}}Response{{/returnResponse}}{{^returnResponse}}{{>returnTypeInterface}}{{/returnResponse}}{{/supportAsync}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}});

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
{{/implicitHeadersParams.0}}
1818
@ApiResponses(value = { {{#responses}}
1919
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#containerType}}, responseContainer = "{{{.}}}"{{/containerType}}){{^-last}},{{/-last}}{{/responses}}
20-
}){{/useSwaggerAnnotations}}
20+
}){{/useSwaggerAnnotations}}{{#useSwaggerV3Annotations}}
21+
@Operation(summary = "{{{summary}}}", description = "{{{notes}}}")
22+
@ApiResponses(value = { {{#responses}}
23+
@ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"){{^-last}},{{/-last}}{{/responses}}
24+
}){{/useSwaggerV3Annotations}}
2125
public {{#supportAsync}}CompletionStage<{{/supportAsync}}Response{{#supportAsync}}>{{/supportAsync}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}) {
2226
return {{#supportAsync}}CompletableFuture.supplyAsync(() -> {{/supportAsync}}Response.ok().entity("magic!").build(){{#supportAsync}}){{/supportAsync}};
2327
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import org.jboss.resteasy.annotations.GZIP;
1414
{{#useSwaggerAnnotations}}
1515
import io.swagger.annotations.*;
1616
{{/useSwaggerAnnotations}}
17+
{{#useSwaggerV3Annotations}}
18+
import io.swagger.v3.oas.annotations.*;
19+
import io.swagger.v3.oas.annotations.media.*;
20+
import io.swagger.v3.oas.annotations.responses.*;
21+
import io.swagger.v3.oas.annotations.tags.Tag;
22+
{{/useSwaggerV3Annotations}}
1723

1824
{{#supportAsync}}
1925
{{#useMutiny}}
@@ -96,7 +102,8 @@ import {{javaxPackage}}.validation.Valid;{{/useBeanValidation}}
96102
openIdConnectUrl = "{{openIdConnectUrl}}"
97103
){{^-last}}, {{/-last}}{{/isOpenId}}{{/authMethods}}
98104
}){{/hasAuthMethods}}{{/useMicroProfileOpenAPIAnnotations}}{{#useSwaggerAnnotations}}
99-
@Api(description = "the {{{baseName}}} API"){{/useSwaggerAnnotations}}
105+
@Api(description = "the {{{baseName}}} API"){{/useSwaggerAnnotations}}{{#useSwaggerV3Annotations}}
106+
@Tag(name = "{{{baseName}}}"){{/useSwaggerV3Annotations}}
100107
@Path("{{commonPath}}"){{#hasConsumes}}
101108
@Consumes({ {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }){{/hasConsumes}}{{#hasProduces}}
102109
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }){{/hasProduces}}

0 commit comments

Comments
 (0)