From 17f3f9c472fedca9b545c81ac2e9e2646735f42f Mon Sep 17 00:00:00 2001 From: Jeremie Bresson Date: Sat, 3 Mar 2018 19:35:05 +0100 Subject: [PATCH] Primitive datatype in Schema components Fix for https://github.com/swagger-api/swagger-codegen/issues/7754 --- .../languages/DefaultCodegenConfig.java | 82 +++++++++-------- .../codegen/languages/java/JavaModelTest.java | 91 ++++++++++++++++++- 2 files changed, 134 insertions(+), 39 deletions(-) diff --git a/src/main/java/io/swagger/codegen/languages/DefaultCodegenConfig.java b/src/main/java/io/swagger/codegen/languages/DefaultCodegenConfig.java index ae1f49621b..024a69c2c6 100644 --- a/src/main/java/io/swagger/codegen/languages/DefaultCodegenConfig.java +++ b/src/main/java/io/swagger/codegen/languages/DefaultCodegenConfig.java @@ -980,50 +980,55 @@ public String getSchemaType(Schema property) { return datatype; } - if (property instanceof StringSchema && "number".equals(property.getFormat())) { - datatype = "BigDecimal"; - } else if (property instanceof ByteArraySchema) { - datatype = "ByteArray"; - } else if (property instanceof BinarySchema) { - datatype = SchemaTypeUtil.BINARY_FORMAT; - } else if (property instanceof FileSchema) { - datatype = "file"; - } else if (property instanceof BooleanSchema) { - datatype = SchemaTypeUtil.BOOLEAN_TYPE; - } else if (property instanceof DateSchema) { - datatype = SchemaTypeUtil.DATE_FORMAT; - } else if (property instanceof DateTimeSchema) { - datatype = "DateTime"; - } else if (property instanceof NumberSchema) { - if(SchemaTypeUtil.FLOAT_FORMAT.equals(property.getFormat())) { - datatype = SchemaTypeUtil.FLOAT_FORMAT; - } else if(SchemaTypeUtil.DOUBLE_FORMAT.equals(property.getFormat())) { - datatype = SchemaTypeUtil.DOUBLE_FORMAT; + datatype = getTypeOfSchema(property); + return datatype; + } + + private static String getTypeOfSchema(Schema schema) { + if (schema instanceof StringSchema && "number".equals(schema.getFormat())) { + return "BigDecimal"; + } else if (schema instanceof ByteArraySchema) { + return "ByteArray"; + } else if (schema instanceof BinarySchema) { + return SchemaTypeUtil.BINARY_FORMAT; + } else if (schema instanceof FileSchema) { + return "file"; + } else if (schema instanceof BooleanSchema) { + return SchemaTypeUtil.BOOLEAN_TYPE; + } else if (schema instanceof DateSchema) { + return SchemaTypeUtil.DATE_FORMAT; + } else if (schema instanceof DateTimeSchema) { + return "DateTime"; + } else if (schema instanceof NumberSchema) { + if(SchemaTypeUtil.FLOAT_FORMAT.equals(schema.getFormat())) { + return SchemaTypeUtil.FLOAT_FORMAT; + } else if(SchemaTypeUtil.DOUBLE_FORMAT.equals(schema.getFormat())) { + return SchemaTypeUtil.DOUBLE_FORMAT; } else { - datatype = "BigDecimal"; + return "BigDecimal"; } - } else if (property instanceof IntegerSchema) { - if(SchemaTypeUtil.INTEGER64_FORMAT.equals(property.getFormat())) { - datatype = "long"; + } else if (schema instanceof IntegerSchema) { + if(SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat())) { + return "long"; } else { - datatype = property.getType(); - } - } else if (property instanceof MapSchema) { - datatype = "map"; - } else if ( property instanceof UUIDSchema) { - datatype = "UUID"; - } else if (property instanceof StringSchema) { - datatype = "string"; + return schema.getType(); + } + } else if (schema instanceof MapSchema) { + return "map"; + } else if ( schema instanceof UUIDSchema) { + return "UUID"; + } else if (schema instanceof StringSchema) { + return "string"; } else { - if (property != null) { - if (SchemaTypeUtil.OBJECT_TYPE.equals(property.getType()) && property.getAdditionalProperties() != null) { - datatype = "map"; + if (schema != null) { + if (SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && schema.getAdditionalProperties() != null) { + return "map"; } else { - datatype = property.getType(); + return schema.getType(); } } } - return datatype; + return null; } /** @@ -2886,8 +2891,9 @@ private static Map getAllAliases(Map allSchemas) for (Map.Entry entry : allSchemas.entrySet()) { String swaggerName = entry.getKey(); Schema schema = entry.getValue(); - if (schema.getType() != null && !schema.getType().equals("object") && schema.getEnum() == null) { - aliases.put(swaggerName, schema.getType()); + String schemaType = getTypeOfSchema(schema); + if (schemaType != null && !schemaType.equals("object") && schema.getEnum() == null) { + aliases.put(swaggerName, schemaType); } } return aliases; diff --git a/src/test/java/io/swagger/codegen/languages/java/JavaModelTest.java b/src/test/java/io/swagger/codegen/languages/java/JavaModelTest.java index 2504ca6cb0..61445533b5 100644 --- a/src/test/java/io/swagger/codegen/languages/java/JavaModelTest.java +++ b/src/test/java/io/swagger/codegen/languages/java/JavaModelTest.java @@ -16,6 +16,7 @@ import io.swagger.v3.oas.models.media.IntegerSchema; import io.swagger.v3.oas.models.media.MapSchema; import io.swagger.v3.oas.models.media.NumberSchema; +import io.swagger.v3.oas.models.media.ObjectSchema; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import io.swagger.v3.oas.models.media.XML; @@ -28,7 +29,9 @@ import org.testng.annotations.Test; import java.io.File; +import java.util.Collections; import java.util.List; +import java.util.Map; import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue; @@ -722,7 +725,7 @@ public void modelWithWrappedXmlTest() { } @Test(description = "convert a boolean parameter") - public void booleanParameterTest() { + public void booleanPropertyTest() { final BooleanSchema property = new BooleanSchema(); final DefaultCodegenConfig codegen = new JavaClientCodegen(); final CodegenProperty cp = codegen.fromProperty("property", property); @@ -736,6 +739,92 @@ public void booleanParameterTest() { Assert.assertEquals(cp.getter, "isProperty"); } + @Test(description = "convert an integer property") + public void integerPropertyTest() { + final IntegerSchema property = new IntegerSchema(); + final DefaultCodegenConfig codegen = new JavaClientCodegen(); + final CodegenProperty cp = codegen.fromProperty("property", property); + + Assert.assertEquals(cp.baseName, "property"); + Assert.assertEquals(cp.datatype, "Integer"); + Assert.assertEquals(cp.name, "property"); + Assert.assertEquals(cp.baseType, "Integer"); + Assert.assertTrue(getBooleanValue(cp, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME)); + Assert.assertTrue(getBooleanValue(cp, CodegenConstants.IS_INTEGER_EXT_NAME)); + Assert.assertFalse(getBooleanValue(cp, CodegenConstants.IS_LONG_EXT_NAME)); + Assert.assertEquals(cp.getter, "getProperty"); + } + + @Test(description = "convert a long property") + public void longPropertyTest() { + final IntegerSchema property = new IntegerSchema().format("int64"); + final DefaultCodegenConfig codegen = new JavaClientCodegen(); + final CodegenProperty cp = codegen.fromProperty("property", property); + + Assert.assertEquals(cp.baseName, "property"); + Assert.assertEquals(cp.datatype, "Long"); + Assert.assertEquals(cp.name, "property"); + Assert.assertEquals(cp.baseType, "Long"); + Assert.assertTrue(getBooleanValue(cp, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME)); + Assert.assertTrue(getBooleanValue(cp, CodegenConstants.IS_LONG_EXT_NAME)); + Assert.assertFalse(getBooleanValue(cp, CodegenConstants.IS_INTEGER_EXT_NAME)); + Assert.assertEquals(cp.getter, "getProperty"); + } + + @Test(description = "convert a long property in a referenced schema") + public void longPropertyInReferencedSchemaTest() { + final IntegerSchema longProperty = new IntegerSchema().format("int64"); + final Schema TestSchema = new ObjectSchema() + .addProperties("Long1", new Schema<>().$ref("#/components/schemas/LongProperty")) + .addProperties("Long2", new IntegerSchema().format("int64")); + final DefaultCodegenConfig codegen = new JavaClientCodegen(); + final Map allDefinitions = Collections.singletonMap("LongProperty", longProperty); + final CodegenModel cm = codegen.fromModel("test", TestSchema, allDefinitions); + + Assert.assertEquals(cm.vars.size(), 2); + + CodegenProperty cp1 = cm.vars.get(0); + Assert.assertEquals(cp1.baseName, "Long1"); + Assert.assertEquals(cp1.datatype, "Long"); + Assert.assertEquals(cp1.name, "long1"); + Assert.assertEquals(cp1.baseType, "Long"); + Assert.assertEquals(cp1.getter, "getLong1"); + + CodegenProperty cp2 = cm.vars.get(1); + Assert.assertEquals(cp2.baseName, "Long2"); + Assert.assertEquals(cp2.datatype, "Long"); + Assert.assertEquals(cp2.name, "long2"); + Assert.assertEquals(cp2.baseType, "Long"); + Assert.assertEquals(cp2.getter, "getLong2"); + } + + @Test(description = "convert am integer property in a referenced schema") + public void integerPropertyInReferencedSchemaTest() { + final IntegerSchema longProperty = new IntegerSchema().format("int32"); + final Schema TestSchema = new ObjectSchema() + .addProperties("Integer1", new Schema<>().$ref("#/components/schemas/IntegerProperty")) + .addProperties("Integer2", new IntegerSchema().format("int32")); + final DefaultCodegenConfig codegen = new JavaClientCodegen(); + final Map allDefinitions = Collections.singletonMap("IntegerProperty", longProperty); + final CodegenModel cm = codegen.fromModel("test", TestSchema, allDefinitions); + + Assert.assertEquals(cm.vars.size(), 2); + + CodegenProperty cp1 = cm.vars.get(0); + Assert.assertEquals(cp1.baseName, "Integer1"); + Assert.assertEquals(cp1.datatype, "Integer"); + Assert.assertEquals(cp1.name, "integer1"); + Assert.assertEquals(cp1.baseType, "Integer"); + Assert.assertEquals(cp1.getter, "getInteger1"); + + CodegenProperty cp2 = cm.vars.get(1); + Assert.assertEquals(cp2.baseName, "Integer2"); + Assert.assertEquals(cp2.datatype, "Integer"); + Assert.assertEquals(cp2.name, "integer2"); + Assert.assertEquals(cp2.baseType, "Integer"); + Assert.assertEquals(cp2.getter, "getInteger2"); + } + @Test(enabled = false, description = "disabled since templates have been moved.") public void generateModel() throws Exception { folder.create();