Skip to content

Commit c0b613d

Browse files
authored
Merge pull request #10563 from swagger-api/enum-values-issue
Enum values issue
2 parents e9cf755 + b755d46 commit c0b613d

File tree

369 files changed

+7653
-423
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

369 files changed

+7653
-423
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public class CodegenModel {
5656
allMandatory = mandatory;
5757
}
5858

59+
public boolean getIsInteger() {
60+
return "Integer".equalsIgnoreCase(this.dataType);
61+
}
62+
63+
public boolean getIsNumber() {
64+
return "BigDecimal".equalsIgnoreCase(this.dataType);
65+
}
66+
5967
@Override
6068
public String toString() {
6169
return String.format("%s(%s)", name, classname);

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.regex.Pattern;
1212

1313
import io.swagger.models.properties.UntypedProperty;
14+
import io.swagger.util.Yaml;
1415
import org.apache.commons.lang3.ObjectUtils;
1516
import org.apache.commons.lang3.StringEscapeUtils;
1617
import org.apache.commons.lang3.StringUtils;
@@ -347,7 +348,7 @@ public String toEnumDefaultValue(String value, String datatype) {
347348
* @return the sanitized value for enum
348349
*/
349350
public String toEnumValue(String value, String datatype) {
350-
if ("number".equalsIgnoreCase(datatype)) {
351+
if (isPrimivite(datatype)) {
351352
return value;
352353
} else {
353354
return "\"" + escapeText(value) + "\"";
@@ -374,6 +375,12 @@ public String toEnumVarName(String value, String datatype) {
374375
}
375376
}
376377

378+
public boolean isPrimivite(String datatype) {
379+
return "number".equalsIgnoreCase(datatype)
380+
|| "integer".equalsIgnoreCase(datatype)
381+
|| "boolean".equalsIgnoreCase(datatype);
382+
}
383+
377384
// override with any special post-processing
378385
@SuppressWarnings("static-method")
379386
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
@@ -1529,6 +1536,9 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
15291536
// comment out below as allowableValues is not set in post processing model enum
15301537
m.allowableValues = new HashMap<String, Object>();
15311538
m.allowableValues.put("values", impl.getEnum());
1539+
if (m.dataType.equals("BigDecimal")) {
1540+
addImport(m, "BigDecimal");
1541+
}
15321542
}
15331543
if (impl.getAdditionalProperties() != null) {
15341544
addAdditionPropertiesToCodeGenModel(m, impl);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public AbstractJavaCodegen() {
122122
"this", "break", "double", "implements", "protected", "throw", "byte", "else",
123123
"import", "public", "throws", "case", "enum", "instanceof", "return", "transient",
124124
"catch", "extends", "int", "short", "try", "char", "final", "interface", "static",
125-
"void", "class", "finally", "long", "strictfp", "volatile", "const", "float",
125+
"void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "list",
126126
"native", "super", "while", "null")
127127
);
128128

@@ -1212,8 +1212,7 @@ public String toEnumVarName(String value, String datatype) {
12121212
}
12131213

12141214
// number
1215-
if ("Integer".equals(datatype) || "Long".equals(datatype) ||
1216-
"Float".equals(datatype) || "Double".equals(datatype)) {
1215+
if ("Integer".equals(datatype) || "Long".equals(datatype) || "Float".equals(datatype) || "Double".equals(datatype) || "BigDecimal".equals(datatype)) {
12171216
String varName = "NUMBER_" + value;
12181217
varName = varName.replaceAll("-", "MINUS_");
12191218
varName = varName.replaceAll("\\+", "PLUS_");
@@ -1232,14 +1231,16 @@ public String toEnumVarName(String value, String datatype) {
12321231

12331232
@Override
12341233
public String toEnumValue(String value, String datatype) {
1235-
if ("Integer".equals(datatype) || "Double".equals(datatype)) {
1234+
if ("Integer".equals(datatype) || "Double".equals(datatype) || "Boolean".equals(datatype)) {
12361235
return value;
12371236
} else if ("Long".equals(datatype)) {
12381237
// add l to number, e.g. 2048 => 2048l
12391238
return value + "l";
12401239
} else if ("Float".equals(datatype)) {
12411240
// add f to number, e.g. 3.14 => 3.14f
12421241
return value + "f";
1242+
} else if ("BigDecimal".equals(datatype)) {
1243+
return "new BigDecimal(" + escapeText(value) + ")";
12431244
} else {
12441245
return "\"" + escapeText(value) + "\"";
12451246
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractKotlinCodegen.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,25 @@ public String toEnumVarName(String value, String datatype) {
406406
return modified;
407407
}
408408

409+
@Override
410+
public String toEnumValue(String value, String datatype) {
411+
if (isPrimivite(datatype)) {
412+
return value;
413+
}
414+
return super.toEnumValue(value, datatype);
415+
}
416+
417+
@Override
418+
public boolean isPrimivite(String datatype) {
419+
return "kotlin.Byte".equalsIgnoreCase(datatype)
420+
|| "kotlin.Short".equalsIgnoreCase(datatype)
421+
|| "kotlin.Int".equalsIgnoreCase(datatype)
422+
|| "kotlin.Long".equalsIgnoreCase(datatype)
423+
|| "kotlin.Float".equalsIgnoreCase(datatype)
424+
|| "kotlin.Double".equalsIgnoreCase(datatype)
425+
|| "kotlin.Boolean".equalsIgnoreCase(datatype);
426+
}
427+
409428
@Override
410429
public String toInstantiationType(Property p) {
411430
if (p instanceof ArrayProperty) {

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavascriptClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ public String toEnumVarName(String value, String datatype) {
14121412

14131413
@Override
14141414
public String toEnumValue(String value, String datatype) {
1415-
if ("Integer".equals(datatype) || "Number".equals(datatype)) {
1415+
if ("Integer".equals(datatype) || "Number".equals(datatype) || "Boolean".equals(datatype)) {
14161416
return value;
14171417
} else {
14181418
return "\"" + escapeText(value) + "\"";

modules/swagger-codegen/src/main/resources/Java/modelEnum.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
6060

6161
@Override
6262
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
63-
{{{dataType}}} value = jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}};
63+
{{{dataType}}} value = {{#isNumber}}new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
6464
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue(String.valueOf(value));
6565
}
6666
}

modules/swagger-codegen/src/main/resources/Java/pojo.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/{{#description}}
44
@ApiModel(description = "{{{description}}}"){{/description}}
55
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}
6-
7-
{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}
8-
6+
{{#notNullJacksonAnnotation}}
7+
@JsonInclude(JsonInclude.Include.NON_NULL)
8+
{{/notNullJacksonAnnotation}}
99
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable {{/serializableModel}}{{/parcelableModel}}{
1010
{{#serializableModel}}
1111
private static final long serialVersionUID = 1L;

modules/swagger-codegen/src/test/java/io/swagger/codegen/java/JavaModelEnumTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.codegen.java;
22

3+
import io.swagger.codegen.CodegenConfig;
34
import io.swagger.codegen.CodegenModel;
45
import io.swagger.codegen.CodegenProperty;
56
import io.swagger.codegen.DefaultCodegen;
@@ -8,8 +9,10 @@
89
import io.swagger.models.Model;
910
import io.swagger.models.ModelImpl;
1011
import io.swagger.models.RefModel;
12+
import io.swagger.models.Swagger;
1113
import io.swagger.models.properties.Property;
1214
import io.swagger.models.properties.StringProperty;
15+
import io.swagger.parser.SwaggerParser;
1316
import org.testng.Assert;
1417
import org.testng.annotations.Test;
1518

@@ -92,4 +95,18 @@ public void overrideEnumTest() {
9295
Assert.assertEquals(enumVar.datatypeWithEnum, "UnsharedThingEnum");
9396
Assert.assertTrue(enumVar.isEnum);
9497
}
98+
99+
@Test(description = "not override identical parent enums")
100+
public void testEnumTypes() {
101+
//final Swagger swagger = parser.read("src/test/resources/issue-913/BS/ApiSpecification.yaml");
102+
final CodegenConfig codegenConfig = new JavaClientCodegen();
103+
104+
final Swagger swagger = new SwaggerParser().read("2_0/issue-10546.yaml", null, true);
105+
final Model booleanModel = swagger.getDefinitions().get("Boolean");
106+
107+
CodegenModel codegenModel = codegenConfig.fromModel("Boolean", booleanModel);
108+
109+
Assert.assertTrue(codegenModel.isEnum);
110+
Assert.assertEquals(codegenModel.dataType, "Boolean");
111+
}
95112
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
swagger: '2.0'
2+
info:
3+
description: Demo
4+
version: 1.0.0
5+
title: Demo for Boolean-element Bug
6+
schemes:
7+
- https
8+
consumes:
9+
- application/json
10+
produces:
11+
- application/json
12+
paths:
13+
/types:
14+
get:
15+
produces:
16+
- application/json
17+
responses:
18+
200:
19+
description: OK
20+
definitions:
21+
Boolean:
22+
type: boolean
23+
description: True or False indicator
24+
enum:
25+
- true
26+
- false
27+
Interos:
28+
type: integer
29+
format: int32
30+
description: True or False indicator
31+
enum:
32+
- 0
33+
- 1
34+
- 2
35+
- 3
36+
- 4
37+
- 5
38+
- 6
39+
Numeros:
40+
type: number
41+
description: some number
42+
enum:
43+
- 7
44+
- 8
45+
- 9
46+
- 10

modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,32 @@ definitions:
14661466
type: string
14671467
OuterBoolean:
14681468
type: boolean
1469+
Boolean:
1470+
type: boolean
1471+
description: True or False indicator
1472+
enum:
1473+
- true
1474+
- false
1475+
Ints:
1476+
type: integer
1477+
format: int32
1478+
description: True or False indicator
1479+
enum:
1480+
- 0
1481+
- 1
1482+
- 2
1483+
- 3
1484+
- 4
1485+
- 5
1486+
- 6
1487+
Numbers:
1488+
type: number
1489+
description: some number
1490+
enum:
1491+
- 7
1492+
- 8
1493+
- 9
1494+
- 10
14691495
externalDocs:
14701496
description: Find out more about Swagger
14711497
url: 'http://swagger.io'

0 commit comments

Comments
 (0)