Skip to content

Commit fad5cd9

Browse files
committed
[java][Microprofile] add config options to disable usage of ApiExceptionMapper
1 parent 6bbefca commit fad5cd9

File tree

6 files changed

+130
-1
lines changed

6 files changed

+130
-1
lines changed

docs/generators/java-microprofile.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ These options may be applied as additional-properties (cli) or configOptions (pl
6666
|licenseName|The name of the license| |Unlicense|
6767
|licenseUrl|The URL of the license| |http://unlicense.org|
6868
|microprofileFramework|Framework for microprofile. Possible values "kumuluzee"| |null|
69+
|microprofileGlobalExceptionMapper|Should ApiExceptionMapper be annotated with @Provider making it a global exception mapper| |true|
6970
|microprofileMutiny|Whether to use async types for microprofile (currently only Smallrye Mutiny is supported).| |null|
71+
|microprofileRegisterExceptionMapper|Should generated API Clients be annotated with @RegisterProvider(ApiExceptionMapper.class).| |true|
7072
|microprofileRestClientVersion|Version of MicroProfile Rest Client API.| |null|
7173
|modelPackage|package for generated models| |org.openapitools.client.model|
7274
|openApiNullable|Enable OpenAPI Jackson Nullable library. Not supported by `microprofile` library.| |true|

docs/generators/java.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ These options may be applied as additional-properties (cli) or configOptions (pl
6666
|licenseName|The name of the license| |Unlicense|
6767
|licenseUrl|The URL of the license| |http://unlicense.org|
6868
|microprofileFramework|Framework for microprofile. Possible values "kumuluzee"| |null|
69+
|microprofileGlobalExceptionMapper|Should ApiExceptionMapper be annotated with @Provider making it a global exception mapper| |true|
6970
|microprofileMutiny|Whether to use async types for microprofile (currently only Smallrye Mutiny is supported).| |null|
71+
|microprofileRegisterExceptionMapper|Should generated API Clients be annotated with @RegisterProvider(ApiExceptionMapper.class).| |true|
7072
|microprofileRestClientVersion|Version of MicroProfile Rest Client API.| |null|
7173
|modelPackage|package for generated models| |org.openapitools.client.model|
7274
|openApiNullable|Enable OpenAPI Jackson Nullable library. Not supported by `microprofile` library.| |true|

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ 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";
74+
public static final String MICROPROFILE_REGISTER_EXCEPTION_MAPPER = "microprofileRegisterExceptionMapper";
7375
public static final String USE_ABSTRACTION_FOR_FILES = "useAbstractionForFiles";
7476
public static final String DYNAMIC_OPERATIONS = "dynamicOperations";
7577
public static final String SUPPORT_STREAMING = "supportStreaming";
@@ -118,6 +120,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen
118120
@Setter protected String microprofileFramework = MICROPROFILE_DEFAULT;
119121
@Setter protected String microprofileRestClientVersion = MICROPROFILE_REST_CLIENT_DEFAULT_VERSION;
120122
@Setter protected boolean microprofileMutiny = false;
123+
@Setter protected boolean microProfileGlobalExceptionMapper = true;
124+
@Setter protected boolean microProfileRegisterExceptionMapper = true;
121125
@Setter protected String configKey = null;
122126
@Setter(AccessLevel.PRIVATE) protected boolean configKeyFromClassName = false;
123127

@@ -226,6 +230,8 @@ public JavaClientCodegen() {
226230
cliOptions.add(CliOption.newBoolean(CASE_INSENSITIVE_RESPONSE_HEADERS, "Make API response's headers case-insensitive. Available on " + OKHTTP_GSON + ", " + JERSEY2 + " libraries"));
227231
cliOptions.add(CliOption.newString(MICROPROFILE_FRAMEWORK, "Framework for microprofile. Possible values \"kumuluzee\""));
228232
cliOptions.add(CliOption.newString(MICROPROFILE_MUTINY, "Whether to use async types for microprofile (currently only Smallrye Mutiny is supported)."));
233+
cliOptions.add(CliOption.newBoolean(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "Should generated API Clients be annotated with @RegisterProvider(ApiExceptionMapper.class).", true));
234+
cliOptions.add(CliOption.newBoolean(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, "Should ApiExceptionMapper be annotated with @Provider making it a global exception mapper", true));
229235
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"));
230236
cliOptions.add(CliOption.newBoolean(DYNAMIC_OPERATIONS, "Generate operations dynamically at runtime from an OAS", this.dynamicOperations));
231237
cliOptions.add(CliOption.newBoolean(SUPPORT_STREAMING, "Support streaming endpoint (beta)", this.supportStreaming));
@@ -375,6 +381,9 @@ public void processOpts() {
375381
}
376382
convertPropertyToStringAndWriteBack(MICROPROFILE_FRAMEWORK, this::setMicroprofileFramework);
377383

384+
additionalProperties.put(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, microProfileGlobalExceptionMapper);
385+
additionalProperties.put(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, microProfileRegisterExceptionMapper);
386+
378387
convertPropertyToBooleanAndWriteBack(MICROPROFILE_MUTINY, this::setMicroprofileMutiny);
379388

380389
convertPropertyToStringAndWriteBack(MICROPROFILE_REST_CLIENT_VERSION, value->microprofileRestClientVersion=value);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import {{rootJavaEEPackage}}.validation.constraints.*;
2424
import {{rootJavaEEPackage}}.validation.Valid;
2525
{{/useBeanValidation}}
2626

27+
{{#microprofileRegisterExceptionMapper}}
2728
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
29+
{{/microprofileRegisterExceptionMapper}}
2830
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
2931

3032
{{#appName}}
@@ -41,7 +43,9 @@ import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
4143
{{^microprofileServer}}
4244
@RegisterRestClient{{#configKey}}(configKey="{{configKey}}"){{/configKey}}{{#configKeyFromClassName}}{{#operations}}(configKey="{{configKey}}"){{/operations}}{{/configKeyFromClassName}}
4345
{{/microprofileServer}}
46+
{{#microprofileRegisterExceptionMapper}}
4447
@RegisterProvider(ApiExceptionMapper.class)
48+
{{/microprofileRegisterExceptionMapper}}
4549
@Path("{{#useAnnotatedBasePath}}{{contextPath}}{{/useAnnotatedBasePath}}{{commonPath}}")
4650
public interface {{classname}} {
4751
{{#operations}}

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

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

44
import {{rootJavaEEPackage}}.ws.rs.core.MultivaluedMap;
55
import {{rootJavaEEPackage}}.ws.rs.core.Response;
6+
{{#microprofileGlobalExceptionMapper}}
67
import {{rootJavaEEPackage}}.ws.rs.ext.Provider;
8+
{{/microprofileGlobalExceptionMapper}}
79
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
810

11+
{{#microprofileGlobalExceptionMapper}}
912
@Provider
13+
{{/microprofileGlobalExceptionMapper}}
1014
public class ApiExceptionMapper
1115
implements ResponseExceptionMapper<ApiException> {
1216

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

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,112 @@ public void testMicroprofileCanHandleCookieParamsSingleRequest() throws Exceptio
127127
.assertInnerClass("GetCustomerRequest")
128128
.assertMethod("cookieParameter");
129129
}
130-
}
130+
131+
@Test
132+
public void testGeneratedApiHasApiExceptionMapperRegisteredWhenUsingDefaultConfiguration() throws Exception {
133+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
134+
output.deleteOnExit();
135+
136+
OpenAPI openAPI = new OpenAPIParser()
137+
.readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI();
138+
139+
codegen.setOutputDir(output.getAbsolutePath());
140+
141+
ClientOptInput input = new ClientOptInput()
142+
.openAPI(openAPI)
143+
.config(codegen);
144+
145+
List<File> files = new DefaultGenerator().opts(input).generate();
146+
147+
Map<String, File> filesMap = files.stream()
148+
.collect(Collectors.toMap(File::getName, Function.identity()));
149+
150+
validateJavaSourceFiles(files);
151+
152+
JavaFileAssert.assertThat(filesMap.get("DefaultApi.java"))
153+
.assertTypeAnnotations()
154+
.containsWithName("RegisterProvider")
155+
.containsWithNameAndAttributes("RegisterProvider", Map.of("value", "ApiExceptionMapper.class"));
156+
}
157+
158+
@Test
159+
public void testGeneratedApiDoesNotHaveApiExceptionMapperRegisteredWhenDisablingItInConfiguration() throws Exception {
160+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
161+
output.deleteOnExit();
162+
163+
OpenAPI openAPI = new OpenAPIParser()
164+
.readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI();
165+
166+
codegen.setOutputDir(output.getAbsolutePath());
167+
codegen.setMicroProfileRegisterExceptionMapper(false);
168+
169+
ClientOptInput input = new ClientOptInput()
170+
.openAPI(openAPI)
171+
.config(codegen);
172+
173+
List<File> files = new DefaultGenerator().opts(input).generate();
174+
175+
Map<String, File> filesMap = files.stream()
176+
.collect(Collectors.toMap(File::getName, Function.identity()));
177+
178+
validateJavaSourceFiles(files);
179+
180+
JavaFileAssert.assertThat(filesMap.get("DefaultApi.java"))
181+
.assertTypeAnnotations()
182+
.doesNotContainWithName("RegisterProvider");
183+
}
184+
185+
@Test
186+
public void testGeneratedApiExceptionMapperHasProviderAnnotationWhenUsingDefaultConfiguration() throws Exception {
187+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
188+
output.deleteOnExit();
189+
190+
OpenAPI openAPI = new OpenAPIParser()
191+
.readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI();
192+
193+
codegen.setOutputDir(output.getAbsolutePath());
194+
195+
ClientOptInput input = new ClientOptInput()
196+
.openAPI(openAPI)
197+
.config(codegen);
198+
199+
List<File> files = new DefaultGenerator().opts(input).generate();
200+
201+
Map<String, File> filesMap = files.stream()
202+
.collect(Collectors.toMap(File::getName, Function.identity()));
203+
204+
validateJavaSourceFiles(files);
205+
206+
JavaFileAssert.assertThat(filesMap.get("ApiExceptionMapper.java"))
207+
.assertTypeAnnotations()
208+
.containsWithName("Provider");
209+
}
210+
211+
@Test
212+
public void testGeneratedApiExceptionMapperDoesNotHaveProviderAnnotationWhenDisablingItInConfiguration() throws Exception {
213+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
214+
output.deleteOnExit();
215+
216+
OpenAPI openAPI = new OpenAPIParser()
217+
.readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI();
218+
219+
codegen.setOutputDir(output.getAbsolutePath());
220+
codegen.setMicroProfileGlobalExceptionMapper(false);
221+
222+
ClientOptInput input = new ClientOptInput()
223+
.openAPI(openAPI)
224+
.config(codegen);
225+
226+
List<File> files = new DefaultGenerator().opts(input).generate();
227+
228+
Map<String, File> filesMap = files.stream()
229+
.collect(Collectors.toMap(File::getName, Function.identity()));
230+
231+
validateJavaSourceFiles(files);
232+
233+
JavaFileAssert.assertThat(filesMap.get("ApiExceptionMapper.java"))
234+
.assertTypeAnnotations()
235+
.doesNotContainWithName("Provider");
236+
}
237+
}
238+

0 commit comments

Comments
 (0)