Skip to content

Commit 4d45da2

Browse files
committed
Merge branch 'master' into kotlin
2 parents 987b919 + cc19d1c commit 4d45da2

File tree

8 files changed

+202
-53
lines changed

8 files changed

+202
-53
lines changed

src/main/java/io/swagger/codegen/languages/DefaultCodegenConfig.java

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -979,50 +979,55 @@ public String getSchemaType(Schema property) {
979979
return datatype;
980980
}
981981

982-
if (property instanceof StringSchema && "number".equals(property.getFormat())) {
983-
datatype = "BigDecimal";
984-
} else if (property instanceof ByteArraySchema) {
985-
datatype = "ByteArray";
986-
} else if (property instanceof BinarySchema) {
987-
datatype = SchemaTypeUtil.BINARY_FORMAT;
988-
} else if (property instanceof FileSchema) {
989-
datatype = "file";
990-
} else if (property instanceof BooleanSchema) {
991-
datatype = SchemaTypeUtil.BOOLEAN_TYPE;
992-
} else if (property instanceof DateSchema) {
993-
datatype = SchemaTypeUtil.DATE_FORMAT;
994-
} else if (property instanceof DateTimeSchema) {
995-
datatype = "DateTime";
996-
} else if (property instanceof NumberSchema) {
997-
if(SchemaTypeUtil.FLOAT_FORMAT.equals(property.getFormat())) {
998-
datatype = SchemaTypeUtil.FLOAT_FORMAT;
999-
} else if(SchemaTypeUtil.DOUBLE_FORMAT.equals(property.getFormat())) {
1000-
datatype = SchemaTypeUtil.DOUBLE_FORMAT;
982+
datatype = getTypeOfSchema(property);
983+
return datatype;
984+
}
985+
986+
private static String getTypeOfSchema(Schema schema) {
987+
if (schema instanceof StringSchema && "number".equals(schema.getFormat())) {
988+
return "BigDecimal";
989+
} else if (schema instanceof ByteArraySchema) {
990+
return "ByteArray";
991+
} else if (schema instanceof BinarySchema) {
992+
return SchemaTypeUtil.BINARY_FORMAT;
993+
} else if (schema instanceof FileSchema) {
994+
return "file";
995+
} else if (schema instanceof BooleanSchema) {
996+
return SchemaTypeUtil.BOOLEAN_TYPE;
997+
} else if (schema instanceof DateSchema) {
998+
return SchemaTypeUtil.DATE_FORMAT;
999+
} else if (schema instanceof DateTimeSchema) {
1000+
return "DateTime";
1001+
} else if (schema instanceof NumberSchema) {
1002+
if(SchemaTypeUtil.FLOAT_FORMAT.equals(schema.getFormat())) {
1003+
return SchemaTypeUtil.FLOAT_FORMAT;
1004+
} else if(SchemaTypeUtil.DOUBLE_FORMAT.equals(schema.getFormat())) {
1005+
return SchemaTypeUtil.DOUBLE_FORMAT;
10011006
} else {
1002-
datatype = "BigDecimal";
1007+
return "BigDecimal";
10031008
}
1004-
} else if (property instanceof IntegerSchema) {
1005-
if(SchemaTypeUtil.INTEGER64_FORMAT.equals(property.getFormat())) {
1006-
datatype = "long";
1009+
} else if (schema instanceof IntegerSchema) {
1010+
if(SchemaTypeUtil.INTEGER64_FORMAT.equals(schema.getFormat())) {
1011+
return "long";
10071012
} else {
1008-
datatype = property.getType();
1009-
}
1010-
} else if (property instanceof MapSchema) {
1011-
datatype = "map";
1012-
} else if ( property instanceof UUIDSchema) {
1013-
datatype = "UUID";
1014-
} else if (property instanceof StringSchema) {
1015-
datatype = "string";
1013+
return schema.getType();
1014+
}
1015+
} else if (schema instanceof MapSchema) {
1016+
return "map";
1017+
} else if (schema instanceof UUIDSchema) {
1018+
return "UUID";
1019+
} else if (schema instanceof StringSchema) {
1020+
return "string";
10161021
} else {
1017-
if (property != null) {
1018-
if (SchemaTypeUtil.OBJECT_TYPE.equals(property.getType()) && property.getAdditionalProperties() != null) {
1019-
datatype = "map";
1022+
if (schema != null) {
1023+
if (SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && schema.getAdditionalProperties() != null) {
1024+
return "map";
10201025
} else {
1021-
datatype = property.getType();
1026+
return schema.getType();
10221027
}
10231028
}
10241029
}
1025-
return datatype;
1030+
return null;
10261031
}
10271032

10281033
/**
@@ -2885,8 +2890,9 @@ private static Map<String, String> getAllAliases(Map<String, Schema> allSchemas)
28852890
for (Map.Entry<String, Schema> entry : allSchemas.entrySet()) {
28862891
String swaggerName = entry.getKey();
28872892
Schema schema = entry.getValue();
2888-
if (schema.getType() != null && !schema.getType().equals("object") && schema.getEnum() == null) {
2889-
aliases.put(swaggerName, schema.getType());
2893+
String schemaType = getTypeOfSchema(schema);
2894+
if (schemaType != null && !schemaType.equals("object") && schema.getEnum() == null) {
2895+
aliases.put(swaggerName, schemaType);
28902896
}
28912897
}
28922898
return aliases;

src/main/java/io/swagger/codegen/languages/java/AbstractJavaCodegen.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ public String toApiFilename(String name) {
508508
@Override
509509
public String toVarName(String name) {
510510
// sanitize name
511-
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
511+
name = sanitizeVarName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
512512

513513
if (name.toLowerCase().matches("^_*class$")) {
514514
return "propertyClass";
@@ -584,7 +584,7 @@ public String toModelName(final String name) {
584584
return importMapping.get(name);
585585
}
586586

587-
final String sanitizedName = super.sanitizeName(name);
587+
final String sanitizedName = sanitizeName(name);
588588

589589
String nameWithPrefixSuffix = sanitizedName;
590590
if (!StringUtils.isEmpty(modelNamePrefix)) {
@@ -1058,8 +1058,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
10581058
return op;
10591059
}
10601060

1061-
@Override
1062-
public String sanitizeName(String name) {
1061+
public String sanitizeVarName(String name) {
10631062
if (name == null) {
10641063
LOGGER.warn("String to be sanitized is null. Default to " + Object.class.getSimpleName());
10651064
return Object.class.getSimpleName();

src/main/resources/v2/JavaJaxRS/apiService.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
88
{{#imports}}import {{import}};
99
{{/imports}}
1010

11+
import java.util.Map;
1112
import java.util.List;
1213
import {{package}}.NotFoundException;
1314

src/main/resources/v2/JavaJaxRS/apiServiceImpl.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {{modelPackage}}.*;
66
{{#imports}}import {{import}};
77
{{/imports}}
88

9+
import java.util.Map;
910
import java.util.List;
1011
import {{package}}.NotFoundException;
1112

src/main/resources/v2/JavaJaxRS/pojo.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
115115

116116
@Override
117117
public int hashCode() {
118-
return Objects.hash({{#vars}}{{name}}{{#has this 'more'}}, {{/has}}{{/vars}}{{#parent}}{{#has this 'vars'}}, {{/has}}super.hashCode(){{/parent}});
118+
return Objects.hash({{#vars}}{{name}}{{#has this 'more'}}, {{/has}}{{/vars}}{{#parentModel}}{{#has this 'vars'}}, {{/has}}super.hashCode(){{/parentModel}});
119119
}
120120

121121
{{/supportJava6}}
@@ -137,7 +137,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali
137137

138138
@Override
139139
public int hashCode() {
140-
return ObjectUtils.hashCodeMulti({{#vars}}{{name}}{{#has this 'more'}}, {{/has}}{{/vars}}{{#parent}}{{#has this 'vars'}}, {{/has}}super.hashCode(){{/parent}});
140+
return ObjectUtils.hashCodeMulti({{#vars}}{{name}}{{#has this 'more'}}, {{/has}}{{/vars}}{{#parentModel}}{{#has this 'vars'}}, {{/has}}super.hashCode(){{/parentModel}});
141141
}
142142

143143
{{/supportJava6}}

src/test/java/io/swagger/codegen/languages/java/AbstractJavaCodegenTest.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,20 @@ public void preprocessSwaggerWithNoFormOrBodyParamsDoesNotSetContentType() {
112112

113113
@Test
114114
public void convertVarName() throws Exception {
115-
AbstractJavaCodegen codegen = new JavaClientCodegen();
116-
Assert.assertEquals(codegen.toVarName("name"), "name");
117-
Assert.assertEquals(codegen.toVarName("$name"), "$name");
118-
Assert.assertEquals(codegen.toVarName("nam$$e"), "nam$$e");
119-
Assert.assertEquals(codegen.toVarName("_name"), "_name");
120-
Assert.assertEquals(codegen.toVarName("user-name"), "userName");
121-
Assert.assertEquals(codegen.toVarName("user_name"), "userName");
122-
Assert.assertEquals(codegen.toVarName("_user_name"), "_userName");
115+
Assert.assertEquals(fakeJavaCodegen.toVarName("name"), "name");
116+
Assert.assertEquals(fakeJavaCodegen.toVarName("$name"), "$name");
117+
Assert.assertEquals(fakeJavaCodegen.toVarName("nam$$e"), "nam$$e");
118+
Assert.assertEquals(fakeJavaCodegen.toVarName("_name"), "_name");
119+
Assert.assertEquals(fakeJavaCodegen.toVarName("user-name"), "userName");
120+
Assert.assertEquals(fakeJavaCodegen.toVarName("user_name"), "userName");
121+
Assert.assertEquals(fakeJavaCodegen.toVarName("_user_name"), "_userName");
122+
}
123+
124+
@Test
125+
public void convertModelName() throws Exception {
126+
Assert.assertEquals(fakeJavaCodegen.toModelName("name"), "Name");
127+
Assert.assertEquals(fakeJavaCodegen.toModelName("$name"), "Name");
128+
Assert.assertEquals(fakeJavaCodegen.toModelName("nam#e"), "Name");
129+
Assert.assertEquals(fakeJavaCodegen.toModelName("$another-fake?"), "AnotherFake");
123130
}
124131
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.swagger.codegen.languages.java;
2+
3+
import io.swagger.codegen.CodegenArgument;
4+
import io.swagger.codegen.CodegenType;
5+
import org.testng.Assert;
6+
import org.testng.annotations.Test;
7+
import java.util.List;
8+
9+
public class AbstractJavaJAXRSServerCodegenTest {
10+
11+
private final AbstractJavaJAXRSServerCodegen fakeJavaJAXRSCodegen = new AbstractJavaJAXRSServerCodegen() {
12+
@Override
13+
public String getArgumentsLocation() {
14+
return null;
15+
}
16+
17+
@Override
18+
public CodegenType getTag() {
19+
return null;
20+
}
21+
22+
@Override
23+
public String getName() {
24+
return null;
25+
}
26+
27+
@Override
28+
public String getHelp() {
29+
return null;
30+
}
31+
32+
@Override
33+
public List<CodegenArgument> readLanguageArguments() {
34+
return null;
35+
}
36+
};
37+
38+
@Test
39+
public void convertApiName() throws Exception {
40+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("name"), "NameApi");
41+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("$name"), "NameApi");
42+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("nam#e"), "NameApi");
43+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("$another-fake?"), "AnotherFakeApi");
44+
Assert.assertEquals(fakeJavaJAXRSCodegen.toApiName("fake_classname_tags 123#$%^"), "FakeClassnameTags123Api");
45+
}
46+
}

src/test/java/io/swagger/codegen/languages/java/JavaModelTest.java

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.swagger.v3.oas.models.media.IntegerSchema;
1717
import io.swagger.v3.oas.models.media.MapSchema;
1818
import io.swagger.v3.oas.models.media.NumberSchema;
19+
import io.swagger.v3.oas.models.media.ObjectSchema;
1920
import io.swagger.v3.oas.models.media.Schema;
2021
import io.swagger.v3.oas.models.media.StringSchema;
2122
import io.swagger.v3.oas.models.media.XML;
@@ -28,7 +29,9 @@
2829
import org.testng.annotations.Test;
2930

3031
import java.io.File;
32+
import java.util.Collections;
3133
import java.util.List;
34+
import java.util.Map;
3235

3336
import static io.swagger.codegen.languages.helpers.ExtensionHelper.getBooleanValue;
3437

@@ -722,7 +725,7 @@ public void modelWithWrappedXmlTest() {
722725
}
723726

724727
@Test(description = "convert a boolean parameter")
725-
public void booleanParameterTest() {
728+
public void booleanPropertyTest() {
726729
final BooleanSchema property = new BooleanSchema();
727730
final DefaultCodegenConfig codegen = new JavaClientCodegen();
728731
final CodegenProperty cp = codegen.fromProperty("property", property);
@@ -736,6 +739,92 @@ public void booleanParameterTest() {
736739
Assert.assertEquals(cp.getter, "isProperty");
737740
}
738741

742+
@Test(description = "convert an integer property")
743+
public void integerPropertyTest() {
744+
final IntegerSchema property = new IntegerSchema();
745+
final DefaultCodegenConfig codegen = new JavaClientCodegen();
746+
final CodegenProperty cp = codegen.fromProperty("property", property);
747+
748+
Assert.assertEquals(cp.baseName, "property");
749+
Assert.assertEquals(cp.datatype, "Integer");
750+
Assert.assertEquals(cp.name, "property");
751+
Assert.assertEquals(cp.baseType, "Integer");
752+
Assert.assertTrue(getBooleanValue(cp, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
753+
Assert.assertTrue(getBooleanValue(cp, CodegenConstants.IS_INTEGER_EXT_NAME));
754+
Assert.assertFalse(getBooleanValue(cp, CodegenConstants.IS_LONG_EXT_NAME));
755+
Assert.assertEquals(cp.getter, "getProperty");
756+
}
757+
758+
@Test(description = "convert a long property")
759+
public void longPropertyTest() {
760+
final IntegerSchema property = new IntegerSchema().format("int64");
761+
final DefaultCodegenConfig codegen = new JavaClientCodegen();
762+
final CodegenProperty cp = codegen.fromProperty("property", property);
763+
764+
Assert.assertEquals(cp.baseName, "property");
765+
Assert.assertEquals(cp.datatype, "Long");
766+
Assert.assertEquals(cp.name, "property");
767+
Assert.assertEquals(cp.baseType, "Long");
768+
Assert.assertTrue(getBooleanValue(cp, CodegenConstants.IS_NOT_CONTAINER_EXT_NAME));
769+
Assert.assertTrue(getBooleanValue(cp, CodegenConstants.IS_LONG_EXT_NAME));
770+
Assert.assertFalse(getBooleanValue(cp, CodegenConstants.IS_INTEGER_EXT_NAME));
771+
Assert.assertEquals(cp.getter, "getProperty");
772+
}
773+
774+
@Test(description = "convert a long property in a referenced schema")
775+
public void longPropertyInReferencedSchemaTest() {
776+
final IntegerSchema longProperty = new IntegerSchema().format("int64");
777+
final Schema TestSchema = new ObjectSchema()
778+
.addProperties("Long1", new Schema<>().$ref("#/components/schemas/LongProperty"))
779+
.addProperties("Long2", new IntegerSchema().format("int64"));
780+
final DefaultCodegenConfig codegen = new JavaClientCodegen();
781+
final Map<String, Schema> allDefinitions = Collections.singletonMap("LongProperty", longProperty);
782+
final CodegenModel cm = codegen.fromModel("test", TestSchema, allDefinitions);
783+
784+
Assert.assertEquals(cm.vars.size(), 2);
785+
786+
CodegenProperty cp1 = cm.vars.get(0);
787+
Assert.assertEquals(cp1.baseName, "Long1");
788+
Assert.assertEquals(cp1.datatype, "Long");
789+
Assert.assertEquals(cp1.name, "long1");
790+
Assert.assertEquals(cp1.baseType, "Long");
791+
Assert.assertEquals(cp1.getter, "getLong1");
792+
793+
CodegenProperty cp2 = cm.vars.get(1);
794+
Assert.assertEquals(cp2.baseName, "Long2");
795+
Assert.assertEquals(cp2.datatype, "Long");
796+
Assert.assertEquals(cp2.name, "long2");
797+
Assert.assertEquals(cp2.baseType, "Long");
798+
Assert.assertEquals(cp2.getter, "getLong2");
799+
}
800+
801+
@Test(description = "convert am integer property in a referenced schema")
802+
public void integerPropertyInReferencedSchemaTest() {
803+
final IntegerSchema longProperty = new IntegerSchema().format("int32");
804+
final Schema TestSchema = new ObjectSchema()
805+
.addProperties("Integer1", new Schema<>().$ref("#/components/schemas/IntegerProperty"))
806+
.addProperties("Integer2", new IntegerSchema().format("int32"));
807+
final DefaultCodegenConfig codegen = new JavaClientCodegen();
808+
final Map<String, Schema> allDefinitions = Collections.singletonMap("IntegerProperty", longProperty);
809+
final CodegenModel cm = codegen.fromModel("test", TestSchema, allDefinitions);
810+
811+
Assert.assertEquals(cm.vars.size(), 2);
812+
813+
CodegenProperty cp1 = cm.vars.get(0);
814+
Assert.assertEquals(cp1.baseName, "Integer1");
815+
Assert.assertEquals(cp1.datatype, "Integer");
816+
Assert.assertEquals(cp1.name, "integer1");
817+
Assert.assertEquals(cp1.baseType, "Integer");
818+
Assert.assertEquals(cp1.getter, "getInteger1");
819+
820+
CodegenProperty cp2 = cm.vars.get(1);
821+
Assert.assertEquals(cp2.baseName, "Integer2");
822+
Assert.assertEquals(cp2.datatype, "Integer");
823+
Assert.assertEquals(cp2.name, "integer2");
824+
Assert.assertEquals(cp2.baseType, "Integer");
825+
Assert.assertEquals(cp2.getter, "getInteger2");
826+
}
827+
739828
@Test(enabled = false, description = "disabled since templates have been moved.")
740829
public void generateModel() throws Exception {
741830
folder.create();

0 commit comments

Comments
 (0)