Skip to content

Commit

Permalink
Primitive datatype in Schema components
Browse files Browse the repository at this point in the history
  • Loading branch information
jmini committed Mar 3, 2018
1 parent 66fd523 commit 17f3f9c
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -2886,8 +2891,9 @@ private static Map<String, String> getAllAliases(Map<String, Schema> allSchemas)
for (Map.Entry<String, Schema> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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<String, Schema> 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<String, Schema> 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();
Expand Down

0 comments on commit 17f3f9c

Please # to comment.