diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java index 28803a499f..a260a92baa 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java @@ -90,6 +90,7 @@ import static org.openapitools.codegen.CodegenConstants.MODEL_PACKAGE; import static org.openapitools.codegen.utils.OnceLogger.once; import static org.openapitools.codegen.utils.StringUtils.camelize; +import static org.openapitools.codegen.utils.StringUtils.underscore; /** * Base generator for Micronaut. @@ -190,6 +191,19 @@ protected AbstractMicronautJavaCodegen() { inlineSchemaOption.put("RESOLVE_INLINE_ENUMS", "true"); // CHECKSTYLE:ON + languageSpecificPrimitives.addAll(Set.of( + "char", + "float", + "double", + "byte", + "short", + "int", + "long" + )); + + typeMapping.put("char", "Character"); + typeMapping.put("byte", "Byte"); + GlobalSettings.setProperty(DIVIDE_OPERATIONS_BY_CONTENT_TYPE, "true"); // Set implemented features for user information @@ -784,7 +798,45 @@ public String toEnumVarName(String value, String datatype) { if (value == null) { return null; } - return super.toEnumVarName(value, datatype); + if (enumNameMapping.containsKey(value)) { + return enumNameMapping.get(value); + } + + if (value.isEmpty()) { + return "EMPTY"; + } + + // for symbol, e.g. $, # + if (getSymbolName(value) != null) { + return getSymbolName(value).toUpperCase(Locale.ROOT); + } + + if (" ".equals(value)) { + return "SPACE"; + } + + // number + if ("Int".equalsIgnoreCase(datatype) + || "Byte".equalsIgnoreCase(datatype) + || "Short".equalsIgnoreCase(datatype) + || "Integer".equalsIgnoreCase(datatype) + || "Long".equalsIgnoreCase(datatype) + || "Float".equalsIgnoreCase(datatype) + || "Double".equalsIgnoreCase(datatype) + || "BigDecimal".equals(datatype)) { + String varName = "NUMBER_" + value; + varName = varName.replaceAll("-", "MINUS_"); + varName = varName.replaceAll("\\+", "PLUS_"); + varName = varName.replaceAll("\\.", "_DOT_"); + return varName; + } + + // string + String var = underscore(value.replaceAll("\\W+", "_")).toUpperCase(Locale.ROOT); + if (var.matches("\\d.*")) { + var = "_" + var; + } + return this.toVarName(var); } @Override @@ -1288,6 +1340,57 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List para } for (ParameterMapping mapping : additionalMappings.values()) { - if (mapping.mappedType() != null) { - CodegenParameter newParam = new CodegenParameter(); - newParam.paramName = mapping.mappedName(); - newParam.required = true; - newParam.isModel = mapping.isValidated(); - - String typeName = makeSureImported(mapping.mappedType(), imports); - newParam.dataType = typeName; - - // Set the paramName if required - if (newParam.paramName == null) { - newParam.paramName = toParamName(typeName); - } + if (mapping.mappedType() == null) { + continue; + } + var newParam = new CodegenParameter(); + newParam.paramName = mapping.mappedName(); + newParam.required = true; + newParam.isModel = mapping.isValidated(); + + String typeName = makeSureImported(mapping.mappedType(), imports); + newParam.dataType = typeName; - params.add(newParam); + // Set the paramName if required + if (newParam.paramName == null) { + newParam.paramName = toParamName(typeName); } + + params.add(newParam); } } @@ -1668,6 +1772,72 @@ public Map postProcessAllModels(Map objs) return objs; } + @Override + protected void updateEnumVarsWithExtensions(List> enumVars, Map vendorExtensions, String dataType) { + super.updateEnumVarsWithExtensions(enumVars, vendorExtensions, dataType); + if (vendorExtensions == null) { + return; + } + + var xDeprecated = (List) vendorExtensions.get("x-deprecated"); + if (xDeprecated != null && !xDeprecated.isEmpty()) { + for (var deprecatedItem : xDeprecated) { + Map foundEnumVar = null; + for (var enumVar : enumVars) { + var isString = (boolean) enumVar.get("isString"); + var value = (String) enumVar.get("value"); + if (!isString) { + if (value.startsWith("(short)")) { + value = value.replace("(short) ", ""); + } else if (value.startsWith("(byte)")) { + value = value.replace("(byte) ", ""); + } + var argPos = value.indexOf('('); + // case for BigDecimal + if (argPos >= 0) { + value = value.substring(argPos + 1, value.indexOf(')')); + } + var upperValue = value.toUpperCase(); + if (upperValue.endsWith("F") + || upperValue.endsWith("L") + || upperValue.endsWith("D")) { + value = value.substring(0, value.length() - 1); + } + if (!value.contains("'")) { + value = value.replace("'", ""); + } + if (!value.contains("\"")) { + value = "\"" + value + "\""; + } + } + if (value.equals("\"" + deprecatedItem + '"')) { + foundEnumVar = enumVar; + break; + } + } + if (foundEnumVar != null) { + foundEnumVar.put("deprecated", true); + } + } + } + + var baseType = (String) vendorExtensions.get("baseType"); + for (var enumVar : enumVars) { + if ((boolean) enumVar.get("isString")) { + continue; + } + var value = (String) enumVar.get("value"); + value = value.replace("\"", ""); + if ("char".equals(baseType) && !value.startsWith("'")) { + enumVar.put("value", "'" + value + "'"); + } else if ("short".equalsIgnoreCase(baseType) && !value.startsWith("(short)")) { + enumVar.put("value", "(short) " + value); + } else if ("byte".equalsIgnoreCase(baseType) && !value.startsWith("(byte)")) { + enumVar.put("value", "(byte) " + value); + } + } + } + private void processOneOfModels(CodegenModel model, Collection models) { if (!model.vendorExtensions.containsKey("x-is-one-of-interface") diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java index ec5312da13..1c173f0d6a 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java @@ -1096,6 +1096,56 @@ public String toEnumValue(String value, String datatype) { @Override public CodegenModel fromModel(String name, Schema schema) { CodegenModel model = super.fromModel(name, schema); + var format = schema.getFormat(); + var schemaType = schema.getType() == null ? "object" : schema.getType(); + var baseType = model.dataType; + if ("char".equals(schemaType)) { + baseType = "char"; + model.dataType = "Char"; + } else if ("byte".equals(schemaType)) { + baseType = "byte"; + model.dataType = "Byte"; + model.isNumeric = true; + model.isNumber = true; + } else if ("integer".equals(schemaType) && ("int8".equals(format) || "byte".equals(format))) { + baseType = "Byte"; + model.dataType = "Byte"; + model.isNumeric = true; + model.isNumber = true; + } else if ("short".equals(schemaType)) { + baseType = "short"; + model.dataType = "Short"; + model.isShort = true; + model.isNumeric = true; + model.isNumber = true; + } else if ("integer".equals(schemaType) && ("int16".equals(format) || "short".equals(format))) { + baseType = "Short"; + model.dataType = "Short"; + model.isShort = true; + model.isNumeric = true; + model.isNumber = true; + } else if ("int".equals(schemaType)) { + baseType = "int"; + model.dataType = "Int"; + model.isNumeric = true; + model.isNumber = true; + } else if ("long".equals(schemaType)) { + baseType = "long"; + model.dataType = "Long"; + model.isNumeric = true; + model.isNumber = true; + } else if ("float".equals(schemaType)) { + baseType = "float"; + model.dataType = "Float"; + model.isNumeric = true; + model.isNumber = true; + } else if ("double".equals(schemaType)) { + baseType = "double"; + model.dataType = "Double"; + model.isNumeric = true; + model.isNumber = true; + } + model.vendorExtensions.put("baseType", baseType); if (!model.oneOf.isEmpty()) { if (useOneOfInterfaces) { model.vendorExtensions.put("x-is-one-of-interface", true); @@ -1364,11 +1414,30 @@ public String toEnumVarName(String value, String datatype) { modified = "EMPTY"; return normalizeKotlinSpecificNames(modified); } - value = value.replaceAll("[^a-zA-Z0-9_]", "_"); - if (isNumeric(value)) { - value = "_" + value; + + String varName = value; + + // number + if ("Int".equalsIgnoreCase(datatype) + || "Byte".equalsIgnoreCase(datatype) + || "Short".equalsIgnoreCase(datatype) + || "Integer".equalsIgnoreCase(datatype) + || "Long".equalsIgnoreCase(datatype) + || "Float".equalsIgnoreCase(datatype) + || "Double".equalsIgnoreCase(datatype) + || "BigDecimal".equals(datatype)) { + varName = "NUMBER_" + varName; + varName = varName.replaceAll("-", "MINUS_"); + varName = varName.replaceAll("\\+", "PLUS_"); + varName = varName.replaceAll("\\.", "_DOT_"); + } + varName = varName.replaceAll("[^a-zA-Z0-9_]", "_"); + + if (" ".equals(varName)) { + return "SPACE"; } - return super.toEnumVarName(value, datatype); + + return super.toEnumVarName(varName, datatype); } public static boolean isNumeric(String str) { @@ -1617,6 +1686,69 @@ public Map postProcessAllModels(Map objs) return objs; } + @Override + protected void updateEnumVarsWithExtensions(List> enumVars, Map vendorExtensions, String dataType) { + super.updateEnumVarsWithExtensions(enumVars, vendorExtensions, dataType); + if (vendorExtensions == null) { + return; + } + var xDeprecated = (List) vendorExtensions.get("x-deprecated"); + if (xDeprecated != null && !xDeprecated.isEmpty()) { + for (var deprecatedItem : xDeprecated) { + Map foundEnumVar = null; + for (var enumVar : enumVars) { + var isString = (boolean) enumVar.get("isString"); + var value = (String) enumVar.get("value"); + if (!isString) { + if (value.startsWith("(short)")) { + value = value.replace("(short) ", ""); + } + var argPos = value.indexOf('('); + // case for BigDecimal + if (argPos >= 0) { + value = value.substring(argPos + 1, value.indexOf(')')); + } + var upperValue = value.toUpperCase(); + if (upperValue.endsWith("F") + || upperValue.endsWith("L") + || upperValue.endsWith("D")) { + value = value.substring(0, value.length() - 1); + } + if (!value.contains("'")) { + value = value.replace("'", ""); + } + if (!value.contains("\"")) { + value = "\"" + value + "\""; + } + } + if (value.equals("\"" + deprecatedItem + '"')) { + foundEnumVar = enumVar; + break; + } + } + if (foundEnumVar != null) { + foundEnumVar.put("deprecated", true); + } + } + } + + var baseType = (String) vendorExtensions.get("baseType"); + for (var enumVar : enumVars) { + if ((boolean) enumVar.get("isString")) { + continue; + } + var value = (String) enumVar.get("value"); + value = value.replace("\"", ""); + if ("char".equals(baseType) && !value.startsWith("'")) { + enumVar.put("value", "'" + value + "'"); + } else if ("short".equalsIgnoreCase(baseType)) { + enumVar.put("value", value); + } else if ("byte".equalsIgnoreCase(baseType)) { + enumVar.put("value", value); + } + } + } + private void processOneOfModels(CodegenModel model, Collection models) { if (!model.vendorExtensions.containsKey("x-is-one-of-interface") diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/Utils.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/Utils.java index 30ae1a38da..e53c1d04d9 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/Utils.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/Utils.java @@ -297,6 +297,12 @@ public static void addStrValueToEnum(List enumVars, boolean isNumeric) { for (var enumVar : enumVars) { var varMap = (Map) enumVar; var value = varMap.get("value").toString(); + if (value.startsWith("(short)")) { + value = value.replace("(short) ", ""); + } else if (value.startsWith("(byte)")) { + value = value.replace("(byte) ", ""); + } + value = value.replace("'", ""); if (isNumeric) { var argPos = value.indexOf('('); // case for BigDecimal @@ -309,9 +315,9 @@ public static void addStrValueToEnum(List enumVars, boolean isNumeric) { || upperValue.endsWith("D")) { value = value.substring(0, value.length() - 1); } - if (!value.contains("\"")) { - value = "\"" + value + "\""; - } + } + if (!value.contains("\"")) { + value = "\"" + value + "\""; } varMap.put("strValue", value); } diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/enum.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/enum.mustache index 8ffc48066b..049982ce6a 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/enum.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/enum.mustache @@ -30,6 +30,9 @@ {{#withXml}} @XmlEnumValue({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}) {{/withXml}} + {{#deprecated}} + @Deprecated + {{/deprecated}} @JsonProperty({{{strValue}}}) {{{name}}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/enumVars}} @@ -38,10 +41,10 @@ {{#formatSingleLine}}public final static Map<{{{dataType}}}, {{>common/model/enumName}}> VALUE_MAPPING = Map.copyOf(Arrays.stream(values()){{/formatSingleLine}} .collect(Collectors.toMap(v -> v.value{{#isString}}{{#useEnumCaseInsensitive}}.toLowerCase(){{/useEnumCaseInsensitive}}{{/isString}}, Function.identity()))); - private final {{{dataType}}} value; + private final {{{vendorExtensions.baseType}}} value; {{^lombok}} - {{#formatSingleLine}}{{>common/model/enumName}}{{/formatSingleLine}}({{{dataType}}} value) { + {{#formatSingleLine}}{{>common/model/enumName}}{{/formatSingleLine}}({{{vendorExtensions.baseType}}} value) { this.value = value; } @@ -51,7 +54,7 @@ {{#jackson}} @JsonValue {{/jackson}} - public {{{dataType}}} getValue() { + public {{{vendorExtensions.baseType}}} getValue() { return value; } {{/lombok}} diff --git a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/enum.mustache b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/enum.mustache index 9934349bce..15e270968f 100644 --- a/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/enum.mustache +++ b/openapi-generator/src/main/resources/templates/kotlin-micronaut/common/model/enum.mustache @@ -30,6 +30,9 @@ {{#withXml}} @XmlEnumValue({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}) {{/withXml}} + {{#deprecated}} + @Deprecated("") + {{/deprecated}} @JsonProperty({{{strValue}}}) {{{name}}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}} {{/enumVars}} diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/AbstractMicronautCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/AbstractMicronautCodegenTest.java index 22caf51265..516b2b3ec3 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/AbstractMicronautCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/AbstractMicronautCodegenTest.java @@ -37,7 +37,7 @@ protected String generateFiles(MicronautCodeGenerator codegen, String configP } catch (IOException e) { fail("Unable to create temporary directory for output"); } - output.deleteOnExit(); +// output.deleteOnExit(); MicronautCodeGeneratorEntryPoint.builder() .forCodeGenerator(codegen, unused -> { diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java index 60e8da69a5..235f70dd94 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java @@ -816,4 +816,114 @@ public static StringEnum fromValue(String value) { } """); } + + @Test + void testEnumsExtensionsAndPrimitives() { + + var codegen = new JavaMicronautClientCodegen(); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/enum2.yml", CodegenConstants.APIS, CodegenConstants.MODELS); + String modelPath = outputPath + "src/main/java/org/openapitools/model/"; + + assertFileContains(modelPath + "BytePrimitiveEnum.java", + "NUMBER_1((byte) 1),", + "private final byte value", + "BytePrimitiveEnum(byte value)", + "public byte getValue() {"); + + assertFileContains(modelPath + "CharPrimitiveEnum.java", + "A('a'),", + "private final char value", + "CharPrimitiveEnum(char value)", + "public char getValue() {"); + + assertFileContains(modelPath + "ShortPrimitiveEnum.java", + "NUMBER_1((short) 1),", + "private final short value", + "ShortPrimitiveEnum(short value)", + "public short getValue() {"); + + assertFileContains(modelPath + "IntPrimitiveEnum.java", + "NUMBER_1(1),", + "private final int value", + "IntPrimitiveEnum(int value)", + "public int getValue() {"); + + assertFileContains(modelPath + "LongPrimitiveEnum.java", + "NUMBER_1(1L),", + "private final long value", + "LongPrimitiveEnum(long value)", + "public long getValue() {"); + + assertFileContains(modelPath + "FloatPrimitiveEnum.java", + "NUMBER_1_DOT_23(1.23F),", + "private final float value", + "FloatPrimitiveEnum(float value)", + "public float getValue() {"); + + assertFileContains(modelPath + "DoublePrimitiveEnum.java", + "NUMBER_1_DOT_23(1.23),", + "private final double value", + "DoublePrimitiveEnum(double value)", + "public double getValue() {"); + + assertFileContains(modelPath + "StringEnum.java", + """ + @Deprecated + @JsonProperty("starting") + STARTING("starting"), + """, + """ + @Deprecated + @JsonProperty("running") + RUNNING("running"), + """); + + assertFileContains(modelPath + "DecimalEnum.java", + """ + @Deprecated + @JsonProperty("34.1") + NUMBER_34_DOT_1(new BigDecimal("34.1")); + """); + + assertFileContains(modelPath + "ByteEnum.java", + "NUMBER_1((byte) 1),", + "private final Byte value", + "ByteEnum(Byte value)", + "public Byte getValue() {"); + + assertFileContains(modelPath + "ShortEnum.java", + "NUMBER_1((short) 1),", + "private final Short value", + "ShortEnum(Short value)", + "public Short getValue() {"); + + assertFileContains(modelPath + "IntEnum.java", + """ + /** + * This is one + */ + @JsonProperty("1") + THE_ONE(1), + """, + """ + @Deprecated + @JsonProperty("2") + THE_TWO(2), + """, + """ + /** + * This is three + */ + @JsonProperty("3") + THE_THREE(3), + """ + ); + + assertFileContains(modelPath + "LongEnum.java", + """ + @Deprecated + @JsonProperty("2") + NUMBER_2(2L), + """); + } } diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java index 347c6d1a7c..0062b53865 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java @@ -463,9 +463,9 @@ fun fromValue(value: String): StringEnum { return VALUE_MAPPING[value]!! } """); - assertFileContains(modelPath + "IntEnum.kt", "@JsonProperty(\"1\")", "_1(1),"); - assertFileContains(modelPath + "LongEnum.kt", "@JsonProperty(\"1\")", "_3(3L),"); - assertFileContains(modelPath + "DecimalEnum.kt", "@JsonProperty(\"1.23\")", "_34_1(BigDecimal(\"34.1\"))"); + assertFileContains(modelPath + "IntEnum.kt", "@JsonProperty(\"1\")", "NUMBER_1(1),"); + assertFileContains(modelPath + "LongEnum.kt", "@JsonProperty(\"1\")", "NUMBER_3(3L),"); + assertFileContains(modelPath + "DecimalEnum.kt", "@JsonProperty(\"1.23\")", "NUMBER_34_DOT_1(BigDecimal(\"34.1\"))"); } @Test @@ -836,4 +836,96 @@ fun fromValue(value: String): StringEnum { } """); } + + @Test + void testEnumsExtensions() { + + var codegen = new KotlinMicronautClientCodegen(); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/enum2.yml", CodegenConstants.APIS, CodegenConstants.MODELS); + String modelPath = outputPath + "src/main/kotlin/org/openapitools/model/"; + + assertFileContains(modelPath + "BytePrimitiveEnum.kt", + "NUMBER_1(1),", + "@get:JsonValue val value: Byte"); + + assertFileContains(modelPath + "CharPrimitiveEnum.kt", + "A('a'),", + "@get:JsonValue val value: Char"); + + assertFileContains(modelPath + "ShortPrimitiveEnum.kt", + "NUMBER_1(1),", + "@get:JsonValue val value: Short"); + + assertFileContains(modelPath + "IntPrimitiveEnum.kt", + "NUMBER_1(1),", + "@get:JsonValue val value: Int"); + + assertFileContains(modelPath + "LongPrimitiveEnum.kt", + "NUMBER_1(1L),", + "@get:JsonValue val value: Long"); + + assertFileContains(modelPath + "FloatPrimitiveEnum.kt", + "NUMBER_1_DOT_23(1.23F),", + "@get:JsonValue val value: Float"); + + assertFileContains(modelPath + "DoublePrimitiveEnum.kt", + "NUMBER_1_DOT_23(1.23),", + "@get:JsonValue val value: Double"); + + assertFileContains(modelPath + "StringEnum.kt", + """ + @Deprecated("") + @JsonProperty("starting") + STARTING("starting"), + """, + """ + @Deprecated("") + @JsonProperty("running") + RUNNING("running"), + """); + + assertFileContains(modelPath + "DecimalEnum.kt", + """ + @Deprecated("") + @JsonProperty("34.1") + NUMBER_34_DOT_1(BigDecimal("34.1")); + """); + + assertFileContains(modelPath + "ByteEnum.kt", + "NUMBER_1(1),", + "@get:JsonValue val value: Byte"); + + assertFileContains(modelPath + "ShortEnum.kt", + "NUMBER_1(1),", + "@get:JsonValue val value: Short"); + + assertFileContains(modelPath + "IntEnum.kt", + """ + /** + * This is one + */ + @JsonProperty("1") + THE_ONE(1), + """, + """ + @Deprecated("") + @JsonProperty("2") + THE_TWO(2), + """, + """ + /** + * This is three + */ + @JsonProperty("3") + THE_THREE(3), + """ + ); + + assertFileContains(modelPath + "LongEnum.kt", + """ + @Deprecated("") + @JsonProperty("2") + NUMBER_2(2L), + """); + } } diff --git a/openapi-generator/src/test/resources/3_0/enum2.yml b/openapi-generator/src/test/resources/3_0/enum2.yml new file mode 100644 index 0000000000..41061c894b --- /dev/null +++ b/openapi-generator/src/test/resources/3_0/enum2.yml @@ -0,0 +1,184 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Compute API + description: API for the Compute Service +servers: + - url: localhost:8000/api + description: The api server + +paths: + /sendEnum: + get: + operationId: sendEnum + tags: [ parameters ] + parameters: + - name: name + in: query + required: true + schema: + $ref: "#/components/schemas/StringEnum" + - name: intEnum + in: query + required: true + schema: + $ref: "#/components/schemas/IntEnum" + - name: longEnum + in: query + required: true + schema: + $ref: "#/components/schemas/LongEnum" + - name: boolEnum + in: query + required: true + schema: + $ref: "#/components/schemas/BooleanEnum" + - name: decimalEnum + in: query + required: true + schema: + $ref: "#/components/schemas/DecimalEnum" + - name: floatEnum + in: query + required: true + schema: + $ref: "#/components/schemas/FloatEnum" + - name: doubleEnum + in: query + required: true + schema: + $ref: "#/components/schemas/DoubleEnum" + - name: bytePrimitiveEnum + in: query + required: true + schema: + $ref: "#/components/schemas/BytePrimitiveEnum" + - name: shortEnum + in: query + required: true + schema: + $ref: "#/components/schemas/ShortPrimitiveEnum" + - name: intEnum + in: query + required: true + schema: + $ref: "#/components/schemas/IntPrimitiveEnum" + - name: longEnum + in: query + required: true + schema: + $ref: "#/components/schemas/LongPrimitiveEnum" + - name: floatPrimitiveEnum + in: query + required: true + schema: + $ref: "#/components/schemas/FloatPrimitiveEnum" + - name: doublePrimitiveEnum + in: query + required: true + schema: + $ref: "#/components/schemas/DoublePrimitiveEnum" + - name: charPrimitiveEnum + in: query + required: true + schema: + $ref: "#/components/schemas/CharPrimitiveEnum" + responses: + 200: + description: Success +components: + schemas: + StringEnum: + type: string + enum: ['starting', 'running', 'stopped', 'deleted'] + x-deprecated: + - starting + - running + ByteEnum: + type: integer + format: byte + enum: [1, 2, 3, 4] + ShortEnum: + type: integer + format: int16 + enum: [1, 2, 3, 4, 5] + IntEnum: + type: integer + format: int32 + enum: [1, 2, 3, 4, 5] + x-deprecated: + - 2 + - 5 + x-enum-varnames: + - THE_ONE + - THE_TWO + - THE_THREE + - THE_FOUR + - THE_FIVE + x-enum-descriptions: + - This is one + - + - This is three + - This is four + - + LongEnum: + type: integer + format: int64 + enum: [1, 2, 3, 4, 5] + x-deprecated: + - 2 + - 5 + BooleanEnum: + type: boolean + enum: ['true', 'false'] + x-deprecated: + - false + FloatEnum: + type: number + format: float + enum: [1.23, 2.45, 34.10] + x-deprecated: + - 34.10 + DoubleEnum: + type: number + format: double + enum: [1.23, 2.45, 34.10] + x-deprecated: + - 34.10 + DecimalEnum: + type: number + enum: [ 1.23, 2.45, 34.10 ] + x-deprecated: + - 34.10 + BytePrimitiveEnum: + type: byte + enum: [1, 2, 3, 4] + x-deprecated: + - 4 + ShortPrimitiveEnum: + type: short + enum: [1, 2, 3, 4, 5] + IntPrimitiveEnum: + type: int + enum: [1, 2, 3, 4, 5] + LongPrimitiveEnum: + type: long + enum: [1, 2, 3, 4, 5] + x-deprecated: + - 2 + - 5 + FloatPrimitiveEnum: + type: float + enum: [1.23, 2.45, 34.10] + x-deprecated: + - 34.10 + DoublePrimitiveEnum: + type: double + enum: [1.23, 2.45, 34.10] + x-deprecated: + - 34.10 + CharPrimitiveEnum: + type: char + enum: ['a', 'b', 'c', 'd'] + x-deprecated: + - c