Skip to content

Commit b273a9b

Browse files
committed
Merge branch 'master' of https://github.com/cmoine-swi/openapi-generator into cmoine-swi-master
2 parents 1d739f1 + b7d9bd7 commit b273a9b

File tree

9 files changed

+180
-74
lines changed

9 files changed

+180
-74
lines changed

docs/generators/jaxrs-spec.md

Lines changed: 71 additions & 70 deletions
Large diffs are not rendered by default.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
3737

3838
public static final String INTERFACE_ONLY = "interfaceOnly";
3939
public static final String RETURN_RESPONSE = "returnResponse";
40+
public static final String RETURN_JBOSS_RESPONSE = "returnJBossResponse";
4041
public static final String GENERATE_POM = "generatePom";
4142
public static final String USE_SWAGGER_ANNOTATIONS = "useSwaggerAnnotations";
4243
public static final String USE_MICROPROFILE_OPENAPI_ANNOTATIONS = "useMicroProfileOpenAPIAnnotations";
@@ -52,6 +53,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
5253

5354
private boolean interfaceOnly = false;
5455
private boolean returnResponse = false;
56+
private boolean returnJbossResponse = false;
5557
private boolean generatePom = true;
5658
private boolean useSwaggerAnnotations = true;
5759
private boolean useMicroProfileOpenAPIAnnotations = false;
@@ -128,6 +130,7 @@ public JavaJAXRSSpecServerCodegen() {
128130
cliOptions.add(CliOption.newBoolean(GENERATE_POM, "Whether to generate pom.xml if the file does not already exist.").defaultValue(String.valueOf(generatePom)));
129131
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.").defaultValue(String.valueOf(interfaceOnly)));
130132
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)));
133+
cliOptions.add(CliOption.newBoolean(RETURN_JBOSS_RESPONSE, "Whether generate API interface should return org.jboss.resteasy.reactive.RestResponse instead of a deserialized entity. Only useful if interfaceOnly is true. This flag has the priority over the returnResponse flag.").defaultValue(String.valueOf(returnJbossResponse)));
131134
cliOptions.add(CliOption.newBoolean(USE_SWAGGER_ANNOTATIONS, "Whether to generate Swagger annotations.", useSwaggerAnnotations));
132135
cliOptions.add(CliOption.newBoolean(USE_MICROPROFILE_OPENAPI_ANNOTATIONS, "Whether to generate Microprofile OpenAPI annotations. Only valid when library is set to quarkus.", useMicroProfileOpenAPIAnnotations));
133136
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."));
@@ -142,6 +145,7 @@ public void processOpts() {
142145

143146
convertPropertyToBooleanAndWriteBack(INTERFACE_ONLY, value -> interfaceOnly = value);
144147
convertPropertyToBooleanAndWriteBack(RETURN_RESPONSE, value -> returnResponse = value);
148+
convertPropertyToBooleanAndWriteBack(RETURN_JBOSS_RESPONSE, value -> returnJbossResponse = value);
145149
convertPropertyToBooleanAndWriteBack(SUPPORT_ASYNC, this::setSupportAsync);
146150
if (QUARKUS_LIBRARY.equals(library) || THORNTAIL_LIBRARY.equals(library) || HELIDON_LIBRARY.equals(library) || OPEN_LIBERTY_LIBRARY.equals(library) || KUMULUZEE_LIBRARY.equals(library)) {
147151
useSwaggerAnnotations = false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package {{package}};
44
{{/imports}}
55

66
import {{javaxPackage}}.ws.rs.*;
7-
import {{javaxPackage}}.ws.rs.core.Response;
7+
{{#returnJBossResponse}}import org.jboss.resteasy.reactive.RestResponse;{{/returnJBossResponse}}{{^returnJBossResponse}}{{#returnResponse}}import {{javaxPackage}}.ws.rs.core.Response;{{/returnResponse}}{{/returnJBossResponse}}
88

99
{{#useGzipFeature}}
1010
import org.jboss.resteasy.annotations.GZIP;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@
4141
{{^vendorExtensions.x-java-is-response-void}}@org.eclipse.microprofile.openapi.annotations.media.Content(schema = @org.eclipse.microprofile.openapi.annotations.media.Schema(implementation = {{{baseType}}}.class{{#vendorExtensions.x-microprofile-open-api-return-schema-container}}, type = {{{.}}} {{/vendorExtensions.x-microprofile-open-api-return-schema-container}}{{#vendorExtensions.x-microprofile-open-api-return-unique-items}}, uniqueItems = true {{/vendorExtensions.x-microprofile-open-api-return-unique-items}})){{/vendorExtensions.x-java-is-response-void}}
4242
}){{^-last}},{{/-last}}{{/responses}}
4343
}){{/hasProduces}}{{/useMicroProfileOpenAPIAnnotations}}
44-
{{#supportAsync}}{{>returnAsyncTypeInterface}}{{/supportAsync}}{{^supportAsync}}{{#returnResponse}}Response{{/returnResponse}}{{^returnResponse}}{{>returnTypeInterface}}{{/returnResponse}}{{/supportAsync}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}});
44+
{{#supportAsync}}{{>returnAsyncTypeInterface}}{{/supportAsync}}{{^supportAsync}}{{#returnJBossResponse}}{{>returnResponseTypeInterface}}{{/returnJBossResponse}}{{^returnJBossResponse}}{{#returnResponse}}Response{{/returnResponse}}{{^returnResponse}}{{>returnTypeInterface}}{{/returnResponse}}{{/returnJBossResponse}}{{/supportAsync}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
{{^vendorExtensions.x-java-is-response-void}}@org.eclipse.microprofile.openapi.annotations.media.Content(schema = @org.eclipse.microprofile.openapi.annotations.media.Schema(implementation = {{{baseType}}}.class{{#vendorExtensions.x-microprofile-open-api-return-schema-container}}, type = {{{.}}} {{/vendorExtensions.x-microprofile-open-api-return-schema-container}}{{#vendorExtensions.x-microprofile-open-api-return-unique-items}}, uniqueItems = true {{/vendorExtensions.x-microprofile-open-api-return-unique-items}})){{/vendorExtensions.x-java-is-response-void}}
4141
}){{^-last}},{{/-last}}{{/responses}}
4242
}){{/hasProduces}}{{/useMicroProfileOpenAPIAnnotations}}
43-
public {{#supportAsync}}{{#useMutiny}}Uni{{/useMutiny}}{{^useMutiny}}CompletionStage{{/useMutiny}}<{{/supportAsync}}Response{{#supportAsync}}>{{/supportAsync}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}) {
43+
public {{#supportAsync}}{{#useMutiny}}Uni{{/useMutiny}}{{^useMutiny}}CompletionStage{{/useMutiny}}<{{/supportAsync}}{{#returnJBossResponse}}{{>returnResponseTypeInterface}}{{/returnJBossResponse}}{{^returnJBossResponse}}Response{{/returnJBossResponse}}{{#supportAsync}}>{{/supportAsync}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}) {
4444
return {{#supportAsync}}{{#useMutiny}}Uni.createFrom().item({{/useMutiny}}{{^useMutiny}}CompletableFuture.supplyAsync(() -> {{/useMutiny}}{{/supportAsync}}Response.ok().entity("magic!").build(){{#supportAsync}}){{/supportAsync}};
4545
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{#useMutiny}}Uni{{/useMutiny}}{{^useMutiny}}CompletionStage{{/useMutiny}}<{{#returnResponse}}Response{{/returnResponse}}{{^returnResponse}}{{#returnContainer}}{{#isMap}}Map<String, {{{returnBaseType}}}>{{/isMap}}{{#isArray}}{{{returnContainer}}}<{{{returnBaseType}}}>{{/isArray}}{{/returnContainer}}{{^returnContainer}}{{{returnBaseType}}}{{/returnContainer}}{{/returnResponse}}>
1+
{{#useMutiny}}Uni{{/useMutiny}}{{^useMutiny}}CompletionStage{{/useMutiny}}<{{>returnResponseTypeInterface}}>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#returnJBossResponse}}RestResponse<{{>returnType}}>{{/returnJBossResponse}}{{^returnJBossResponse}}{{#returnResponse}}Response{{/returnResponse}}{{^returnResponse}}{{>returnType}}{{/returnResponse}}{{/returnJBossResponse}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#returnContainer}}{{#isMap}}Map<String, {{{returnBaseType}}}>{{/isMap}}{{#isArray}}{{{returnContainer}}}<{{{returnBaseType}}}>{{/isArray}}{{/returnContainer}}{{^returnContainer}}{{{returnBaseType}}}{{/returnContainer}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,38 @@ public void generateApiWithAsyncSupportAndInterfaceOnlyAndResponse() throws Exce
524524
"\nCompletionStage<Response> pingGet();\n");
525525
}
526526

527+
@Test
528+
public void generateApiWithAsyncSupportAndInterfaceOnlyAndJBossResponse() throws Exception {
529+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
530+
output.deleteOnExit();
531+
532+
final OpenAPI openAPI = new OpenAPIParser()
533+
.readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI();
534+
535+
codegen.setOutputDir(output.getAbsolutePath());
536+
codegen.setLibrary(QUARKUS_LIBRARY);
537+
codegen.additionalProperties().put(SUPPORT_ASYNC, true); //Given support async is enabled
538+
codegen.additionalProperties().put(INTERFACE_ONLY, true); //And only interfaces are generated
539+
codegen.additionalProperties().put(RETURN_JBOSS_RESPONSE, true); //And return type is RestResponse
540+
541+
final ClientOptInput input = new ClientOptInput()
542+
.openAPI(openAPI)
543+
.config(codegen); //Using JavaJAXRSSpecServerCodegen
544+
545+
final DefaultGenerator generator = new DefaultGenerator();
546+
final List<File> files = generator.opts(input).generate(); //When generating files
547+
548+
//Then the java files are compilable
549+
validateJavaSourceFiles(files);
550+
551+
//And the generated interface contains CompletionStage<RestResponse<Pet>>
552+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/PetApi.java");
553+
assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/api/PetApi.java"),
554+
"\nimport org.jboss.resteasy.reactive.RestResponse;\n",
555+
"\nimport java.util.concurrent.CompletionStage;\n",
556+
"CompletionStage<RestResponse<Pet>> addPet", "CompletionStage<RestResponse<Void>> deletePet");
557+
}
558+
527559

528560
@Test
529561
public void generatePetstoreAPIWithAsyncSupport() throws Exception {
@@ -975,6 +1007,73 @@ public void generateSpecInterfaceWithMicroprofileOpenApiAnnotations() throws Exc
9751007
" title = \"user\", version=\"1.0.0\", description=\"Operations about user\",");
9761008
}
9771009

1010+
@Test
1011+
public void generateSpecInterfaceWithJBossResponse() throws Exception {
1012+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1013+
output.deleteOnExit();
1014+
1015+
final OpenAPI openAPI = new OpenAPIParser()
1016+
.readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI();
1017+
1018+
codegen.setOutputDir(output.getAbsolutePath());
1019+
codegen.additionalProperties().put(INTERFACE_ONLY, true); //And only interfaces are generated
1020+
codegen.additionalProperties().put(USE_TAGS, true); //And use tags to generate everything in several API files
1021+
codegen.additionalProperties().put(RETURN_JBOSS_RESPONSE, true); // Use JBoss Response type
1022+
codegen.additionalProperties().put(USE_JAKARTA_EE, true); // Use Jakarta
1023+
codegen.setLibrary(QUARKUS_LIBRARY); // Set Quarkus
1024+
1025+
final ClientOptInput input = new ClientOptInput()
1026+
.openAPI(openAPI)
1027+
.config(codegen); //using JavaJAXRSSpecServerCodegen
1028+
1029+
final DefaultGenerator generator = new DefaultGenerator();
1030+
final List<File> files = generator.opts(input).generate(); //When generating files
1031+
1032+
//Then the java files are compilable
1033+
validateJavaSourceFiles(files);
1034+
1035+
//And the generated interfaces contains RestResponse
1036+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/PetApi.java");
1037+
assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/api/PetApi.java"),
1038+
"\nimport org.jboss.resteasy.reactive.RestResponse;\n",
1039+
"RestResponse<Pet> addPet", "RestResponse<Void> deletePet", "RestResponse<List<Pet>> findPetsByStatus",
1040+
"RestResponse<Void> updatePetWithForm", "RestResponse<ModelApiResponse> uploadFile");
1041+
}
1042+
1043+
@Test
1044+
public void generateSpecInterfaceWithMutinyAndJBossResponse() throws Exception {
1045+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1046+
output.deleteOnExit();
1047+
1048+
final OpenAPI openAPI = new OpenAPIParser()
1049+
.readLocation("src/test/resources/3_0/petstore.yaml", null, new ParseOptions()).getOpenAPI();
1050+
1051+
codegen.setOutputDir(output.getAbsolutePath());
1052+
codegen.additionalProperties().put(INTERFACE_ONLY, true); //And only interfaces are generated
1053+
codegen.additionalProperties().put(USE_TAGS, true); //And use tags to generate everything in several API files
1054+
codegen.additionalProperties().put(RETURN_JBOSS_RESPONSE, true); // Use JBoss Response type
1055+
codegen.additionalProperties().put(SUPPORT_ASYNC, true);
1056+
codegen.additionalProperties().put(USE_MUTINY, true); // Use Mutiny
1057+
codegen.setLibrary(QUARKUS_LIBRARY); // Set Quarkus
1058+
1059+
final ClientOptInput input = new ClientOptInput()
1060+
.openAPI(openAPI)
1061+
.config(codegen); //using JavaJAXRSSpecServerCodegen
1062+
1063+
final DefaultGenerator generator = new DefaultGenerator();
1064+
final List<File> files = generator.opts(input).generate(); //When generating files
1065+
1066+
//Then the java files are compilable
1067+
validateJavaSourceFiles(files);
1068+
1069+
//And the generated interfaces contains RestResponse
1070+
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/api/PetApi.java");
1071+
assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/api/PetApi.java"),
1072+
"\nimport org.jboss.resteasy.reactive.RestResponse;\n", "Uni<RestResponse<Pet>> addPet",
1073+
"Uni<RestResponse<Void>> deletePet", "Uni<RestResponse<List<Pet>>> findPetsByStatus",
1074+
"Uni<RestResponse<ModelApiResponse>> uploadFile");
1075+
}
1076+
9781077
@Test
9791078
public void generateSpecNonInterfaceWithMicroprofileOpenApiAnnotations() throws Exception {
9801079
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();

0 commit comments

Comments
 (0)