Skip to content

Commit e4e322e

Browse files
committed
Implements #21845 behavior
- Not generating 'ApiExceptionMapper' if 'microProfileRegisterExceptionMapper' is set to 'false' - Only import RegisterRestClient if actually needed - Default 'useRuntimeException' to 'true' for Microprofile and use 'WebApplicationException' instead of custom 'ApiException' Signed-off-by: Patrick Reinhart <[email protected]>
1 parent 20d5126 commit e4e322e

File tree

6 files changed

+69
-27
lines changed

6 files changed

+69
-27
lines changed

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public class JavaClientCodegen extends AbstractJavaCodegen
7070
public static final String CASE_INSENSITIVE_RESPONSE_HEADERS = "caseInsensitiveResponseHeaders";
7171
public static final String MICROPROFILE_FRAMEWORK = "microprofileFramework";
7272
public static final String MICROPROFILE_MUTINY = "microprofileMutiny";
73-
public static final String MICROPROFILE_GLOBAL_EXCEPTION_MAPPER = "microprofileGlobalExceptionMapper";
7473
public static final String MICROPROFILE_REGISTER_EXCEPTION_MAPPER = "microprofileRegisterExceptionMapper";
7574
public static final String USE_ABSTRACTION_FOR_FILES = "useAbstractionForFiles";
7675
public static final String DYNAMIC_OPERATIONS = "dynamicOperations";
@@ -128,8 +127,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
128127
@Setter protected String microprofileFramework = MICROPROFILE_DEFAULT;
129128
@Setter protected String microprofileRestClientVersion = MICROPROFILE_REST_CLIENT_DEFAULT_VERSION;
130129
@Setter protected boolean microprofileMutiny = false;
131-
@Setter protected boolean microProfileGlobalExceptionMapper = true;
132-
@Setter protected boolean microProfileRegisterExceptionMapper = true;
130+
@Setter protected boolean microProfileRegisterExceptionMapper = false;
133131
@Setter protected String configKey = null;
134132
@Setter(AccessLevel.PRIVATE) protected boolean configKeyFromClassName = false;
135133

