From 0cb59fc4bc84d9f3990c4c9ae0b6290fd24ba221 Mon Sep 17 00:00:00 2001 From: Andrii Serkes <74911628+aserkes@users.noreply.github.com> Date: Wed, 20 Apr 2022 08:29:43 +0200 Subject: [PATCH] =?UTF-8?q?Allow=20selection=20of=20MP=20REST=20API=20vers?= =?UTF-8?q?ion=20for=20MicroProfile=20REST=20client=20g=E2=80=A6=20(#12043?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Allow selection of MP REST API version for MicroProfile REST client generation * fix typo in pom.xml * fix typo in pom.xml, update samples * add exception when incorrect MP Rest Client version is chosen --- docs/generators/java.md | 1 + .../codegen/languages/JavaClientCodegen.java | 50 ++++- .../Java/libraries/microprofile/api.mustache | 6 +- .../microprofile/api_exception.mustache | 2 +- .../api_exception_mapper.mustache | 6 +- .../microprofile/generatedAnnotation.mustache | 2 +- .../libraries/microprofile/model.mustache | 4 +- .../Java/libraries/microprofile/pojo.mustache | 34 +-- .../Java/libraries/microprofile/pom.mustache | 74 ++++--- .../libraries/microprofile/pom_3.0.mustache | 200 ++++++++++++++++++ .../codegen/java/JavaClientCodegenTest.java | 120 +++++++++++ .../java/microprofile-rest-client/pom.xml | 68 +++--- 12 files changed, 478 insertions(+), 89 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom_3.0.mustache diff --git a/docs/generators/java.md b/docs/generators/java.md index b52feda4d25b..65645070598b 100644 --- a/docs/generators/java.md +++ b/docs/generators/java.md @@ -56,6 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |licenseName|The name of the license| |Unlicense| |licenseUrl|The URL of the license| |http://unlicense.org| |microprofileFramework|Framework for microprofile. Possible values "kumuluzee"| |null| +|microprofileRestClientVersion|Version of MicroProfile Rest Client API.| |null| |modelPackage|package for generated models| |org.openapitools.client.model| |openApiNullable|Enable OpenAPI Jackson Nullable library| |true| |parcelableModel|Whether to generate models for Android that implement Parcelable with the okhttp-gson library.| |false| 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 6b52ed17027a..7cc112cf6a80 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 @@ -89,6 +89,9 @@ public class JavaClientCodegen extends AbstractJavaCodegen public static final String VERTX = "vertx"; public static final String MICROPROFILE = "microprofile"; public static final String APACHE = "apache-httpclient"; + public static final String MICROPROFILE_REST_CLIENT_VERSION = "microprofileRestClientVersion"; + public static final String MICROPROFILE_REST_CLIENT_DEFAULT_VERSION = "2.0"; + public static final String MICROPROFILE_REST_CLIENT_DEFAULT_ROOT_PACKAGE = "javax"; public static final String SERIALIZATION_LIBRARY_GSON = "gson"; public static final String SERIALIZATION_LIBRARY_JACKSON = "jackson"; @@ -122,6 +125,18 @@ public class JavaClientCodegen extends AbstractJavaCodegen protected String authFolder; protected String serializationLibrary = null; protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup + protected String rootJavaEEPackage; + protected Map mpRestClientVersions = new HashMap<>(); + + private static class MpRestClientVersion { + public final String rootPackage; + public final String pomTemplate; + + public MpRestClientVersion(String rootPackage, String pomTemplate) { + this.rootPackage = rootPackage; + this.pomTemplate = pomTemplate; + } + } public JavaClientCodegen() { super(); @@ -138,6 +153,7 @@ public JavaClientCodegen() { artifactId = "openapi-java-client"; apiPackage = "org.openapitools.client.api"; modelPackage = "org.openapitools.client.model"; + rootJavaEEPackage = MICROPROFILE_REST_CLIENT_DEFAULT_ROOT_PACKAGE; // cliOptions default redefinition need to be updated updateOption(CodegenConstants.INVOKER_PACKAGE, this.getInvokerPackage()); @@ -166,6 +182,7 @@ public JavaClientCodegen() { cliOptions.add(CliOption.newString(ERROR_OBJECT_TYPE, "Error Object type. (This option is for okhttp-gson-next-gen only)")); cliOptions.add(CliOption.newString(CONFIG_KEY, "Config key in @RegisterRestClient. Default to none. Only `microprofile` supports this option.")); cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC + " Only jersey2, jersey3, native, okhttp-gson support this option.")); + cliOptions.add(CliOption.newString(MICROPROFILE_REST_CLIENT_VERSION, "Version of MicroProfile Rest Client API.")); supportedLibraries.put(JERSEY1, "HTTP client: Jersey client 1.19.x. JSON processing: Jackson 2.9.x. Enable gzip request encoding using '-DuseGzipFeature=true'. IMPORTANT NOTE: jersey 1.x is no longer actively maintained so please upgrade to 'jersey3' or other HTTP libraries instead."); supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.9.x"); @@ -203,6 +220,13 @@ public JavaClientCodegen() { // and the discriminator mapping schemas in the OAS document. this.setLegacyDiscriminatorBehavior(false); + initMpRestClientVersionToRootPackage(); + } + + private void initMpRestClientVersionToRootPackage() { + mpRestClientVersions.put("1.4.1", new MpRestClientVersion("javax", "pom.mustache")); + mpRestClientVersions.put("2.0", new MpRestClientVersion("javax", "pom.mustache")); + mpRestClientVersions.put("3.0", new MpRestClientVersion("jakarta", "pom_3.0.mustache")); } @Override @@ -280,6 +304,28 @@ public void processOpts() { } additionalProperties.put(MICROPROFILE_FRAMEWORK, microprofileFramework); + if (!additionalProperties.containsKey(MICROPROFILE_REST_CLIENT_VERSION)) { + additionalProperties.put(MICROPROFILE_REST_CLIENT_VERSION, MICROPROFILE_REST_CLIENT_DEFAULT_VERSION); + } else { + String mpRestClientVersion = (String) additionalProperties.get(MICROPROFILE_REST_CLIENT_VERSION); + if (!mpRestClientVersions.containsKey(mpRestClientVersion)){ + throw new IllegalArgumentException( + String.format(Locale.ROOT, + "Version %s of MicroProfile Rest Client is not supported or incorrect. Supported versions are %s", + mpRestClientVersion, + String.join(", ", mpRestClientVersions.keySet()) + ) + ); + } + } + if (!additionalProperties.containsKey("rootJavaEEPackage")) { + String mpRestClientVersion = (String) additionalProperties.get(MICROPROFILE_REST_CLIENT_VERSION); + if (mpRestClientVersions.containsKey(mpRestClientVersion)) { + rootJavaEEPackage = mpRestClientVersions.get(mpRestClientVersion).rootPackage; + } + additionalProperties.put("rootJavaEEPackage", rootJavaEEPackage); + } + if (additionalProperties.containsKey(CONFIG_KEY)) { this.setConfigKey(additionalProperties.get(CONFIG_KEY).toString()); } @@ -542,7 +588,9 @@ public void processOpts() { } else if (MICROPROFILE.equals(getLibrary())) { supportingFiles.clear(); // Don't need extra files provided by Java Codegen String apiExceptionFolder = (sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar); - supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); + String mpRestClientVersion = (String) additionalProperties.get(MICROPROFILE_REST_CLIENT_VERSION); + String pomTemplate = mpRestClientVersions.get(mpRestClientVersion).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")); 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 7760b3bf61bc..49ad40447d9d 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 @@ -9,9 +9,9 @@ import java.io.OutputStream; import java.util.List; import java.util.Map; import java.util.Set; -import javax.ws.rs.*; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.MediaType; +import {{rootJavaEEPackage}}.ws.rs.*; +import {{rootJavaEEPackage}}.ws.rs.core.Response; +import {{rootJavaEEPackage}}.ws.rs.core.MediaType; {{^disableMultipart}} import org.apache.cxf.jaxrs.ext.multipart.*; {{/disableMultipart}} 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 fc5c5e5000ae..af0ecc7235af 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 @@ -1,7 +1,7 @@ {{>licenseInfo}} package {{apiPackage}}; -import javax.ws.rs.core.Response; +import {{rootJavaEEPackage}}.ws.rs.core.Response; public class ApiException extends Exception { 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 9c5988414cd3..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 @@ -1,9 +1,9 @@ {{>licenseInfo}} package {{apiPackage}}; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; +import {{rootJavaEEPackage}}.ws.rs.core.MultivaluedMap; +import {{rootJavaEEPackage}}.ws.rs.core.Response; +import {{rootJavaEEPackage}}.ws.rs.ext.Provider; import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; @Provider diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/generatedAnnotation.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/generatedAnnotation.mustache index 875d7b97afee..356a48872aae 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/generatedAnnotation.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/generatedAnnotation.mustache @@ -1 +1 @@ -@javax.annotation.Generated(value = "{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}}) \ No newline at end of file +@{{rootJavaEEPackage}}.annotation.Generated(value = "{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}}) \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/model.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/model.mustache index 5272ff094879..00a3c3db1315 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/model.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/model.mustache @@ -7,8 +7,8 @@ package {{package}}; import java.io.Serializable; {{/serializableModel}} {{#useBeanValidation}} -import javax.validation.constraints.*; -import javax.validation.Valid; +import {{rootJavaEEPackage}}.validation.constraints.*; +import {{rootJavaEEPackage}}.validation.Valid; {{/useBeanValidation}} {{#models}} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache index c7bbae535ca7..84c08fbb26cf 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache @@ -1,25 +1,25 @@ {{#withXml}} -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlType; -import javax.xml.bind.annotation.XmlEnum; -import javax.xml.bind.annotation.XmlEnumValue; +import {{rootJavaEEPackage}}.xml.bind.annotation.XmlElement; +import {{rootJavaEEPackage}}.xml.bind.annotation.XmlRootElement; +import {{rootJavaEEPackage}}.xml.bind.annotation.XmlAccessType; +import {{rootJavaEEPackage}}.xml.bind.annotation.XmlAccessorType; +import {{rootJavaEEPackage}}.xml.bind.annotation.XmlType; +import {{rootJavaEEPackage}}.xml.bind.annotation.XmlEnum; +import {{rootJavaEEPackage}}.xml.bind.annotation.XmlEnumValue; {{/withXml}} {{^withXml}} import java.lang.reflect.Type; -import javax.json.bind.annotation.JsonbTypeDeserializer; -import javax.json.bind.annotation.JsonbTypeSerializer; -import javax.json.bind.serializer.DeserializationContext; -import javax.json.bind.serializer.JsonbDeserializer; -import javax.json.bind.serializer.JsonbSerializer; -import javax.json.bind.serializer.SerializationContext; -import javax.json.stream.JsonGenerator; -import javax.json.stream.JsonParser; -import javax.json.bind.annotation.JsonbProperty; +import {{rootJavaEEPackage}}.json.bind.annotation.JsonbTypeDeserializer; +import {{rootJavaEEPackage}}.json.bind.annotation.JsonbTypeSerializer; +import {{rootJavaEEPackage}}.json.bind.serializer.DeserializationContext; +import {{rootJavaEEPackage}}.json.bind.serializer.JsonbDeserializer; +import {{rootJavaEEPackage}}.json.bind.serializer.JsonbSerializer; +import {{rootJavaEEPackage}}.json.bind.serializer.SerializationContext; +import {{rootJavaEEPackage}}.json.stream.JsonGenerator; +import {{rootJavaEEPackage}}.json.stream.JsonParser; +import {{rootJavaEEPackage}}.json.bind.annotation.JsonbProperty; {{#vendorExtensions.x-has-readonly-properties}} -import javax.json.bind.annotation.JsonbCreator; +import {{rootJavaEEPackage}}.json.bind.annotation.JsonbCreator; {{/vendorExtensions.x-has-readonly-properties}} {{/withXml}} diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom.mustache index cb9d96251def..d4199a18be3e 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom.mustache @@ -14,7 +14,7 @@ org.jboss.jandex jandex-maven-plugin - 1.1.0 + ${jandex.maven.plugin.version} make-index @@ -26,7 +26,7 @@ maven-failsafe-plugin - 2.6 + ${maven.failsafe.plugin.version} @@ -39,7 +39,7 @@ org.codehaus.mojo build-helper-maven-plugin - 1.9.1 + ${build.helper.maven.plugin.version} add-source @@ -61,7 +61,7 @@ junit junit - ${junit-version} + ${junit.version} test {{#useBeanValidation}} @@ -69,7 +69,7 @@ jakarta.validation jakarta.validation-api - ${beanvalidation-version} + ${beanvalidation.version} provided {{/useBeanValidation}} @@ -77,83 +77,83 @@ org.eclipse.microprofile.rest.client microprofile-rest-client-api - 1.4.1 + ${microprofile.rest.client.api.version} jakarta.ws.rs jakarta.ws.rs-api - ${jakarta.ws.rs-version} + ${jakarta.ws.rs.version} provided io.smallrye smallrye-rest-client - 1.2.1 + ${smallrye.rest.client.version} test io.smallrye smallrye-config - 1.3.5 + ${smallrye.config.version} test {{^disableMultipart}} org.apache.cxf cxf-rt-rs-extension-providers - 3.2.6 + ${cxf.rt.rs.extension.providers.version} {{/disableMultipart}} jakarta.json.bind jakarta.json.bind-api - ${jakarta.json.bind-version} + ${jakarta.json.bind.version} jakarta.json jakarta.json-api - ${jakarta.json-version} + ${jakarta.json.version} jakarta.xml.bind jakarta.xml.bind-api - ${jakarta.xml.bind-version} + ${jakarta.xml.bind.version} com.sun.xml.bind jaxb-core - 2.2.11 + ${jaxb.core.version} com.sun.xml.bind jaxb-impl - 2.2.11 + ${jaxb.impl.version} jakarta.activation jakarta.activation-api - ${jakarta.activation-version} + ${jakarta.activation.version} com.fasterxml.jackson.datatype jackson-datatype-jsr310 - ${jackson-jaxrs-version} + ${jackson.jaxrs.version} {{#useBeanValidationFeature}} org.hibernate hibernate-validator - 5.2.2.Final + ${hibernate.validator.version} {{/useBeanValidationFeature}} jakarta.annotation jakarta.annotation-api - ${jakarta-annotation-version} + ${jakarta.annotation.version} provided @@ -170,21 +170,31 @@ 1.8 ${java.version} ${java.version} - 1.5.18 - 9.2.9.v20150224 - 4.13.2 - 1.2.10 + 1.5.18 + 9.2.9.v20150224 + 4.13.2 + 1.2.10 {{#useBeanValidation}} - 2.0.2 + 2.0.2 {{/useBeanValidation}} - 3.2.7 - 2.9.7 - 1.2.2 - 1.3.5 - 1.0.2 - 1.1.6 - 2.1.6 - 2.3.3 + 3.2.7 + 2.9.7 + 1.2.2 + 1.3.5 + 1.0.2 + 1.1.6 + 2.1.6 + 2.3.3 + {{microprofileRestClientVersion}} + 1.2.1 + 1.3.5 + 3.2.6 + 2.2.11 + 2.2.11 + 5.2.2.Final + 1.1.0 + 2.6 + 1.9.1 UTF-8 diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom_3.0.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom_3.0.mustache new file mode 100644 index 000000000000..f2795bf32bb1 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pom_3.0.mustache @@ -0,0 +1,200 @@ + + 4.0.0 + {{groupId}} + {{artifactId}} + jar + {{artifactId}} + {{#appDescription}} + {{.}} + {{/appDescription}} + {{artifactVersion}} + + src/main/java + + + org.jboss.jandex + jandex-maven-plugin + ${jandex.maven.plugin.version} + + + make-index + + jandex + + + + + + maven-failsafe-plugin + ${maven.failsafe.plugin.version} + + + + integration-test + verify + + + + + + org.codehaus.mojo + build-helper-maven-plugin + ${build.helper.maven.plugin.version} + + + add-source + generate-sources + + add-source + + + + src/gen/java + + + + + + + + + + junit + junit + ${junit.version} + test + +{{#useBeanValidation}} + + + jakarta.validation + jakarta.validation-api + ${beanvalidation.version} + provided + +{{/useBeanValidation}} + + + org.eclipse.microprofile.rest.client + microprofile-rest-client-api + ${microprofile.rest.client.api.version} + + + + + jakarta.ws.rs + jakarta.ws.rs-api + ${jakarta.ws.rs.version} + provided + + + + org.glassfish.jersey.ext.microprofile + jersey-mp-rest-client + ${jersey.mp.rest.client.version} + test + + + org.apache.geronimo.config + geronimo-config-impl + ${geronimo.config.impl.version} + test + + + {{^disableMultipart}} + + org.apache.cxf + cxf-rt-rs-extension-providers + ${cxf.rt.rs.extension.providers.version} + + {{/disableMultipart}} + + jakarta.json.bind + jakarta.json.bind-api + ${jakarta.json.bind.version} + + + jakarta.json + jakarta.json-api + ${jakarta.json.version} + + + jakarta.xml.bind + jakarta.xml.bind-api + ${jakarta.xml.bind.version} + + + com.sun.xml.bind + jaxb-core + ${jaxb.core.version} + + + com.sun.xml.bind + jaxb-impl + ${jaxb.impl.version} + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation.version} + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + ${jackson.jaxrs.version} + +{{#useBeanValidationFeature}} + + org.hibernate + hibernate-validator + ${hibernate.validator.version} + +{{/useBeanValidationFeature}} + + jakarta.annotation + jakarta.annotation-api + ${jakarta.annotation.version} + provided + + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + true + + + + + 11 + ${java.version} + ${java.version} + 1.5.18 + 9.2.9.v20150224 + 4.13.2 + 1.2.10 +{{#useBeanValidation}} + 3.0.1 +{{/useBeanValidation}} + 3.2.7 + 2.13.2 + 2.1.0 + 2.0.0 + 2.0.0 + 2.0.1 + 3.0.0 + 3.0.1 + {{microprofileRestClientVersion}} + 3.0.4 + 1.2.3 + 3.5.1 + 3.0.2 + 3.0.2 + 7.0.4.Final + 1.1.0 + 2.6 + 1.9.1 + UTF-8 + + 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 ad00fb1281da..f3c64cfd172f 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 @@ -20,6 +20,7 @@ import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; import java.io.File; import java.io.IOException; @@ -1389,6 +1390,125 @@ public void testExtraAnnotationsApache() throws IOException { testExtraAnnotations(JavaClientCodegen.APACHE); } + @Test + public void testDefaultMicroprofileRestClientVersion() throws Exception { + File output = Files.createTempDirectory("test").toFile(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("java") + .setLibrary(JavaClientCodegen.MICROPROFILE) + .setInputSpec("src/test/resources/3_0/petstore.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(clientOptInput).generate(); + + TestUtils.ensureContainsFile(files, output, "pom.xml"); + + validateJavaSourceFiles(files); + + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "2.0"); + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "1.2.1"); + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "1.8"); + TestUtils.assertFileContains(Paths.get(output + "/src/main/java/org/openapitools/client/api/PetApi.java"), + "import javax."); + + output.deleteOnExit(); + } + + @Test + public void testMicroprofileRestClientVersion_1_4_1() throws Exception { + Map properties = new HashMap<>(); + properties.put(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "1.4.1"); + + File output = Files.createTempDirectory("test").toFile(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setAdditionalProperties(properties) + .setGeneratorName("java") + .setLibrary(JavaClientCodegen.MICROPROFILE) + .setInputSpec("src/test/resources/3_0/petstore.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(clientOptInput).generate(); + + TestUtils.ensureContainsFile(files, output, "pom.xml"); + + validateJavaSourceFiles(files); + + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "1.4.1"); + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "1.2.1"); + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "1.8"); + TestUtils.assertFileContains(Paths.get(output + "/src/main/java/org/openapitools/client/api/PetApi.java"), + "import javax."); + + output.deleteOnExit(); + } + + @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Version incorrectVersion of MicroProfile Rest Client is not supported or incorrect. Supported versions are 1.4.1, 2.0, 3.0") + public void testMicroprofileRestClientIncorrectVersion() throws Exception { + Map properties = new HashMap<>(); + properties.put(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "incorrectVersion"); + + File output = Files.createTempDirectory("test").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setAdditionalProperties(properties) + .setGeneratorName("java") + .setLibrary(JavaClientCodegen.MICROPROFILE) + .setInputSpec("src/test/resources/3_0/petstore.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(); + generator.opts(clientOptInput).generate(); + fail("Expected an exception that did not occur"); + } + + @Test + public void testMicroprofileRestClientVersion_3_0() throws Exception { + Map properties = new HashMap<>(); + properties.put(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0"); + + File output = Files.createTempDirectory("test").toFile(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setAdditionalProperties(properties) + .setGeneratorName("java") + .setLibrary(JavaClientCodegen.MICROPROFILE) + .setInputSpec("src/test/resources/3_0/petstore.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(clientOptInput).generate(); + + TestUtils.ensureContainsFile(files, output, "pom.xml"); + + validateJavaSourceFiles(files); + + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "3.0"); + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "3.0.4"); + TestUtils.assertFileContains(Paths.get(output + "/pom.xml"), + "11"); + TestUtils.assertFileContains(Paths.get(output + "/src/main/java/org/openapitools/client/api/PetApi.java"), + "import jakarta."); + + output.deleteOnExit(); + } + public void testExtraAnnotations(String library) throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); diff --git a/samples/client/petstore/java/microprofile-rest-client/pom.xml b/samples/client/petstore/java/microprofile-rest-client/pom.xml index 992fd5ba9bb3..968f176c15f8 100644 --- a/samples/client/petstore/java/microprofile-rest-client/pom.xml +++ b/samples/client/petstore/java/microprofile-rest-client/pom.xml @@ -12,7 +12,7 @@ org.jboss.jandex jandex-maven-plugin - 1.1.0 + ${jandex.maven.plugin.version} make-index @@ -24,7 +24,7 @@ maven-failsafe-plugin - 2.6 + ${maven.failsafe.plugin.version} @@ -37,7 +37,7 @@ org.codehaus.mojo build-helper-maven-plugin - 1.9.1 + ${build.helper.maven.plugin.version} add-source @@ -59,81 +59,81 @@ junit junit - ${junit-version} + ${junit.version} test org.eclipse.microprofile.rest.client microprofile-rest-client-api - 1.4.1 + ${microprofile.rest.client.api.version} jakarta.ws.rs jakarta.ws.rs-api - ${jakarta.ws.rs-version} + ${jakarta.ws.rs.version} provided io.smallrye smallrye-rest-client - 1.2.1 + ${smallrye.rest.client.version} test io.smallrye smallrye-config - 1.3.5 + ${smallrye.config.version} test org.apache.cxf cxf-rt-rs-extension-providers - 3.2.6 + ${cxf.rt.rs.extension.providers.version} jakarta.json.bind jakarta.json.bind-api - ${jakarta.json.bind-version} + ${jakarta.json.bind.version} jakarta.json jakarta.json-api - ${jakarta.json-version} + ${jakarta.json.version} jakarta.xml.bind jakarta.xml.bind-api - ${jakarta.xml.bind-version} + ${jakarta.xml.bind.version} com.sun.xml.bind jaxb-core - 2.2.11 + ${jaxb.core.version} com.sun.xml.bind jaxb-impl - 2.2.11 + ${jaxb.impl.version} jakarta.activation jakarta.activation-api - ${jakarta.activation-version} + ${jakarta.activation.version} com.fasterxml.jackson.datatype jackson-datatype-jsr310 - ${jackson-jaxrs-version} + ${jackson.jaxrs.version} jakarta.annotation jakarta.annotation-api - ${jakarta-annotation-version} + ${jakarta.annotation.version} provided @@ -150,18 +150,28 @@ 1.8 ${java.version} ${java.version} - 1.5.18 - 9.2.9.v20150224 - 4.13.2 - 1.2.10 - 3.2.7 - 2.9.7 - 1.2.2 - 1.3.5 - 1.0.2 - 1.1.6 - 2.1.6 - 2.3.3 + 1.5.18 + 9.2.9.v20150224 + 4.13.2 + 1.2.10 + 3.2.7 + 2.9.7 + 1.2.2 + 1.3.5 + 1.0.2 + 1.1.6 + 2.1.6 + 2.3.3 + 2.0 + 1.2.1 + 1.3.5 + 3.2.6 + 2.2.11 + 2.2.11 + 5.2.2.Final + 1.1.0 + 2.6 + 1.9.1 UTF-8