From f9af3c27527c147c5dea937818d306bb0f350ab8 Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 8 Nov 2019 07:38:05 +0100 Subject: [PATCH] [kotlin] [bugfix] [maven-plugin]: prevent ClassCastException with boolean config options (#4361) * fix: prevent classcast exception during execution of openapi-generator-maven-plugin. * style: revert styling to openapi defaults * test: unit test coverage for handling boolean config options * fix: replace option value with boolean, if it is a string literal boolean * style: use data type long * test: add maven testfile kotlin.xml to travis build * test: runnable maven test of the kotlin generator --- .travis.yml | 1 + .../examples/kotlin.xml | 237 ++++++++++++++++++ .../languages/AbstractKotlinCodegen.java | 20 +- .../kotlin/AbstractKotlinCodegenTest.java | 47 +++- 4 files changed, 298 insertions(+), 7 deletions(-) create mode 100644 modules/openapi-generator-maven-plugin/examples/kotlin.xml diff --git a/.travis.yml b/.travis.yml index 2e034e8a7c8..cc0654536bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -138,6 +138,7 @@ script: # test maven plugin - mvn clean compile -f modules/openapi-generator-maven-plugin/examples/java-client.xml - mvn clean compile -f modules/openapi-generator-maven-plugin/examples/multi-module/pom.xml + - mvn clean compile -f modules/openapi-generator-maven-plugin/examples/kotlin.xml # test gradle plugin - (cd modules/openapi-generator-gradle-plugin/samples/local-spec && ./gradlew buildGoSdk) - (cd modules/openapi-generator-gradle-plugin/samples/local-spec && ./gradlew openApiGenerate) diff --git a/modules/openapi-generator-maven-plugin/examples/kotlin.xml b/modules/openapi-generator-maven-plugin/examples/kotlin.xml new file mode 100644 index 00000000000..83b54be3dab --- /dev/null +++ b/modules/openapi-generator-maven-plugin/examples/kotlin.xml @@ -0,0 +1,237 @@ + + 4.0.0 + org.openapitools + sample-project + jar + 1.0-SNAPSHOT + sample-project + http://maven.apache.org + + + + + + org.openapitools + openapi-generator-maven-plugin + + 4.2.1-SNAPSHOT + + + + default + + generate + + + + ${project.basedir}/swagger.yaml + + + kotlin + + + + + + true + + + + + + kotlin + generate-sources + + generate + + + + https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml + + + + kotlin + + + + + + true + + + ${project.build.directory}/generated-sources/kotlin + kotlintest.org.openapitools.client.api + kotlintest.org.openapitools.client.model + kotlintest.org.openapitools.client + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + none + + + + kotlin-maven-plugin + org.jetbrains.kotlin + + ${kotlinJvmTarget} + + + + + compile + + compile + + + + ${project.build.directory}/generated-sources/kotlin/src/main/kotlin + + + + + + + + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + + + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + + + + + + io.swagger + swagger-annotations + ${swagger-annotations-version} + + + + + + + org.glassfish.jersey.core + jersey-client + ${jersey-version} + + + org.glassfish.jersey.media + jersey-media-multipart + ${jersey-version} + + + org.glassfish.jersey.media + jersey-media-json-jackson + ${jersey-version} + + + + + com.google.code.findbugs + jsr305 + 3.0.2 + + + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-base + ${jackson-version} + + + com.fasterxml.jackson.core + jackson-core + ${jackson-version} + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson-version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson-version} + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + ${jackson-version} + + + org.openapitools + jackson-databind-nullable + ${jackson-databind-nullable-version} + + + + + com.fasterxml.jackson.datatype + jackson-datatype-joda + ${jackson-version} + + + joda-time + joda-time + ${jodatime-version} + + + + + com.brsanthu + migbase64 + ${migbase64.version} + + + com.squareup.moshi + moshi-kotlin + ${moshi-kotlin.version} + + + com.squareup.moshi + moshi-adapters + ${moshi-kotlin.version} + + + com.squareup.okhttp3 + okhttp + 4.2.2 + + + + + 1.5.8 + 2.27 + 2.8.9 + 0.2.0 + 2.7 + 1.0.0 + 4.8.1 + 1.3.50 + 1.8 + 1.8.0 + 2.2 + + diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java index 57c45a76fb0..b861a390cf7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java @@ -434,19 +434,19 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co } if (additionalProperties.containsKey(CodegenConstants.SERIALIZABLE_MODEL)) { - this.setSerializableModel(Boolean.valueOf((String) additionalProperties.get(CodegenConstants.SERIALIZABLE_MODEL))); + this.setSerializableModel(getBooleanOption(CodegenConstants.SERIALIZABLE_MODEL)); } else { additionalProperties.put(CodegenConstants.SERIALIZABLE_MODEL, serializableModel); } if (additionalProperties.containsKey(CodegenConstants.PARCELIZE_MODELS)) { - this.setParcelizeModels(Boolean.valueOf((String) additionalProperties.get(CodegenConstants.PARCELIZE_MODELS))); + this.setParcelizeModels(getBooleanOption(CodegenConstants.PARCELIZE_MODELS)); } else { additionalProperties.put(CodegenConstants.PARCELIZE_MODELS, parcelizeModels); } if (additionalProperties.containsKey(CodegenConstants.NON_PUBLIC_API)) { - this.setNonPublicApi(Boolean.valueOf((String) additionalProperties.get(CodegenConstants.NON_PUBLIC_API))); + this.setNonPublicApi(getBooleanOption(CodegenConstants.NON_PUBLIC_API)); } else { additionalProperties.put(CodegenConstants.NON_PUBLIC_API, nonPublicApi); } @@ -458,6 +458,18 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co additionalProperties.put("modelDocPath", modelDocPath); } + private boolean getBooleanOption(String key) { + final Object booleanValue = additionalProperties.get(key); + Boolean result = Boolean.FALSE; + if (booleanValue instanceof Boolean) { + result = (Boolean) booleanValue; + } else if (booleanValue instanceof String) { + result = Boolean.parseBoolean((String) booleanValue); + } + additionalProperties.put(key, result); + return result; + } + public void setArtifactId(String artifactId) { this.artifactId = artifactId; } @@ -501,7 +513,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co public void setSerializableModel(boolean serializableModel) { this.serializableModel = serializableModel; } - + public boolean nonPublicApi() { return nonPublicApi; } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java index ff23d307d90..90c8acff528 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/AbstractKotlinCodegenTest.java @@ -1,5 +1,6 @@ package org.openapitools.codegen.kotlin; +import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenType; import org.openapitools.codegen.languages.AbstractKotlinCodegen; import org.testng.Assert; @@ -53,7 +54,7 @@ public class AbstractKotlinCodegenTest { } @Test - public void toEnumValue(){ + public void toEnumValue() { assertEquals(codegen.toEnumValue("1", "kotlin.Int"), "1"); assertEquals(codegen.toEnumValue("1", "kotlin.Double"), "1.0"); assertEquals(codegen.toEnumValue("1.3", "kotlin.Double"), "1.3"); @@ -81,14 +82,14 @@ public class AbstractKotlinCodegenTest { } @Test - public void isDataTypeString(){ + public void isDataTypeString() { assertFalse(codegen.isDataTypeString("kotlin.Int")); assertTrue(codegen.isDataTypeString("kotlin.String")); assertTrue(codegen.isDataTypeString("String")); } @Test - public void toModelNameShouldUseProvidedMapping() { + public void toModelNameShouldUseProvidedMapping() { codegen.importMapping().put("json_myclass", "com.test.MyClass"); assertEquals("com.test.MyClass", codegen.toModelName("json_myclass")); } @@ -155,5 +156,45 @@ public class AbstractKotlinCodegenTest { Assert.assertEquals(codegen.apiTestFileFolder(), "/User/open/api/tools/test/folder/org/openapitools/codegen/api".replace('/', File.separatorChar)); } + @Test + public void processOptsBooleanTrueFromString() { + codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, "true"); + codegen.processOpts(); + Assert.assertTrue((boolean) codegen.additionalProperties().get(CodegenConstants.SERIALIZABLE_MODEL)); + } + @Test + public void processOptsBooleanTrueFromBoolean() { + codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, true); + codegen.processOpts(); + Assert.assertTrue((boolean) codegen.additionalProperties().get(CodegenConstants.SERIALIZABLE_MODEL)); + } + + @Test + public void processOptsBooleanFalseFromString() { + codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, "false"); + codegen.processOpts(); + Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SERIALIZABLE_MODEL)); + } + + @Test + public void processOptsBooleanFalseFromBoolean() { + codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, false); + codegen.processOpts(); + Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SERIALIZABLE_MODEL)); + } + + @Test + public void processOptsBooleanFalseFromGarbage() { + codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, "blibb"); + codegen.processOpts(); + Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SERIALIZABLE_MODEL)); + } + + @Test + public void processOptsBooleanFalseFromNumeric() { + codegen.additionalProperties().put(CodegenConstants.SERIALIZABLE_MODEL, 42L); + codegen.processOpts(); + Assert.assertFalse((boolean) codegen.additionalProperties().get(CodegenConstants.SERIALIZABLE_MODEL)); + } } \ No newline at end of file