Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum values issue #10563

Merged
merged 5 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public class CodegenModel {
allMandatory = mandatory;
}

public boolean getIsInteger() {
return "Integer".equalsIgnoreCase(this.dataType);
}

public boolean getIsNumber() {
return "BigDecimal".equalsIgnoreCase(this.dataType);
}

@Override
public String toString() {
return String.format("%s(%s)", name, classname);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.regex.Pattern;

import io.swagger.models.properties.UntypedProperty;
import io.swagger.util.Yaml;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -347,7 +348,7 @@ public String toEnumDefaultValue(String value, String datatype) {
* @return the sanitized value for enum
*/
public String toEnumValue(String value, String datatype) {
if ("number".equalsIgnoreCase(datatype)) {
if (isPrimivite(datatype)) {
return value;
} else {
return "\"" + escapeText(value) + "\"";
Expand All @@ -374,6 +375,12 @@ public String toEnumVarName(String value, String datatype) {
}
}

public boolean isPrimivite(String datatype) {
return "number".equalsIgnoreCase(datatype)
|| "integer".equalsIgnoreCase(datatype)
|| "boolean".equalsIgnoreCase(datatype);
}

// override with any special post-processing
@SuppressWarnings("static-method")
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
Expand Down Expand Up @@ -1529,6 +1536,9 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
// comment out below as allowableValues is not set in post processing model enum
m.allowableValues = new HashMap<String, Object>();
m.allowableValues.put("values", impl.getEnum());
if (m.dataType.equals("BigDecimal")) {
addImport(m, "BigDecimal");
}
}
if (impl.getAdditionalProperties() != null) {
addAdditionPropertiesToCodeGenModel(m, impl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public AbstractJavaCodegen() {
"this", "break", "double", "implements", "protected", "throw", "byte", "else",
"import", "public", "throws", "case", "enum", "instanceof", "return", "transient",
"catch", "extends", "int", "short", "try", "char", "final", "interface", "static",
"void", "class", "finally", "long", "strictfp", "volatile", "const", "float",
"void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "list",
"native", "super", "while", "null")
);

Expand Down Expand Up @@ -1212,8 +1212,7 @@ public String toEnumVarName(String value, String datatype) {
}

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

@Override
public String toEnumValue(String value, String datatype) {
if ("Integer".equals(datatype) || "Double".equals(datatype)) {
if ("Integer".equals(datatype) || "Double".equals(datatype) || "Boolean".equals(datatype)) {
return value;
} else if ("Long".equals(datatype)) {
// add l to number, e.g. 2048 => 2048l
return value + "l";
} else if ("Float".equals(datatype)) {
// add f to number, e.g. 3.14 => 3.14f
return value + "f";
} else if ("BigDecimal".equals(datatype)) {
return "new BigDecimal(" + escapeText(value) + ")";
} else {
return "\"" + escapeText(value) + "\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,25 @@ public String toEnumVarName(String value, String datatype) {
return modified;
}

@Override
public String toEnumValue(String value, String datatype) {
if (isPrimivite(datatype)) {
return value;
}
return super.toEnumValue(value, datatype);
}

@Override
public boolean isPrimivite(String datatype) {
return "kotlin.Byte".equalsIgnoreCase(datatype)
|| "kotlin.Short".equalsIgnoreCase(datatype)
|| "kotlin.Int".equalsIgnoreCase(datatype)
|| "kotlin.Long".equalsIgnoreCase(datatype)
|| "kotlin.Float".equalsIgnoreCase(datatype)
|| "kotlin.Double".equalsIgnoreCase(datatype)
|| "kotlin.Boolean".equalsIgnoreCase(datatype);
}

@Override
public String toInstantiationType(Property p) {
if (p instanceof ArrayProperty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ public String toEnumVarName(String value, String datatype) {

@Override
public String toEnumValue(String value, String datatype) {
if ("Integer".equals(datatype) || "Number".equals(datatype)) {
if ("Integer".equals(datatype) || "Number".equals(datatype) || "Boolean".equals(datatype)) {
return value;
} else {
return "\"" + escapeText(value) + "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum

@Override
public {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} read(final JsonReader jsonReader) throws IOException {
{{{dataType}}} value = jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}};
{{{dataType}}} value = {{#isNumber}}new BigDecimal(jsonReader.nextDouble()){{/isNumber}}{{^isNumber}}jsonReader.{{#isInteger}}nextInt(){{/isInteger}}{{^isInteger}}next{{{dataType}}}(){{/isInteger}}{{/isNumber}};
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.fromValue(String.valueOf(value));
}
}
Expand Down
6 changes: 3 additions & 3 deletions modules/swagger-codegen/src/main/resources/Java/pojo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/{{#description}}
@ApiModel(description = "{{{description}}}"){{/description}}
{{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}}

{{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}}

{{#notNullJacksonAnnotation}}
@JsonInclude(JsonInclude.Include.NON_NULL)
{{/notNullJacksonAnnotation}}
public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable {{/serializableModel}}{{/parcelableModel}}{
{{#serializableModel}}
private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.swagger.codegen.java;

import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.DefaultCodegen;
Expand All @@ -8,8 +9,10 @@
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.RefModel;
import io.swagger.models.Swagger;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.StringProperty;
import io.swagger.parser.SwaggerParser;
import org.testng.Assert;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -92,4 +95,18 @@ public void overrideEnumTest() {
Assert.assertEquals(enumVar.datatypeWithEnum, "UnsharedThingEnum");
Assert.assertTrue(enumVar.isEnum);
}

@Test(description = "not override identical parent enums")
public void testEnumTypes() {
//final Swagger swagger = parser.read("src/test/resources/issue-913/BS/ApiSpecification.yaml");
final CodegenConfig codegenConfig = new JavaClientCodegen();

final Swagger swagger = new SwaggerParser().read("2_0/issue-10546.yaml", null, true);
final Model booleanModel = swagger.getDefinitions().get("Boolean");

CodegenModel codegenModel = codegenConfig.fromModel("Boolean", booleanModel);

Assert.assertTrue(codegenModel.isEnum);
Assert.assertEquals(codegenModel.dataType, "Boolean");
}
}
46 changes: 46 additions & 0 deletions modules/swagger-codegen/src/test/resources/2_0/issue-10546.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
swagger: '2.0'
info:
description: Demo
version: 1.0.0
title: Demo for Boolean-element Bug
schemes:
- https
consumes:
- application/json
produces:
- application/json
paths:
/types:
get:
produces:
- application/json
responses:
200:
description: OK
definitions:
Boolean:
type: boolean
description: True or False indicator
enum:
- true
- false
Interos:
type: integer
format: int32
description: True or False indicator
enum:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
Numeros:
type: number
description: some number
enum:
- 7
- 8
- 9
- 10
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,32 @@ definitions:
type: string
OuterBoolean:
type: boolean
Boolean:
type: boolean
description: True or False indicator
enum:
- true
- false
Ints:
type: integer
format: int32
description: True or False indicator
enum:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
Numbers:
type: number
description: some number
enum:
- 7
- 8
- 9
- 10
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.8-SNAPSHOT
2.4.18-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Swagger Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/


package io.swagger.client.model;

import java.util.Objects;
import java.util.Arrays;
import io.swagger.annotations.ApiModel;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* True or False indicator
*/
public enum Ints {

NUMBER_0(0),

NUMBER_1(1),

NUMBER_2(2),

NUMBER_3(3),

NUMBER_4(4),

NUMBER_5(5),

NUMBER_6(6);

private Integer value;

Ints(Integer value) {
this.value = value;
}

@JsonValue
public Integer getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static Ints fromValue(String text) {
for (Ints b : Ints.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Swagger Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/


package io.swagger.client.model;

import java.util.Objects;
import java.util.Arrays;
import io.swagger.annotations.ApiModel;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

/**
* True or False indicator
*/
public enum ModelBoolean {

TRUE(true),

FALSE(false);

private Boolean value;

ModelBoolean(Boolean value) {
this.value = value;
}

@JsonValue
public Boolean getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static ModelBoolean fromValue(String text) {
for (ModelBoolean b : ModelBoolean.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}

Loading