Skip to content

Commit c9d86e5

Browse files
JacobOJbdyjcj
authored andcommitted
[java][Microprofile] add config options to disable usage of ApiExceptionMapper
1 parent 5cef080 commit c9d86e5

File tree

6 files changed

+139
-1
lines changed

6 files changed

+139
-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: 17 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.newString(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "Should generated API Clients be annotated with @RegisterProvider(ApiExceptionMapper.class).").defaultValue("true"));
234+
cliOptions.add(CliOption.newString(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, "Should ApiExceptionMapper be annotated with @Provider making it a global exception mapper").defaultValue("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,17 @@ public void processOpts() {
375381
}
376382
convertPropertyToStringAndWriteBack(MICROPROFILE_FRAMEWORK, this::setMicroprofileFramework);
377383

384+
convertPropertyToBooleanAndWriteBack(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, this::setMicroProfileGlobalExceptionMapper);
385+
convertPropertyToBooleanAndWriteBack(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, this::setMicroProfileRegisterExceptionMapper);
386+
387+
if (!additionalProperties.containsKey(MICROPROFILE_REGISTER_EXCEPTION_MAPPER)) {
388+
additionalProperties.put(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, true);
389+
}
390+
391+
if (!additionalProperties.containsKey(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER)) {
392+
additionalProperties.put(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, true);
393+
}
394+
378395
convertPropertyToBooleanAndWriteBack(MICROPROFILE_MUTINY, this::setMicroprofileMutiny);
379396

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

0 commit comments

Comments
 (0)