Skip to content

Commit 0013874

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 <patrick@reini.net>
1 parent 7ce0096 commit 0013874

6 files changed

Lines changed: 69 additions & 27 deletions

File tree

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
@@ -25,7 +25,9 @@ import {{rootJavaEEPackage}}.validation.Valid;
2525
{{#microprofileRegisterExceptionMapper}}
2626
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
2727
{{/microprofileRegisterExceptionMapper}}
28+
{{^microprofileServer}}
2829
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
30+
{{/microprofileServer}}
2931

3032

3133
{{#appName}}
@@ -75,10 +77,10 @@ public interface {{classname}} {
7577
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} })
7678
{{/hasProduces}}
7779
{{^singleRequestParameter}}
78-
{{^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;
80+
{{^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;
7981
{{/singleRequestParameter}}
8082
{{#singleRequestParameter}}
81-
{{^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;
83+
{{^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;
8284
{{#hasNonBodyParams}}
8385
public class {{operationIdCamelCase}}Request {
8486

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
@@ -1486,8 +1486,13 @@ public void testDefaultMicroprofileRestClientVersion() {
14861486

14871487
validateJavaSourceFiles(files);
14881488
assertThat(files).contains(output.resolve("pom.xml").toFile());
1489+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).doesNotExist();
1490+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).doesNotExist();
14891491
assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content()
1490-
.contains("import javax.");
1492+
.contains("import javax.")
1493+
.doesNotContain("ApiException")
1494+
.contains("WebApplicationException");
1495+
;
14911496
assertThat(output.resolve("pom.xml")).content()
14921497
.contains(
14931498
"<microprofile.rest.client.api.version>2.0</microprofile.rest.client.api.version>",
@@ -1500,7 +1505,11 @@ public void testDefaultMicroprofileRestClientVersion() {
15001505
public void testMicroprofileRestClientVersion_1_4_1() {
15011506
final Path output = newTempFolder();
15021507
final CodegenConfigurator configurator = new CodegenConfigurator()
1503-
.setAdditionalProperties(Map.of(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "1.4.1"))
1508+
.setAdditionalProperties(Map.of(
1509+
JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "1.4.1",
1510+
JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "true",
1511+
USE_RUNTIME_EXCEPTION,"false"
1512+
))
15041513
.setGeneratorName(JAVA_GENERATOR)
15051514
.setLibrary(JavaClientCodegen.MICROPROFILE)
15061515
.setInputSpec("src/test/resources/3_0/petstore.yaml")
@@ -1510,8 +1519,12 @@ public void testMicroprofileRestClientVersion_1_4_1() {
15101519

15111520
validateJavaSourceFiles(files);
15121521
assertThat(files).contains(output.resolve("pom.xml").toFile());
1522+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).exists();
1523+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).exists();
15131524
assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content()
1514-
.contains("import javax.");
1525+
.contains("import javax.")
1526+
.contains("ApiException")
1527+
.doesNotContain("WebApplicationException");
15151528
assertThat(output.resolve("pom.xml")).content()
15161529
.contains(
15171530
"<microprofile.rest.client.api.version>1.4.1</microprofile.rest.client.api.version>",
@@ -1544,7 +1557,10 @@ public void testMicroprofileRestClientIncorrectVersion() {
15441557
public void testMicroprofileRestClientVersion_3_0() {
15451558
final Path output = newTempFolder();
15461559
final CodegenConfigurator configurator = new CodegenConfigurator()
1547-
.setAdditionalProperties(Map.of(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0"))
1560+
.setAdditionalProperties(Map.of(
1561+
JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0",
1562+
JavaClientCodegen.USE_RUNTIME_EXCEPTION, "false"
1563+
))
15481564
.setGeneratorName(JAVA_GENERATOR)
15491565
.setLibrary(JavaClientCodegen.MICROPROFILE)
15501566
.setInputSpec("src/test/resources/3_0/petstore.yaml")
@@ -1554,8 +1570,12 @@ public void testMicroprofileRestClientVersion_3_0() {
15541570

15551571
validateJavaSourceFiles(files);
15561572
assertThat(files).contains(output.resolve("pom.xml").toFile());
1573+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).exists();
1574+
assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).doesNotExist();
15571575
assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content()
1558-
.contains("import jakarta.");
1576+
.contains("import jakarta.")
1577+
.contains("ApiException")
1578+
.doesNotContain("WebApplicationException");
15591579
assertThat(output.resolve("pom.xml")).content()
15601580
.contains(
15611581
"<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)