@@ -240,8 +238,7 @@ public JavaClientCodegen() {
240238
cliOptions.add(CliOption.newBoolean(CASE_INSENSITIVE_RESPONSE_HEADERS, "Make API response's headers case-insensitive. Available on " + OKHTTP_GSON + ", " + JERSEY2 + " libraries"));
241239
cliOptions.add(CliOption.newString(MICROPROFILE_FRAMEWORK, "Framework for microprofile. Possible values \"kumuluzee\""));
242240
cliOptions.add(CliOption.newString(MICROPROFILE_MUTINY, "Whether to use async types for microprofile (currently only Smallrye Mutiny is supported)."));
243-
cliOptions.add(CliOption.newString(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "Should generated API Clients be annotated with @RegisterProvider(ApiExceptionMapper.class).").defaultValue("true"));
244-
cliOptions.add(CliOption.newString(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, "Should ApiExceptionMapper be annotated with @Provider making it a global exception mapper").defaultValue("true"));
241+
cliOptions.add(CliOption.newBoolean(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "Should generated API Clients be annotated with @RegisterProvider(ApiExceptionMapper.class).", this.microProfileRegisterExceptionMapper));
245242
cliOptions.add(CliOption.newBoolean(USE_ABSTRACTION_FOR_FILES, "Use alternative types instead of java.io.File to allow passing bytes without a file on disk. Available on resttemplate, webclient, restclient, libraries"));
246243
cliOptions.add(CliOption.newBoolean(DYNAMIC_OPERATIONS, "Generate operations dynamically at runtime from an OAS", this.dynamicOperations));
247244
cliOptions.add(CliOption.newBoolean(SUPPORT_STREAMING, "Support streaming endpoint (beta)", this.supportStreaming));
@@ -363,6 +360,11 @@ public void processOpts() {
363360
this.jackson = !additionalProperties.containsKey(CodegenConstants.SERIALIZATION_LIBRARY) ||
364361
SERIALIZATION_LIBRARY_JACKSON.equals(additionalProperties.get(CodegenConstants.SERIALIZATION_LIBRARY));
365362

363+
// use runtime exceptions for microprofile
364+
if (libMicroprofile) {
365+
useRuntimeException = true;
366+
}
367+
366368
convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, this::setUseOneOfDiscriminatorLookup);
367369

368370
// RxJava
@@ -399,11 +401,9 @@ public void processOpts() {
399401
}
400402
convertPropertyToStringAndWriteBack(MICROPROFILE_FRAMEWORK, this::setMicroprofileFramework);
401403

402-
convertPropertyToBooleanAndWriteBack(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, this::setMicroProfileGlobalExceptionMapper);
403404
convertPropertyToBooleanAndWriteBack(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, this::setMicroProfileRegisterExceptionMapper);
404405

405406
additionalProperties.put(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, microProfileRegisterExceptionMapper);
406-
additionalProperties.put(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, microProfileGlobalExceptionMapper);
407407

408408
convertPropertyToBooleanAndWriteBack(MICROPROFILE_MUTINY, this::setMicroprofileMutiny);
409409

@@ -709,8 +709,19 @@ public void processOpts() {
709709
String pomTemplate = mpRestClientVersions.get(microprofileRestClientVersion).pomTemplate;
710710
supportingFiles.add(new SupportingFile(pomTemplate, "", "pom.xml"));
711711
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
712-
supportingFiles.add(new SupportingFile("api_exception.mustache", apiExceptionFolder, "ApiException.java"));
713-
supportingFiles.add(new SupportingFile("api_exception_mapper.mustache", apiExceptionFolder, "ApiExceptionMapper.java"));
712+
713+
if (useRuntimeException) {
714+
if (microProfileRegisterExceptionMapper) {
715+
supportingFiles.add(new SupportingFile("api_exception.mustache", apiExceptionFolder, "ApiException.java"));
716+
supportingFiles.add(new SupportingFile("api_exception_mapper.mustache", apiExceptionFolder, "ApiExceptionMapper.java"));
717+
}
718+
} else {
719+
supportingFiles.add(new SupportingFile("api_exception.mustache", apiExceptionFolder, "ApiException.java"));
720+
if (microProfileRegisterExceptionMapper) {
721+
supportingFiles.add(new SupportingFile("api_exception_mapper.mustache", apiExceptionFolder, "ApiExceptionMapper.java"));
722+
}
723+
}
724+
714725
if (getSerializationLibrary() == null) {
715726
LOGGER.info("No serializationLibrary configured, using '{}' as fallback", SERIALIZATION_LIBRARY_JSONB);
716727
setSerializationLibrary(SERIALIZATION_LIBRARY_JSONB);
@@ -803,7 +814,7 @@ public void processOpts() {
803814
additionalProperties.remove(SERIALIZATION_LIBRARY_JSONB);
804815
break;
805816
}
806-
817+
807818
if (isLibrary(FEIGN)) {
808819
additionalProperties.put("feign-okhttp", "true");
809820
} else if (isLibrary(FEIGN_HC5)) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import {{rootJavaEEPackage}}.validation.Valid;
2727
{{#microprofileRegisterExceptionMapper}}
2828
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
2929
{{/microprofileRegisterExceptionMapper}}
30+
{{^microprofileServer}}
3031
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
32+
{{/microprofileServer}}
3133

3234
{{#appName}}
3335
/**
@@ -76,10 +78,10 @@ public interface {{classname}} {
7678
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} })
7779
{{/hasProduces}}
7880
{{^singleRequestParameter}}
79-
{{^vendorExtensions.x-java-is-response-void}}{{#microprofileServer}}{{> server_operation}}{{/microprofileServer}}{{^microprofileServer}}{{> client_operation}}{{/microprofileServer}}{{/vendorExtensions.x-java-is-response-void}}{{#vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<Void>{{/microprofileMutiny}}{{^microprofileMutiny}}void{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException, ProcessingException;
81+
{{^vendorExtensions.x-java-is-response-void}}{{#microprofileServer}}{{> server_operation}}{{/microprofileServer}}{{^microprofileServer}}{{> client_operation}}{{/microprofileServer}}{{/vendorExtensions.x-java-is-response-void}}{{#vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<Void>{{/microprofileMutiny}}{{^microprofileMutiny}}void{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}}, {{/-last}}{{/allParams}}) throws {{#useRuntimeException}}WebApplicationException{{/useRuntimeException}}{{^useRuntimeException}}ApiException{{/useRuntimeException}}, ProcessingException;
8082
{{/singleRequestParameter}}
8183
{{#singleRequestParameter}}
82-
{{^vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<{{{returnType}}}>{{/microprofileMutiny}}{{^microprofileMutiny}}{{{returnType}}}{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}}{{#vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<Void>{{/microprofileMutiny}}{{^microprofileMutiny}}void{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}} {{nickname}}({{#hasNonBodyParams}}@BeanParam {{operationIdCamelCase}}Request request{{/hasNonBodyParams}}{{#bodyParams}}{{#hasNonBodyParams}}, {{/hasNonBodyParams}}{{>bodyParams}}{{/bodyParams}}) throws ApiException, ProcessingException;
84+
{{^vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<{{{returnType}}}>{{/microprofileMutiny}}{{^microprofileMutiny}}{{{returnType}}}{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}}{{#vendorExtensions.x-java-is-response-void}}{{#microprofileMutiny}}Uni<Void>{{/microprofileMutiny}}{{^microprofileMutiny}}void{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}} {{nickname}}({{#hasNonBodyParams}}@BeanParam {{operationIdCamelCase}}Request request{{/hasNonBodyParams}}{{#bodyParams}}{{#hasNonBodyParams}}, {{/hasNonBodyParams}}{{>bodyParams}}{{/bodyParams}}) throws {{#useRuntimeException}}WebApplicationException{{/useRuntimeException}}{{^useRuntimeException}}ApiException{{/useRuntimeException}}, ProcessingException;
8385
{{#hasNonBodyParams}}
8486
public class {{operationIdCamelCase}}Request {
8587

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22
package {{apiPackage}};
33

44
import {{rootJavaEEPackage}}.ws.rs.core.Response;
5+
{{#useRuntimeException}}
6+
import {{rootJavaEEPackage}}.ws.rs.WebApplicationException;
7+
{{/useRuntimeException}}
58

6-
public class ApiException extends{{#useRuntimeException}} RuntimeException {{/useRuntimeException}}{{^useRuntimeException}} Exception {{/useRuntimeException}}{
9+
public class ApiException extends{{#useRuntimeException}} WebApplicationException {{/useRuntimeException}}{{^useRuntimeException}} Exception {{/useRuntimeException}}{
710
811
private static final long serialVersionUID = 1L;
12+
{{#useRuntimeException}}
13+
14+
public ApiException(Response response) {
15+
super(response);
16+
}
17+
{{/useRuntimeException}}
18+
{{^useRuntimeException}}
919
private Response response;
1020

1121
public ApiException() {
@@ -20,4 +30,5 @@ public class ApiException extends{{#useRuntimeException}} RuntimeException {{/us
2030
public Response getResponse() {
2131
return this.response;
2232
}
33+
{{/useRuntimeException}}
2334
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ package {{apiPackage}};
33

44
import {{rootJavaEEPackage}}.ws.rs.core.MultivaluedMap;
55
import {{rootJavaEEPackage}}.ws.rs.core.Response;
6-
{{#microprofileGlobalExceptionMapper}}
76
import {{rootJavaEEPackage}}.ws.rs.ext.Provider;
8-
{{/microprofileGlobalExceptionMapper}}
97
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
108

11-
{{#microprofileGlobalExceptionMapper}}
129
@Provider
13-
{{/microprofileGlobalExceptionMapper}}
1410
public class ApiExceptionMapper
1511
implements ResponseExceptionMapper<ApiException> {
1612

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,8 +1488,13 @@ public void testDefaultMicroprofileRestClientVersion() {
14881488

14891489
validateJavaSourceFiles(files);
14901490
assertThat(files).contains(output.resolve("pom.xml").toFile());
1491+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).doesNotExist();
1492+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).doesNotExist();
14911493
assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content()
1492-
.contains("import javax.");
1494+
.contains("import javax.")
1495+
.doesNotContain("ApiException")
1496+
.contains("WebApplicationException");
1497+
;
14931498
assertThat(output.resolve("pom.xml")).content()
14941499
.contains(
14951500
"<microprofile.rest.client.api.version>2.0</microprofile.rest.client.api.version>",
@@ -1502,7 +1507,11 @@ public void testDefaultMicroprofileRestClientVersion() {
15021507
public void testMicroprofileRestClientVersion_1_4_1() {
15031508
final Path output = newTempFolder();
15041509
final CodegenConfigurator configurator = new CodegenConfigurator()
1505-
.setAdditionalProperties(Map.of(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "1.4.1"))
1510+
.setAdditionalProperties(Map.of(
1511+
JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "1.4.1",
1512+
JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "true",
1513+
USE_RUNTIME_EXCEPTION,"false"
1514+
))
15061515
.setGeneratorName(JAVA_GENERATOR)
15071516
.setLibrary(JavaClientCodegen.MICROPROFILE)
15081517
.setInputSpec("src/test/resources/3_0/petstore.yaml")
@@ -1512,8 +1521,12 @@ public void testMicroprofileRestClientVersion_1_4_1() {
15121521

15131522
validateJavaSourceFiles(files);
15141523
assertThat(files).contains(output.resolve("pom.xml").toFile());
1524+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).exists();
1525+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).exists();
15151526
assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content()
1516-
.contains("import javax.");
1527+
.contains("import javax.")
1528+
.contains("ApiException")
1529+
.doesNotContain("WebApplicationException");
15171530
assertThat(output.resolve("pom.xml")).content()
15181531
.contains(
15191532
"<microprofile.rest.client.api.version>1.4.1</microprofile.rest.client.api.version>",
@@ -1546,7 +1559,10 @@ public void testMicroprofileRestClientIncorrectVersion() {
15461559
public void testMicroprofileRestClientVersion_3_0() {
15471560
final Path output = newTempFolder();
15481561
final CodegenConfigurator configurator = new CodegenConfigurator()
1549-
.setAdditionalProperties(Map.of(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0"))
1562+
.setAdditionalProperties(Map.of(
1563+
JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0",
1564+
JavaClientCodegen.USE_RUNTIME_EXCEPTION, "false"
1565+
))
15501566
.setGeneratorName(JAVA_GENERATOR)
15511567
.setLibrary(JavaClientCodegen.MICROPROFILE)
15521568
.setInputSpec("src/test/resources/3_0/petstore.yaml")
@@ -1556,8 +1572,12 @@ public void testMicroprofileRestClientVersion_3_0() {
15561572

15571573
validateJavaSourceFiles(files);
15581574
assertThat(files).contains(output.resolve("pom.xml").toFile());
1575+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).exists();
1576+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).doesNotExist();
15591577
assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content()
1560-
.contains("import jakarta.");
1578+
.contains("import jakarta.")
1579+
.contains("ApiException")
1580+
.doesNotContain("WebApplicationException");
15611581
assertThat(output.resolve("pom.xml")).content()
15621582
.contains(
15631583
"<microprofile.rest.client.api.version>3.0</microprofile.rest.client.api.version>",

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/microprofile/JavaMicroprofileServerCodegenTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.function.Function;
2020
import java.util.stream.Collectors;
2121

22+
import static org.assertj.core.api.Assertions.assertThat;
2223
import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;
2324

2425
public class JavaMicroprofileServerCodegenTest {
@@ -138,6 +139,7 @@ public void testGeneratedApiHasApiExceptionMapperRegisteredWhenUsingDefaultConfi
138139
.readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI();
139140

140141
codegen.setOutputDir(output.getAbsolutePath());
142+
codegen.additionalProperties().put(JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "true");
141143

142144
ClientOptInput input = new ClientOptInput()
143145
.openAPI(openAPI)
@@ -165,7 +167,6 @@ public void testGeneratedApiDoesNotHaveApiExceptionMapperRegisteredWhenDisabling
165167
.readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI();
166168

167169
codegen.setOutputDir(output.getAbsolutePath());
168-
codegen.additionalProperties().put(JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "false");
169170

170171
ClientOptInput input = new ClientOptInput()
171172
.openAPI(openAPI)
@@ -192,6 +193,7 @@ public void testGeneratedApiExceptionMapperHasProviderAnnotationWhenUsingDefault
192193
.readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI();
193194

194195
codegen.setOutputDir(output.getAbsolutePath());
196+
codegen.additionalProperties().put(JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "true");
195197

196198
ClientOptInput input = new ClientOptInput()
197199
.openAPI(openAPI)
@@ -218,7 +220,8 @@ public void testGeneratedApiExceptionMapperDoesNotHaveProviderAnnotationWhenDisa
218220
.readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI();
219221

220222
codegen.setOutputDir(output.getAbsolutePath());
221-
codegen.additionalProperties().put(JavaClientCodegen.MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, "false");
223+
codegen.additionalProperties().put(JavaClientCodegen.USE_RUNTIME_EXCEPTION, "true");
224+
222225

223226
ClientOptInput input = new ClientOptInput()
224227
.openAPI(openAPI)
@@ -231,9 +234,8 @@ public void testGeneratedApiExceptionMapperDoesNotHaveProviderAnnotationWhenDisa
231234

232235
validateJavaSourceFiles(files);
233236

234-
JavaFileAssert.assertThat(filesMap.get("ApiExceptionMapper.java"))
235-
.assertTypeAnnotations()
236-
.doesNotContainWithName("Provider");
237+
assertThat(filesMap.get("ApiException.java")).isNull();
238+
assertThat(filesMap.get("ApiExceptionMapper.java")).isNull();
237239
}
238240
}
239241

0 commit comments

Comments
 (0)