diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index 43767dc58d00..d5db9f49bfde 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -70,7 +70,6 @@ public class JavaClientCodegen extends AbstractJavaCodegen public static final String CASE_INSENSITIVE_RESPONSE_HEADERS = "caseInsensitiveResponseHeaders"; public static final String MICROPROFILE_FRAMEWORK = "microprofileFramework"; public static final String MICROPROFILE_MUTINY = "microprofileMutiny"; - public static final String MICROPROFILE_GLOBAL_EXCEPTION_MAPPER = "microprofileGlobalExceptionMapper"; public static final String MICROPROFILE_REGISTER_EXCEPTION_MAPPER = "microprofileRegisterExceptionMapper"; public static final String USE_ABSTRACTION_FOR_FILES = "useAbstractionForFiles"; public static final String DYNAMIC_OPERATIONS = "dynamicOperations"; @@ -128,8 +127,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen @Setter protected String microprofileFramework = MICROPROFILE_DEFAULT; @Setter protected String microprofileRestClientVersion = MICROPROFILE_REST_CLIENT_DEFAULT_VERSION; @Setter protected boolean microprofileMutiny = false; - @Setter protected boolean microProfileGlobalExceptionMapper = true; - @Setter protected boolean microProfileRegisterExceptionMapper = true; + @Setter protected boolean microProfileRegisterExceptionMapper = false; @Setter protected String configKey = null; @Setter(AccessLevel.PRIVATE) protected boolean configKeyFromClassName = false; @@ -240,8 +238,7 @@ public JavaClientCodegen() { cliOptions.add(CliOption.newBoolean(CASE_INSENSITIVE_RESPONSE_HEADERS, "Make API response's headers case-insensitive. Available on " + OKHTTP_GSON + ", " + JERSEY2 + " libraries")); cliOptions.add(CliOption.newString(MICROPROFILE_FRAMEWORK, "Framework for microprofile. Possible values \"kumuluzee\"")); cliOptions.add(CliOption.newString(MICROPROFILE_MUTINY, "Whether to use async types for microprofile (currently only Smallrye Mutiny is supported).")); - cliOptions.add(CliOption.newString(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "Should generated API Clients be annotated with @RegisterProvider(ApiExceptionMapper.class).").defaultValue("true")); - cliOptions.add(CliOption.newString(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, "Should ApiExceptionMapper be annotated with @Provider making it a global exception mapper").defaultValue("true")); + cliOptions.add(CliOption.newBoolean(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "Should generated API Clients be annotated with @RegisterProvider(ApiExceptionMapper.class).", this.microProfileRegisterExceptionMapper)); 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")); cliOptions.add(CliOption.newBoolean(DYNAMIC_OPERATIONS, "Generate operations dynamically at runtime from an OAS", this.dynamicOperations)); cliOptions.add(CliOption.newBoolean(SUPPORT_STREAMING, "Support streaming endpoint (beta)", this.supportStreaming)); @@ -363,6 +360,11 @@ public void processOpts() { this.jackson = !additionalProperties.containsKey(CodegenConstants.SERIALIZATION_LIBRARY) || SERIALIZATION_LIBRARY_JACKSON.equals(additionalProperties.get(CodegenConstants.SERIALIZATION_LIBRARY)); + // use runtime exceptions for microprofile + if (libMicroprofile) { + useRuntimeException = true; + } + convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, this::setUseOneOfDiscriminatorLookup); // RxJava @@ -399,11 +401,9 @@ public void processOpts() { } convertPropertyToStringAndWriteBack(MICROPROFILE_FRAMEWORK, this::setMicroprofileFramework); - convertPropertyToBooleanAndWriteBack(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, this::setMicroProfileGlobalExceptionMapper); convertPropertyToBooleanAndWriteBack(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, this::setMicroProfileRegisterExceptionMapper); additionalProperties.put(MICROPROFILE_REGISTER_EXCEPTION_MAPPER, microProfileRegisterExceptionMapper); - additionalProperties.put(MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, microProfileGlobalExceptionMapper); convertPropertyToBooleanAndWriteBack(MICROPROFILE_MUTINY, this::setMicroprofileMutiny); @@ -709,8 +709,19 @@ public void processOpts() { String pomTemplate = mpRestClientVersions.get(microprofileRestClientVersion).pomTemplate; supportingFiles.add(new SupportingFile(pomTemplate, "", "pom.xml")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); - supportingFiles.add(new SupportingFile("api_exception.mustache", apiExceptionFolder, "ApiException.java")); - supportingFiles.add(new SupportingFile("api_exception_mapper.mustache", apiExceptionFolder, "ApiExceptionMapper.java")); + + if (useRuntimeException) { + if (microProfileRegisterExceptionMapper) { + supportingFiles.add(new SupportingFile("api_exception.mustache", apiExceptionFolder, "ApiException.java")); + supportingFiles.add(new SupportingFile("api_exception_mapper.mustache", apiExceptionFolder, "ApiExceptionMapper.java")); + } + } else { + supportingFiles.add(new SupportingFile("api_exception.mustache", apiExceptionFolder, "ApiException.java")); + if (microProfileRegisterExceptionMapper) { + supportingFiles.add(new SupportingFile("api_exception_mapper.mustache", apiExceptionFolder, "ApiExceptionMapper.java")); + } + } + if (getSerializationLibrary() == null) { LOGGER.info("No serializationLibrary configured, using '{}' as fallback", SERIALIZATION_LIBRARY_JSONB); setSerializationLibrary(SERIALIZATION_LIBRARY_JSONB); @@ -803,7 +814,7 @@ public void processOpts() { additionalProperties.remove(SERIALIZATION_LIBRARY_JSONB); break; } - + if (isLibrary(FEIGN)) { additionalProperties.put("feign-okhttp", "true"); } else if (isLibrary(FEIGN_HC5)) { diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api.mustache index 5215f5b8cc0f..e0bc045e897e 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api.mustache @@ -25,7 +25,9 @@ import {{rootJavaEEPackage}}.validation.Valid; {{#microprofileRegisterExceptionMapper}} import org.eclipse.microprofile.rest.client.annotation.RegisterProvider; {{/microprofileRegisterExceptionMapper}} +{{^microprofileServer}} import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; +{{/microprofileServer}} {{#appName}} @@ -75,10 +77,10 @@ public interface {{classname}} { @Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }) {{/hasProduces}} {{^singleRequestParameter}} - {{^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{{/microprofileMutiny}}{{^microprofileMutiny}}void{{/microprofileMutiny}}{{/vendorExtensions.x-java-is-response-void}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}}, {{/-last}}{{/allParams}}) throws ApiException, ProcessingException; + {{^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{{/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; {{/singleRequestParameter}} {{#singleRequestParameter}} - {{^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{{/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; + {{^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{{/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; {{#hasNonBodyParams}} public class {{operationIdCamelCase}}Request { diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api_exception.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api_exception.mustache index 63a218675c4d..ace4806d63be 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api_exception.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api_exception.mustache @@ -2,10 +2,20 @@ package {{apiPackage}}; import {{rootJavaEEPackage}}.ws.rs.core.Response; +{{#useRuntimeException}} +import {{rootJavaEEPackage}}.ws.rs.WebApplicationException; +{{/useRuntimeException}} -public class ApiException extends{{#useRuntimeException}} RuntimeException {{/useRuntimeException}}{{^useRuntimeException}} Exception {{/useRuntimeException}}{ +public class ApiException extends{{#useRuntimeException}} WebApplicationException {{/useRuntimeException}}{{^useRuntimeException}} Exception {{/useRuntimeException}}{ private static final long serialVersionUID = 1L; +{{#useRuntimeException}} + + public ApiException(Response response) { + super(response); + } +{{/useRuntimeException}} +{{^useRuntimeException}} private Response response; public ApiException() { @@ -20,4 +30,5 @@ public class ApiException extends{{#useRuntimeException}} RuntimeException {{/us public Response getResponse() { return this.response; } +{{/useRuntimeException}} } diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api_exception_mapper.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api_exception_mapper.mustache index 2842b1c87a72..12988f5ed9db 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api_exception_mapper.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/api_exception_mapper.mustache @@ -3,14 +3,10 @@ package {{apiPackage}}; import {{rootJavaEEPackage}}.ws.rs.core.MultivaluedMap; import {{rootJavaEEPackage}}.ws.rs.core.Response; -{{#microprofileGlobalExceptionMapper}} import {{rootJavaEEPackage}}.ws.rs.ext.Provider; -{{/microprofileGlobalExceptionMapper}} import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; -{{#microprofileGlobalExceptionMapper}} @Provider -{{/microprofileGlobalExceptionMapper}} public class ApiExceptionMapper implements ResponseExceptionMapper { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java index ac6c08a65201..8c22d0b3883a 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java @@ -1486,8 +1486,13 @@ public void testDefaultMicroprofileRestClientVersion() { validateJavaSourceFiles(files); assertThat(files).contains(output.resolve("pom.xml").toFile()); + assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).doesNotExist(); + assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).doesNotExist(); assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content() - .contains("import javax."); + .contains("import javax.") + .doesNotContain("ApiException") + .contains("WebApplicationException"); + ; assertThat(output.resolve("pom.xml")).content() .contains( "2.0", @@ -1500,7 +1505,11 @@ public void testDefaultMicroprofileRestClientVersion() { public void testMicroprofileRestClientVersion_1_4_1() { final Path output = newTempFolder(); final CodegenConfigurator configurator = new CodegenConfigurator() - .setAdditionalProperties(Map.of(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "1.4.1")) + .setAdditionalProperties(Map.of( + JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "1.4.1", + JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "true", + USE_RUNTIME_EXCEPTION,"false" + )) .setGeneratorName(JAVA_GENERATOR) .setLibrary(JavaClientCodegen.MICROPROFILE) .setInputSpec("src/test/resources/3_0/petstore.yaml") @@ -1510,8 +1519,12 @@ public void testMicroprofileRestClientVersion_1_4_1() { validateJavaSourceFiles(files); assertThat(files).contains(output.resolve("pom.xml").toFile()); + assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).exists(); + assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).exists(); assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content() - .contains("import javax."); + .contains("import javax.") + .contains("ApiException") + .doesNotContain("WebApplicationException"); assertThat(output.resolve("pom.xml")).content() .contains( "1.4.1", @@ -1544,7 +1557,10 @@ public void testMicroprofileRestClientIncorrectVersion() { public void testMicroprofileRestClientVersion_3_0() { final Path output = newTempFolder(); final CodegenConfigurator configurator = new CodegenConfigurator() - .setAdditionalProperties(Map.of(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0")) + .setAdditionalProperties(Map.of( + JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0", + JavaClientCodegen.USE_RUNTIME_EXCEPTION, "false" + )) .setGeneratorName(JAVA_GENERATOR) .setLibrary(JavaClientCodegen.MICROPROFILE) .setInputSpec("src/test/resources/3_0/petstore.yaml") @@ -1554,8 +1570,12 @@ public void testMicroprofileRestClientVersion_3_0() { validateJavaSourceFiles(files); assertThat(files).contains(output.resolve("pom.xml").toFile()); + assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiException.java")).exists(); + assertThat(output.resolve("src/main/java/org/openapitools/client/api/ApiExceptionMapper.java")).doesNotExist(); assertThat(output.resolve("src/main/java/org/openapitools/client/api/PetApi.java")).content() - .contains("import jakarta."); + .contains("import jakarta.") + .contains("ApiException") + .doesNotContain("WebApplicationException"); assertThat(output.resolve("pom.xml")).content() .contains( "3.0", diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/microprofile/JavaMicroprofileServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/microprofile/JavaMicroprofileServerCodegenTest.java index 06cb837baa6d..d39269b7ce1b 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/microprofile/JavaMicroprofileServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/microprofile/JavaMicroprofileServerCodegenTest.java @@ -19,6 +19,7 @@ import java.util.function.Function; import java.util.stream.Collectors; +import static org.assertj.core.api.Assertions.assertThat; import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles; public class JavaMicroprofileServerCodegenTest { @@ -138,6 +139,7 @@ public void testGeneratedApiHasApiExceptionMapperRegisteredWhenUsingDefaultConfi .readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI(); codegen.setOutputDir(output.getAbsolutePath()); + codegen.additionalProperties().put(JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "true"); ClientOptInput input = new ClientOptInput() .openAPI(openAPI) @@ -165,7 +167,6 @@ public void testGeneratedApiDoesNotHaveApiExceptionMapperRegisteredWhenDisabling .readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI(); codegen.setOutputDir(output.getAbsolutePath()); - codegen.additionalProperties().put(JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "false"); ClientOptInput input = new ClientOptInput() .openAPI(openAPI) @@ -192,6 +193,7 @@ public void testGeneratedApiExceptionMapperHasProviderAnnotationWhenUsingDefault .readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI(); codegen.setOutputDir(output.getAbsolutePath()); + codegen.additionalProperties().put(JavaClientCodegen.MICROPROFILE_REGISTER_EXCEPTION_MAPPER, "true"); ClientOptInput input = new ClientOptInput() .openAPI(openAPI) @@ -218,7 +220,8 @@ public void testGeneratedApiExceptionMapperDoesNotHaveProviderAnnotationWhenDisa .readLocation("src/test/resources/bugs/microprofile_cookie.yaml", null, new ParseOptions()).getOpenAPI(); codegen.setOutputDir(output.getAbsolutePath()); - codegen.additionalProperties().put(JavaClientCodegen.MICROPROFILE_GLOBAL_EXCEPTION_MAPPER, "false"); + codegen.additionalProperties().put(JavaClientCodegen.USE_RUNTIME_EXCEPTION, "true"); + ClientOptInput input = new ClientOptInput() .openAPI(openAPI) @@ -231,9 +234,8 @@ public void testGeneratedApiExceptionMapperDoesNotHaveProviderAnnotationWhenDisa validateJavaSourceFiles(files); - JavaFileAssert.assertThat(filesMap.get("ApiExceptionMapper.java")) - .assertTypeAnnotations() - .doesNotContainWithName("Provider"); + assertThat(filesMap.get("ApiException.java")).isNull(); + assertThat(filesMap.get("ApiExceptionMapper.java")).isNull(); } }