From b44f6c302aff918df7b80280717ce9f0ee4e810c Mon Sep 17 00:00:00 2001 From: steco Date: Wed, 3 Jul 2019 02:12:38 +0100 Subject: [PATCH] Update DefaultCodeGen to allow additional primitive types (#2799) * Update DefaultCodeGen to allow additional primitive types If a string field is specified with a format which is also defined using --typeMappings, it will be treated as a primitive type * Fixed typo in android-petstore-httpclient.bat --- bin/windows/android-petstore-httpclient.bat | 2 +- .../java/org/openapitools/codegen/DefaultCodegen.java | 11 +++++++++-- .../org/openapitools/codegen/DefaultCodegenTest.java | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/bin/windows/android-petstore-httpclient.bat b/bin/windows/android-petstore-httpclient.bat index aa94d719fc7..181ec906a49 100755 --- a/bin/windows/android-petstore-httpclient.bat +++ b/bin/windows/android-petstore-httpclient.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties -set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g android -o samples\client\petstore\android\httpclient-Dlibrary=httpclient +set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g android -o samples\client\petstore\android\httpclient -Dlibrary=httpclient java %JAVA_OPTS% -jar %executable% %ags% diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 507a4d1d59f..811b49c02a6 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -1475,7 +1475,7 @@ public class DefaultCodegen implements CodegenConfig { * @param schema * @return type */ - private static String getPrimitiveType(Schema schema) { + private String getPrimitiveType(Schema schema) { if (schema == null) { throw new RuntimeException("schema cannot be null in getPrimitiveType"); } else if (ModelUtils.isStringSchema(schema) && "number".equals(schema.getFormat())) { @@ -1519,6 +1519,13 @@ public class DefaultCodegen implements CodegenConfig { } else if (ModelUtils.isURISchema(schema)) { return "URI"; } else if (ModelUtils.isStringSchema(schema)) { + if(typeMapping.containsKey(schema.getFormat())) { + // If the format matches a typeMapping (supplied with the --typeMappings flag) + // then treat the format as a primitive type. + // This allows the typeMapping flag to add a new custom type which can then + // be used in the format field. + return schema.getFormat(); + } return "string"; } else if (ModelUtils.isFreeFormObject(schema)) { return "object"; @@ -3668,7 +3675,7 @@ public class DefaultCodegen implements CodegenConfig { * @param schemas The complete set of model definitions (schemas). * @return A mapping from model name to type alias */ - static Map getAllAliases(Map schemas) { + Map getAllAliases(Map schemas) { if (schemas == null || schemas.isEmpty()) { return new HashMap<>(); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index b8b3a717905..8c2e2aabc63 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -189,6 +189,7 @@ public class DefaultCodegenTest { @Test public void testArraySchemaIsNotIncluedInAliases() throws Exception { + final DefaultCodegen codegen = new DefaultCodegen(); Map schemas = new HashMap() { { put("ArraySchemaTest", new ArraySchema()); @@ -196,7 +197,7 @@ public class DefaultCodegenTest { }; - Map aliases = DefaultCodegen.getAllAliases(schemas); + Map aliases = codegen.getAllAliases(schemas); Assert.assertEquals(aliases.size(), 0); }