From 2764d85a4d8792ca70ea3115789a25930dcc9561 Mon Sep 17 00:00:00 2001 From: altro3 Date: Sat, 21 Sep 2024 17:14:44 +0700 Subject: [PATCH] Added support deprecated extension for enum values. Added support primitive types in enum values Fixes for numeric type generating. --- .../AbstractMicronautJavaCodegen.java | 354 +++++++++++++- .../AbstractMicronautKotlinCodegen.java | 285 ++++++++++- .../openapi/generator/MnSchemaTypeUtil.java | 40 ++ .../io/micronaut/openapi/generator/Utils.java | 33 +- .../openapitools/codegen/DefaultCodegen.java | 4 +- .../client/doc/api_doc.mustache | 2 +- .../client/params/type.mustache | 4 +- .../client/test/api_test.groovy.mustache | 2 +- .../client/test/api_test.mustache | 2 +- .../java-micronaut/common/model/enum.mustache | 11 +- .../java-micronaut/common/model/pojo.mustache | 8 +- .../server/controllerOperationBody.mustache | 2 +- .../server/params/type.mustache | 4 +- .../test/controller_test.groovy.mustache | 4 +- .../server/test/controller_test.mustache | 4 +- .../common/model/enum.mustache | 3 + .../JavaMicronautClientCodegenTest.java | 186 +++++++ .../KotlinMicronautClientCodegenTest.java | 174 ++++++- .../src/test/resources/3_0/enum2.yml | 184 +++++++ .../resources/3_0/model-with-primitives.yml | 453 ++++++++++++++++++ .../src/test/resources/3_0/openmeteo.yml | 4 +- 21 files changed, 1690 insertions(+), 73 deletions(-) create mode 100644 openapi-generator/src/main/java/io/micronaut/openapi/generator/MnSchemaTypeUtil.java create mode 100644 openapi-generator/src/test/resources/3_0/enum2.yml create mode 100644 openapi-generator/src/test/resources/3_0/model-with-primitives.yml 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 6692930e14..15c399b0ba 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 @@ -26,6 +26,7 @@ import io.swagger.v3.oas.models.media.MediaType; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.parameters.Parameter; +import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.servers.Server; import io.swagger.v3.parser.util.SchemaTypeUtil; import org.apache.commons.lang3.StringUtils; @@ -40,6 +41,8 @@ import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.CodegenParameter; import org.openapitools.codegen.CodegenProperty; +import org.openapitools.codegen.CodegenResponse; +import org.openapitools.codegen.IJsonSchemaValidationProperties; import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.config.GlobalSettings; import org.openapitools.codegen.languages.AbstractJavaCodegen; @@ -75,6 +78,17 @@ import java.util.UUID; import java.util.stream.Collectors; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.FORMAT_INT16; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.FORMAT_INT8; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.FORMAT_SHORT; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_BYTE; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_CHAR; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_CHARACTER; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_DOUBLE; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_FLOAT; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_INT; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_LONG; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_SHORT; import static io.micronaut.openapi.generator.Utils.DEFAULT_BODY_PARAM_NAME; import static io.micronaut.openapi.generator.Utils.DIVIDE_OPERATIONS_BY_CONTENT_TYPE; import static io.micronaut.openapi.generator.Utils.EXT_ANNOTATIONS_CLASS; @@ -86,11 +100,14 @@ import static io.micronaut.openapi.generator.Utils.normalizeExtraAnnotations; import static io.micronaut.openapi.generator.Utils.processGenericAnnotations; import static io.micronaut.openapi.generator.Utils.readListOfStringsProperty; +import static io.swagger.v3.parser.util.SchemaTypeUtil.BYTE_FORMAT; +import static io.swagger.v3.parser.util.SchemaTypeUtil.INTEGER_TYPE; import static org.openapitools.codegen.CodegenConstants.API_PACKAGE; import static org.openapitools.codegen.CodegenConstants.INVOKER_PACKAGE; 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. @@ -191,6 +208,22 @@ protected AbstractMicronautJavaCodegen() { inlineSchemaOption.put("RESOLVE_INLINE_ENUMS", "true"); // CHECKSTYLE:ON + languageSpecificPrimitives.addAll(Set.of( + "char", + "float", + "double", + "byte", + "short", + "int", + "long", + "Byte", + "Short", + "Character" + )); + + typeMapping.put("char", "Character"); + typeMapping.put("byte", "Byte"); + GlobalSettings.setProperty(DIVIDE_OPERATIONS_BY_CONTENT_TYPE, "true"); // Set implemented features for user information @@ -703,6 +736,7 @@ public String toModelTestFilename(String name) { @Override public CodegenParameter fromParameter(Parameter p, Set imports) { var parameter = super.fromParameter(p, imports); + checkPrimitives(parameter, unaliasSchema(p.getSchema())); // if name is escaped var realName = parameter.paramName; if (realName.startsWith("_") && !parameter.baseName.startsWith("_") && isReservedWord(parameter.baseName)) { @@ -750,8 +784,16 @@ public CodegenParameter fromParameter(Parameter p, Set imports) { } @Override - public CodegenProperty fromProperty(String name, Schema p, boolean required, boolean schemaIsFromAdditionalProperties) { - var property = super.fromProperty(name, p, required, schemaIsFromAdditionalProperties); + public CodegenResponse fromResponse(String responseCode, ApiResponse response) { + var resp = super.fromResponse(responseCode, response); + checkPrimitives(resp, (Schema) resp.schema); + return resp; + } + + @Override + public CodegenProperty fromProperty(String name, Schema schema, boolean required, boolean schemaIsFromAdditionalProperties) { + var property = super.fromProperty(name, schema, required, schemaIsFromAdditionalProperties); + checkPrimitives(property, unaliasSchema(schema)); // if name is escaped var realName = property.name; @@ -769,21 +811,21 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo } property.vendorExtensions.put("realName", realName); - if (p != null && p.get$ref() != null) { - p = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, p.get$ref()); + if (schema != null && schema.get$ref() != null) { + schema = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, schema.get$ref()); } String defaultValueInit; var items = property.items; if (items == null) { defaultValueInit = calcDefaultValues(null, null, false, false, - false, false, false, false, false, p).getLeft(); + false, false, false, false, false, schema).getLeft(); } else { defaultValueInit = calcDefaultValues(items.datatypeWithEnum, items.dataType, items.getIsEnumOrRef(), property.isArray, items.isString, items.isNumeric, items.isFloat, items.isMap, items.isNullable, - p).getLeft(); + schema).getLeft(); } - if (p != null && ModelUtils.isEnumSchema(p)) { + if (schema != null && ModelUtils.isEnumSchema(schema)) { var enumVarName = toEnumVarName(property.defaultValue, property.dataType); if (enumVarName != null) { defaultValueInit = property.dataType + "." + enumVarName; @@ -803,7 +845,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 @@ -1304,9 +1384,172 @@ 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); } } @@ -1692,6 +1936,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 3bfd509378..9bc824c366 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 @@ -29,6 +29,7 @@ import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import io.swagger.v3.oas.models.parameters.Parameter; +import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.servers.Server; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; @@ -42,7 +43,9 @@ import org.openapitools.codegen.CodegenOperation; import org.openapitools.codegen.CodegenParameter; import org.openapitools.codegen.CodegenProperty; +import org.openapitools.codegen.CodegenResponse; import org.openapitools.codegen.DefaultCodegen; +import org.openapitools.codegen.IJsonSchemaValidationProperties; import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.config.GlobalSettings; import org.openapitools.codegen.languages.AbstractKotlinCodegen; @@ -79,6 +82,17 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.FORMAT_INT16; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.FORMAT_INT8; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.FORMAT_SHORT; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_BYTE; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_CHAR; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_CHARACTER; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_DOUBLE; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_FLOAT; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_INT; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_LONG; +import static io.micronaut.openapi.generator.MnSchemaTypeUtil.TYPE_SHORT; import static io.micronaut.openapi.generator.Utils.DEFAULT_BODY_PARAM_NAME; import static io.micronaut.openapi.generator.Utils.DIVIDE_OPERATIONS_BY_CONTENT_TYPE; import static io.micronaut.openapi.generator.Utils.EXT_ANNOTATIONS_CLASS; @@ -90,6 +104,8 @@ import static io.micronaut.openapi.generator.Utils.normalizeExtraAnnotations; import static io.micronaut.openapi.generator.Utils.processGenericAnnotations; import static io.micronaut.openapi.generator.Utils.readListOfStringsProperty; +import static io.swagger.v3.parser.util.SchemaTypeUtil.BYTE_FORMAT; +import static io.swagger.v3.parser.util.SchemaTypeUtil.INTEGER_TYPE; import static org.openapitools.codegen.CodegenConstants.API_PACKAGE; import static org.openapitools.codegen.CodegenConstants.INVOKER_PACKAGE; import static org.openapitools.codegen.CodegenConstants.MODEL_PACKAGE; @@ -1120,9 +1136,165 @@ public String toEnumValue(String value, String datatype) { } } + private void checkPrimitives(IJsonSchemaValidationProperties obj, Schema schema) { + + if (obj == null) { + return; + } + + CodegenModel model = null; + CodegenParameter param = null; + CodegenProperty prop = null; + CodegenResponse response = null; + if (obj instanceof CodegenModel m) { + model = m; + } else if (obj instanceof CodegenProperty p) { + prop = p; + } else if (obj instanceof CodegenParameter p) { + param = p; + } else if (obj instanceof CodegenResponse r) { + response = r; + } + var extMap = model != null ? model.vendorExtensions : param != null ? param.vendorExtensions : prop != null ? prop.vendorExtensions : response.vendorExtensions; + var dataType = model != null ? model.dataType : param != null ? param.dataType : prop != null ? prop.dataType : response.dataType; + var dataTypeWithEnum = param != null ? param.datatypeWithEnum : prop != null ? prop.datatypeWithEnum : null; + + if (schema == null) { + extMap.put("baseType", dataType); + return; + } + + var isNumeric = model != null ? model.isNumeric : param != null ? param.isNumeric : prop != null ? prop.isNumeric : response.isNumeric; + var isNumber = model != null ? model.isNumber : param != null ? param.isNumber : prop != null ? prop.isNumber : response.isNumber; + var isShort = model != null ? model.isShort : param != null ? param.isShort : prop != null ? prop.isShort : response.isShort; + var isInteger = model != null ? model.isInteger : param != null ? param.isInteger : prop != null ? prop.isInteger : response.isInteger; + var isLong = model != null ? model.isLong : param != null ? param.isLong : prop != null ? prop.isLong : response.isLong; + var isFloat = model != null ? model.isFloat : param != null ? param.isFloat : prop != null ? prop.isFloat : response.isFloat; + var isDouble = model != null ? model.isDouble : param != null ? param.isDouble : prop != null ? prop.isDouble : response.isDouble; + + var extensions = schema.getExtensions(); + var format = extensions != null ? extensions.get("x-format") : null; + if (format == null) { + format = schema.getFormat() == null ? "object" : schema.getFormat(); + } + var schemaType = extensions != null ? extensions.get("x-type") : null; + if (schemaType == null) { + schemaType = schema.getType() == null ? "object" : schema.getType(); + } + var baseType = dataType; + + if (TYPE_CHAR.equals(schemaType) || TYPE_CHARACTER.equals(schemaType)) { + baseType = "char"; + dataType = "Char"; + dataTypeWithEnum = "Char"; + } else if (TYPE_BYTE.equals(schemaType)) { + baseType = "byte"; + dataType = "Byte"; + dataTypeWithEnum = "Byte"; + isNumeric = true; + isNumber = true; + isInteger = true; + } else if (INTEGER_TYPE.equals(schemaType) && (FORMAT_INT8.equals(format) || BYTE_FORMAT.equals(format))) { + baseType = "Byte"; + dataType = "Byte"; + dataTypeWithEnum = "Byte"; + isNumeric = true; + isNumber = true; + isInteger = true; + } else if (TYPE_SHORT.equals(schemaType)) { + baseType = "short"; + dataType = "Short"; + dataTypeWithEnum = "Short"; + isNumeric = true; + isNumber = true; + isShort = true; + } else if (INTEGER_TYPE.equals(schemaType) && (FORMAT_INT16.equals(format) || FORMAT_SHORT.equals(format))) { + baseType = "Short"; + dataType = "Short"; + dataTypeWithEnum = "Short"; + isNumeric = true; + isNumber = true; + isShort = true; + } else if (TYPE_INT.equals(schemaType)) { + baseType = "int"; + dataType = "Int"; + dataTypeWithEnum = "Int"; + isNumeric = true; + isNumber = true; + isInteger = true; + } else if (TYPE_LONG.equals(schemaType)) { + baseType = "long"; + dataType = "Long"; + dataTypeWithEnum = "Long"; + isNumeric = true; + isNumber = true; + isLong = true; + } else if (TYPE_FLOAT.equals(schemaType)) { + baseType = "float"; + dataType = "Float"; + dataTypeWithEnum = "Float"; + isNumeric = true; + isNumber = true; + isFloat = true; + } else if (TYPE_DOUBLE.equals(schemaType)) { + baseType = "double"; + dataType = "Double"; + dataTypeWithEnum = "Double"; + isNumeric = true; + isNumber = true; + isDouble = true; + } + + extMap.put("baseType", baseType); + // kotlin has not primitive types + extMap.put("isPrimitive", false); + + if (model != null) { + model.dataType = dataType; + model.isNumeric = isNumeric; + model.isNumber = isNumber; + model.isShort = isShort; + model.isInteger = isInteger; + model.isLong = isLong; + model.isFloat = isFloat; + model.isDouble = isDouble; + } else if (prop != null) { + prop.dataType = dataType; + prop.datatypeWithEnum = dataTypeWithEnum; + prop.isNumeric = isNumeric; + prop.isNumber = isNumber; + prop.isShort = isShort; + prop.isInteger = isInteger; + prop.isLong = isLong; + prop.isFloat = isFloat; + prop.isDouble = isDouble; + } else if (param != null) { + param.dataType = dataType; + param.datatypeWithEnum = dataTypeWithEnum; + param.isNumeric = isNumeric; + param.isNumber = isNumber; + param.isShort = isShort; + param.isInteger = isInteger; + param.isLong = isLong; + param.isFloat = isFloat; + param.isDouble = isDouble; + } else { + response.dataType = dataType; + response.isNumeric = isNumeric; + response.isNumber = isNumber; + response.isShort = isShort; + response.isInteger = isInteger; + response.isLong = isLong; + response.isFloat = isFloat; + response.isDouble = isDouble; + } + } + @Override public CodegenModel fromModel(String name, Schema schema) { CodegenModel model = super.fromModel(name, schema); + checkPrimitives(model, unaliasSchema(schema)); + if (!model.oneOf.isEmpty()) { if (useOneOfInterfaces) { model.vendorExtensions.put("x-is-one-of-interface", true); @@ -1247,6 +1419,7 @@ public String toModelName(final String name) { @Override public CodegenParameter fromParameter(Parameter p, Set imports) { var parameter = super.fromParameter(p, imports); + checkPrimitives(parameter, unaliasSchema(p.getSchema())); // if name is escaped var realName = parameter.paramName; if (realName.contains("`")) { @@ -1294,8 +1467,16 @@ public CodegenParameter fromParameter(Parameter p, Set imports) { } @Override - public CodegenProperty fromProperty(String name, Schema p, boolean required, boolean schemaIsFromAdditionalProperties) { - var property = super.fromProperty(name, p, required, schemaIsFromAdditionalProperties); + public CodegenResponse fromResponse(String responseCode, ApiResponse response) { + var resp = super.fromResponse(responseCode, response); + checkPrimitives(resp, (Schema) resp.schema); + return resp; + } + + @Override + public CodegenProperty fromProperty(String name, Schema schema, boolean required, boolean schemaIsFromAdditionalProperties) { + var property = super.fromProperty(name, schema, required, schemaIsFromAdditionalProperties); + checkPrimitives(property, unaliasSchema(schema)); // if name is escaped var realName = property.name; @@ -1306,18 +1487,18 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo } property.vendorExtensions.put("realName", realName); - if (p != null && p.get$ref() != null) { - p = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, p.get$ref()); + if (schema != null && schema.get$ref() != null) { + schema = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, schema.get$ref()); } String defaultValueInit; var items = property.items; if (items == null) { - defaultValueInit = calcDefaultValues(null, null, false, p).getLeft(); + defaultValueInit = calcDefaultValues(null, null, false, schema).getLeft(); } else { - defaultValueInit = calcDefaultValues(items.datatypeWithEnum, items.dataType, items.getIsEnumOrRef(), p).getLeft(); + defaultValueInit = calcDefaultValues(items.datatypeWithEnum, items.dataType, items.getIsEnumOrRef(), schema).getLeft(); } - if (p != null && ModelUtils.isEnumSchema(p)) { + if (schema != null && ModelUtils.isEnumSchema(schema)) { var enumVarName = toEnumVarName(property.defaultValue, property.dataType); if (enumVarName != null) { defaultValueInit = property.dataType + "." + enumVarName; @@ -1391,11 +1572,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) { @@ -1663,6 +1863,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/MnSchemaTypeUtil.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MnSchemaTypeUtil.java new file mode 100644 index 0000000000..9b9fcc130c --- /dev/null +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MnSchemaTypeUtil.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.micronaut.openapi.generator; + +/** + * Schema type utils and constants. + * + * @since 6.12.4 + */ +public final class MnSchemaTypeUtil { + + public static final String TYPE_CHAR = "char"; + public static final String TYPE_CHARACTER = "character"; + public static final String TYPE_BYTE = "byte"; + public static final String TYPE_SHORT = "short"; + public static final String TYPE_INT = "int"; + public static final String TYPE_LONG = "long"; + public static final String TYPE_FLOAT = "float"; + public static final String TYPE_DOUBLE = "double"; + + public static final String FORMAT_INT8 = "int8"; + public static final String FORMAT_INT16 = "int16"; + public static final String FORMAT_SHORT = "short"; + + private MnSchemaTypeUtil() { + } +} 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 efcbdfb370..9f7daca204 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 @@ -204,14 +204,16 @@ private static String genericAnnotations(CodegenProperty prop, boolean isGenerat result.append(") "); } } - if (prop.isNullable) { - if (isGenerateHardNullable) { - result.append("@Nullable(inherited = true) "); - } else { - result.append("@Nullable "); + if (!(Boolean) prop.vendorExtensions.get("isPrimitive")) { + if (prop.isNullable) { + if (isGenerateHardNullable) { + result.append("@Nullable(inherited = true) "); + } else { + result.append("@Nullable "); + } + } else if (!containsNotEmpty) { + result.append("@NotNull "); } - } else if (!containsNotEmpty) { - result.append("@NotNull "); } if (StringUtils.isNotEmpty(prop.minimum)) { try { @@ -271,9 +273,8 @@ private static boolean isPrimitive(String type) { return false; } return switch (type) { - case "array", "string", "boolean", "byte", "uri", "url", "uuid", "email", "integer", "long", "float", - "double", - "number", "partial-time", "date", "date-time", "bigdecimal", "biginteger" -> true; + case "array", "char", "character", "string", "boolean", "byte", "short", "int", "integer", "long", "uri", "url", "uuid", "email", "float", + "double", "number", "partial-time", "date", "date-time", "bigdecimal", "biginteger" -> true; default -> false; }; } @@ -298,6 +299,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 @@ -310,9 +317,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/java/org/openapitools/codegen/DefaultCodegen.java b/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 2558a22261..34d52b8a65 100644 --- a/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -2422,7 +2422,7 @@ public String toAllOfName(List names, Schema composedSchema) { if (exts != null && exts.containsKey("x-all-of-name")) { return (String) exts.get("x-all-of-name"); } - if (names.size() == 0) { + if (names.isEmpty()) { LOGGER.error("allOf has no member defined: {}. Default to ERROR_ALLOF_SCHEMA", composedSchema); return "ERROR_ALLOF_SCHEMA"; } else if (names.size() == 1) { @@ -5290,7 +5290,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set imports) parameterModelName = getParameterDataType(parameter, parameterSchema); CodegenProperty prop; if (this instanceof RustServerCodegen) { - // for rust server, we need to do somethings special as it uses + // for rust server, we need to do something special as it uses // $ref (e.g. #components/schemas/Pet) to determine whether it's a model prop = fromProperty(parameter.getName(), parameterSchema, false); } else if (getUseInlineModelResolver()) { diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/doc/api_doc.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/doc/api_doc.mustache index 9bbd710a76..76489c85bf 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/doc/api_doc.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/doc/api_doc.mustache @@ -52,7 +52,7 @@ More information can be found inside [Inversion of Control guide section](https: {{#allParams}}{{#-last}}### Parameters | Name | Type | Description | Notes | |------------- | ------------- | ------------- | -------------| -{{#allParams}}| **{{paramName}}** | {{#isPrimitiveType}}`{{dataType}}`{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}`{{dataType}}`{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} |{{^required}} [optional parameter]{{/required}}{{#defaultValue}} [default to `{{defaultValue}}`]{{/defaultValue}}{{#allowableValues}} [enum: {{#values}}`{{{.}}}`{{^-last}}, {{/-last}}{{/values}}]{{/allowableValues}} | +{{#allParams}}| **{{paramName}}** | {{#isPrimitiveType}}`{{vendorExtensions.baseType}}`{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}`{{dataType}}`{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} |{{^required}} [optional parameter]{{/required}}{{#defaultValue}} [default to `{{defaultValue}}`]{{/defaultValue}}{{#allowableValues}} [enum: {{#values}}`{{{.}}}`{{^-last}}, {{/-last}}{{/values}}]{{/allowableValues}} | {{/allParams}}{{/-last}}{{/allParams}} {{#returnType}}### Return type diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/params/type.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/params/type.mustache index d17641dc57..dc8f5ddf0d 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/params/type.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/params/type.mustache @@ -1,3 +1,3 @@ {{^isDate}}{{^isDateTime}}{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{/isDateTime}}{{/isDate}} -{{#isDateTime}}{{#vendorExtensions.formatPattern}}@Format("{{{vendorExtensions.formatPattern}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateTimeFormat}}@Format("{{{dateTimeFormat}}}"){{/dateTimeFormat}}{{/vendorExtensions.formatPattern}} {{{dataType}}} {{/isDateTime}} -{{#isDate}}{{#vendorExtensions.formatPattern}}@Format("{{{vendorExtensions.formatPattern}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateFormat}}@Format("{{{dateFormat}}}"){{/dateFormat}}{{/vendorExtensions.formatPattern}} {{{dataType}}} {{/isDate}} \ No newline at end of file +{{#isDateTime}}{{#vendorExtensions.formatPattern}}@Format("{{{vendorExtensions.formatPattern}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateTimeFormat}}@Format("{{{dateTimeFormat}}}"){{/dateTimeFormat}}{{/vendorExtensions.formatPattern}} {{{vendorExtensions.baseType}}} {{/isDateTime}} +{{#isDate}}{{#vendorExtensions.formatPattern}}@Format("{{{vendorExtensions.formatPattern}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateFormat}}@Format("{{{dateFormat}}}"){{/dateFormat}}{{/vendorExtensions.formatPattern}} {{{vendorExtensions.baseType}}} {{/isDate}} \ No newline at end of file diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.groovy.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.groovy.mustache index 2014c0fbf9..cb01224072 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.groovy.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.groovy.mustache @@ -36,7 +36,7 @@ class {{classname}}Spec extends Specification { void '{{operationId}}() test'() { given: {{#allParams}} - {{{dataType}}} {{paramName}} = {{{vendorExtensions.groovyExample}}} + {{{vendorExtensions.baseType}}} {{paramName}} = {{{vendorExtensions.groovyExample}}} {{/allParams}} when: diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.mustache index 586ab3769f..17076aaf7f 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/client/test/api_test.mustache @@ -38,7 +38,7 @@ class {{classname}}Test { void {{operationId}}Test() { // given {{#allParams}} - {{{dataType}}} {{paramName}} = {{{example}}}; + {{{vendorExtensions.baseType}}} {{paramName}} = {{{example}}}; {{/allParams}} // when 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..1be64f084e 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 @@ -3,7 +3,7 @@ */ {{#withXml}} @XmlType(name="{{datatypeWithEnum}}") -@XmlEnum({{dataType}}.class) +@XmlEnum({{vendorExtensions.baseType}}.class) {{/withXml}} {{#lombok}} @RequiredArgsConstructor @@ -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/java-micronaut/common/model/pojo.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache index 866c26d21e..5e02bd6b51 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/common/model/pojo.mustache @@ -131,10 +131,10 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE {{/vendorExtensions.x-field-extra-annotation}} {{#vendorExtensions.x-is-jackson-optional-nullable}} {{#isContainer}} - private JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>undefined(); + private JsonNullable<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}> {{name}} = JsonNullable.<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}>undefined(); {{/isContainer}} {{^isContainer}} - private JsonNullable<{{{datatypeWithEnum}}}> {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>{{#defaultValue}}of({{{.}}}){{/defaultValue}}{{^defaultValue}}undefined(){{/defaultValue}}; + private JsonNullable<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}> {{name}} = JsonNullable.<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}>{{#defaultValue}}of({{{.}}}){{/defaultValue}}{{^defaultValue}}undefined(){{/defaultValue}}; {{/isContainer}} {{/vendorExtensions.x-is-jackson-optional-nullable}} {{^vendorExtensions.x-is-jackson-optional-nullable}} @@ -142,7 +142,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE private {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}} {{name}}{{#required}}{{^requiredPropertiesInConstructor}}{{#vendorExtensions.defaultValueIsNotNull}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{/vendorExtensions.defaultValueIsNotNull}}{{/requiredPropertiesInConstructor}}{{/required}}; {{/isContainer}} {{^isContainer}} - {{#isDiscriminator}}protected{{/isDiscriminator}}{{^isDiscriminator}}private{{/isDiscriminator}} {{{datatypeWithEnum}}} {{name}}{{#vendorExtensions.defaultValueIsNotNull}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{/vendorExtensions.defaultValueIsNotNull}}; + {{#isDiscriminator}}protected{{/isDiscriminator}}{{^isDiscriminator}}private{{/isDiscriminator}} {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}} {{name}}{{#vendorExtensions.defaultValueIsNotNull}}{{#vendorExtensions.defaultValueInit}} = {{{.}}}{{/vendorExtensions.defaultValueInit}}{{/vendorExtensions.defaultValueIsNotNull}}; {{/isContainer}} {{/vendorExtensions.x-is-jackson-optional-nullable}} {{/formatNoEmptyLines}} @@ -337,7 +337,7 @@ public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#vendorE public {{classname}} put{{nameInCamelCase}}Item(String key, {{{items.datatypeWithEnum}}} {{name}}Item) { {{#vendorExtensions.x-is-jackson-optional-nullable}} if ({{name}} == null || !{{name}}.isPresent()) { - {{name}} = JsonNullable.<{{{datatypeWithEnum}}}>of({{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}}); + {{name}} = JsonNullable.<{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}>of({{{defaultValue}}}{{^defaultValue}}new HashMap<>(){{/defaultValue}}); } try { {{name}}.get().put(key, {{name}}Item); diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/controllerOperationBody.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/controllerOperationBody.mustache index b1bf1c667a..5b07e41663 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/controllerOperationBody.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/controllerOperationBody.mustache @@ -12,7 +12,7 @@ {{!The body is generated to verify that example values are passed correctly}} {{#allParams}} {{^isFile}} - {{{dataType}}} {{paramName}}Expected = {{{example}}}; + {{{vendorExtensions.baseType}}} {{paramName}}Expected = {{{example}}}; assert {{paramName}}.equals({{paramName}}Expected) : "The parameter {{paramName}} was expected to match its example value"; {{/isFile}} {{/allParams}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/params/type.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/params/type.mustache index 15e874eaf6..a94ab25eb3 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/params/type.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/params/type.mustache @@ -2,10 +2,10 @@ {{{vendorExtensions.typeWithGenericAnnotations}}} {{/isDate}}{{/isDateTime}}{{/isEnum}} {{#isDateTime}} - {{#vendorExtensions.formatPattern}}@Format("{{{.}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateTimeFormat}}@Format("{{{.}}}"){{/dateTimeFormat}}{{/vendorExtensions.formatPattern}} {{{dataType}}} + {{#vendorExtensions.formatPattern}}@Format("{{{.}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateTimeFormat}}@Format("{{{.}}}"){{/dateTimeFormat}}{{/vendorExtensions.formatPattern}} {{#isEnum}}{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{/isEnum}}{{^isEnum}}{{{vendorExtensions.typeWithGenericAnnotations}}}{{/isEnum}} {{/isDateTime}} {{#isDate}} - {{#vendorExtensions.formatPattern}}@Format("{{{.}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateFormat}}@Format("{{{.}}}"){{/dateFormat}}{{/vendorExtensions.formatPattern}} {{{dataType}}} + {{#vendorExtensions.formatPattern}}@Format("{{{.}}}"){{/vendorExtensions.formatPattern}}{{^vendorExtensions.formatPattern}}{{#dateFormat}}@Format("{{{.}}}"){{/dateFormat}}{{/vendorExtensions.formatPattern}} {{#isEnum}}{{{vendorExtensions.typeWithEnumWithGenericAnnotations}}}{{/isEnum}}{{^isEnum}}{{{vendorExtensions.typeWithGenericAnnotations}}}{{/isEnum}} {{/isDate}} {{#isEnum}} {{{vendorExtensions.typeWithEnumWithGenericAnnotations}}} diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.groovy.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.groovy.mustache index a019363fe2..ab2c91ec55 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.groovy.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.groovy.mustache @@ -61,7 +61,7 @@ class {{classname}}Spec extends Specification { def '{{operationId}}() method test'() { given: {{#allParams}} - {{{dataType}}} {{paramName}} = {{{vendorExtensions.groovyExample}}} + {{{vendorExtensions.baseType}}} {{paramName}} = {{{vendorExtensions.groovyExample}}} {{/allParams}} when: @@ -94,7 +94,7 @@ class {{classname}}Spec extends Specification { given: {{!Create the body}} {{#bodyParam}} - {{{dataType}}} body = {{{vendorExtensions.groovyExample}}} + {{{vendorExtensions.baseType}}} body = {{{vendorExtensions.groovyExample}}} {{/bodyParam}} {{#formParams.0}} var form = [ diff --git a/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.mustache b/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.mustache index e7718c8c75..5ccef78358 100644 --- a/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.mustache +++ b/openapi-generator/src/main/resources/templates/java-micronaut/server/test/controller_test.mustache @@ -70,7 +70,7 @@ class {{classname}}Test { void {{operationId}}MethodTest() { // given {{#allParams}} - {{{dataType}}} {{paramName}} = {{{example}}}; + {{{vendorExtensions.baseType}}} {{paramName}} = {{{example}}}; {{/allParams}} // when @@ -103,7 +103,7 @@ class {{classname}}Test { // given {{!Create the body}} {{#bodyParam}} - {{{dataType}}} body = {{{example}}}; + {{{vendorExtensions.baseType}}} body = {{{example}}}; {{/bodyParam}} {{#formParams.0}} var form = new HashMap(); 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/JavaMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java index 00ee64e869..be4cf3d08c 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 @@ -854,4 +854,190 @@ void testAdditionalAnnotations2() { assertFileContains(path + "model/Subject.java", "@java.io.MyAnnotation3"); assertFileContains(path + "model/PersonSex.java", "@java.io.MyAnnotation41\n", "@java.io.MyAnnotation42\n", "@java.io.MyAnnotation43\n"); } + + @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), + """); + } + + @Test + void testPrimitives() { + + var codegen = new JavaMicronautClientCodegen(); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/model-with-primitives.yml", CodegenConstants.APIS, CodegenConstants.MODELS); + String basePath = outputPath + "src/main/java/org/openapitools/"; + + assertFileContains(basePath + "api/ParametersApi.java", + "@QueryValue(\"name\") @NotNull String name", + "@QueryValue(\"byteType\") @NotNull Byte byteType", + "@QueryValue(\"byteType2\") @NotNull Byte byteType2", + "@QueryValue(\"shortType\") @NotNull Short shortType", + "@QueryValue(\"shortType2\") @NotNull Short shortType2", + "@QueryValue(\"intType\") @NotNull Integer intType", + "@QueryValue(\"longType\") @NotNull Long longType", + "@QueryValue(\"boolType\") @NotNull Boolean boolType", + "@QueryValue(\"decimalType\") @NotNull BigDecimal decimalType", + "@QueryValue(\"floatType\") @NotNull Float floatType", + "@QueryValue(\"doubleType\") @NotNull Double doubleType", + "@QueryValue(\"bytePrimitiveType\") @NotNull Byte bytePrimitiveType", + "@QueryValue(\"shortPrimitiveType\") @NotNull Short shortPrimitiveType", + "@QueryValue(\"intPrimitiveType\") @NotNull Integer intPrimitiveType", + "@QueryValue(\"longPrimitiveType\") @NotNull Long longPrimitiveType", + "@QueryValue(\"floatPrimitiveType\") @NotNull Float floatPrimitiveType", + "@QueryValue(\"doublePrimitiveType\") @NotNull Double doublePrimitiveType", + "@QueryValue(\"charPrimitiveType\") @NotNull Character charPrimitiveType", + "@QueryValue(\"bytePrimitiveTypes\") @NotNull List bytePrimitiveTypes", + "@QueryValue(\"shortPrimitiveTypes\") @NotNull List shortPrimitiveTypes", + "@QueryValue(\"intPrimitiveTypes\") @NotNull List intPrimitiveTypes", + "@QueryValue(\"longPrimitiveTypes\") @NotNull List longPrimitiveTypes", + "@QueryValue(\"floatPrimitiveTypes\") @NotNull List floatPrimitiveTypes", + "@QueryValue(\"doublePrimitiveTypes\") @NotNull List doublePrimitiveTypes", + "@QueryValue(\"charPrimitiveTypes\") @NotNull List charPrimitiveTypes", + "@QueryValue(\"byteTypes\") @NotNull List<@NotNull Byte> byteTypes", + "@QueryValue(\"byteTypes2\") @NotNull List<@NotNull Byte> byteTypes2", + "@QueryValue(\"shortTypes\") @NotNull List<@NotNull Short> shortTypes", + "@QueryValue(\"shortTypes2\") @NotNull List<@NotNull Short> shortTypes2", + "@QueryValue(\"intTypes\") @NotNull List<@NotNull Integer> intTypes", + "@QueryValue(\"longTypes\") @NotNull List<@NotNull Long> longTypes" + ); + + assertFileContains(basePath + "model/Obj.java", + "private String name", + "private Byte byteType", + "private Byte byteType2", + "private Short shortType", + "private Short shortType2", + "private Integer intType", + "private Long longType", + "private Boolean boolType", + "private BigDecimal decimalType", + "private Float floatType", + "private Double doubleType", + "private Byte bytePrimitiveType", + "private Short shortPrimitiveType", + "private Integer intPrimitiveType", + "private Long longPrimitiveType", + "private Float floatPrimitiveType", + "private Double doublePrimitiveType", + "private Character charPrimitiveType", + "private List bytePrimitiveTypes", + "private List shortPrimitiveTypes", + "private List intPrimitiveTypes", + "private List longPrimitiveTypes", + "private List floatPrimitiveTypes", + "private List doublePrimitiveTypes", + "private List charPrimitiveTypes", + "private List<@NotNull Byte> byteTypes", + "private List<@NotNull Byte> byteTypes2", + "private List<@NotNull Short> shortTypes", + "private List<@NotNull Short> shortTypes2", + "private List<@NotNull Integer> intTypes", + "private List<@NotNull Long> longTypes" + ); + } } 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 9623743c0f..70de70ab9a 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 @@ -464,9 +464,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 @@ -874,4 +874,172 @@ void testAdditionalAnnotations2() { assertFileContains(path + "model/Subject.kt", "@java.io.MyAnnotation3"); assertFileContains(path + "model/PersonSex.kt", "@java.io.MyAnnotation41\n", "@java.io.MyAnnotation42\n", "@java.io.MyAnnotation43\n"); } + + @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), + """); + } + + @Test + void testPrimitives() { + + var codegen = new KotlinMicronautClientCodegen(); + String outputPath = generateFiles(codegen, "src/test/resources/3_0/model-with-primitives.yml", CodegenConstants.APIS, CodegenConstants.MODELS); + String basePath = outputPath + "src/main/kotlin/org/openapitools/"; + + assertFileContains(basePath + "api/ParametersApi.kt", + "@QueryValue(\"name\") @NotNull name: String", + "@QueryValue(\"byteType\") @NotNull byteType: Byte", + "@QueryValue(\"byteType2\") @NotNull byteType2: Byte", + "@QueryValue(\"shortType\") @NotNull shortType: Short", + "@QueryValue(\"shortType2\") @NotNull shortType2: Short", + "@QueryValue(\"intType\") @NotNull intType: Int", + "@QueryValue(\"longType\") @NotNull longType: Long", + "@QueryValue(\"boolType\") @NotNull boolType: Boolean", + "@QueryValue(\"decimalType\") @NotNull decimalType: BigDecimal", + "@QueryValue(\"floatType\") @NotNull floatType: Float", + "@QueryValue(\"doubleType\") @NotNull doubleType: Double", + "@QueryValue(\"bytePrimitiveType\") @NotNull bytePrimitiveType: Byte", + "@QueryValue(\"shortPrimitiveType\") @NotNull shortPrimitiveType: Short", + "@QueryValue(\"intPrimitiveType\") @NotNull intPrimitiveType: Int", + "@QueryValue(\"longPrimitiveType\") @NotNull longPrimitiveType: Long", + "@QueryValue(\"floatPrimitiveType\") @NotNull floatPrimitiveType: Float", + "@QueryValue(\"doublePrimitiveType\") @NotNull doublePrimitiveType: Double", + "@QueryValue(\"charPrimitiveType\") @NotNull charPrimitiveType: Char", + "@QueryValue(\"bytePrimitiveTypes\") @NotNull bytePrimitiveTypes: List<@NotNull Byte>", + "@QueryValue(\"shortPrimitiveTypes\") @NotNull shortPrimitiveTypes: List<@NotNull Short>", + "@QueryValue(\"intPrimitiveTypes\") @NotNull intPrimitiveTypes: List<@NotNull Int>", + "@QueryValue(\"longPrimitiveTypes\") @NotNull longPrimitiveTypes: List<@NotNull Long>", + "@QueryValue(\"floatPrimitiveTypes\") @NotNull floatPrimitiveTypes: List<@NotNull Float>", + "@QueryValue(\"doublePrimitiveTypes\") @NotNull doublePrimitiveTypes: List<@NotNull Double>", + "@QueryValue(\"charPrimitiveTypes\") @NotNull charPrimitiveTypes: List<@NotNull Char>", + "@QueryValue(\"byteTypes\") @NotNull byteTypes: List<@NotNull Byte>", + "@QueryValue(\"byteTypes2\") @NotNull byteTypes2: List<@NotNull Byte>", + "@QueryValue(\"shortTypes\") @NotNull shortTypes: List<@NotNull Short>", + "@QueryValue(\"shortTypes2\") @NotNull shortTypes2: List<@NotNull Short>", + "@QueryValue(\"intTypes\") @NotNull intTypes: List<@NotNull Int>", + "@QueryValue(\"longTypes\") @NotNull longTypes: List<@NotNull Long>" + ); + + assertFileContains(basePath + "model/Obj.kt", + "name: String?", + "byteType: Byte?", + "byteType2: Byte?", + "shortType: Short?", + "shortType2: Short?", + "intType: Int?", + "longType: Long?", + "boolType: Boolean?", + "decimalType: BigDecimal?", + "floatType: Float?", + "doubleType: Double?", + "bytePrimitiveType: Byte?", + "shortPrimitiveType: Short?", + "intPrimitiveType: Int?", + "longPrimitiveType: Long?", + "floatPrimitiveType: Float?", + "doublePrimitiveType: Double?", + "charPrimitiveType: Char?", + "bytePrimitiveTypes: List<@NotNull Byte>?", + "shortPrimitiveTypes: List<@NotNull Short>?", + "intPrimitiveTypes: List<@NotNull Int>?", + "longPrimitiveTypes: List<@NotNull Long>?", + "floatPrimitiveTypes: List<@NotNull Float>?", + "doublePrimitiveTypes: List<@NotNull Double>?", + "charPrimitiveTypes: List<@NotNull Char>?", + "byteTypes: List<@NotNull Byte>?", + "byteTypes2: List<@NotNull Byte>?", + "shortTypes: List<@NotNull Short>?", + "shortTypes2: List<@NotNull Short>", + "intTypes: List<@NotNull Int>", + "longTypes: List<@NotNull Long>" + ); + } } 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 diff --git a/openapi-generator/src/test/resources/3_0/model-with-primitives.yml b/openapi-generator/src/test/resources/3_0/model-with-primitives.yml new file mode 100644 index 0000000000..53170ecc9e --- /dev/null +++ b/openapi-generator/src/test/resources/3_0/model-with-primitives.yml @@ -0,0 +1,453 @@ +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: + /sendByRef: + get: + tags: [ parameters ] + parameters: + - name: name + in: query + required: true + schema: + $ref: "#/components/schemas/StringType" + - name: byteType + in: query + required: true + schema: + $ref: "#/components/schemas/ByteType" + - name: byteType2 + in: query + required: true + schema: + $ref: "#/components/schemas/ByteType2" + - name: byteType3 + in: query + required: true + schema: + $ref: "#/components/schemas/ByteType3" + - name: byteType4 + in: query + required: true + schema: + $ref: "#/components/schemas/ByteType4" + - name: shortType + in: query + required: true + schema: + $ref: "#/components/schemas/ShortType" + - name: shortType2 + in: query + required: true + schema: + $ref: "#/components/schemas/ShortType2" + - name: shortType3 + in: query + required: true + schema: + $ref: "#/components/schemas/ShortType3" + - name: shortType4 + in: query + required: true + schema: + $ref: "#/components/schemas/ShortType4" + - name: intType + in: query + required: true + schema: + $ref: "#/components/schemas/IntType" + - name: longType + in: query + required: true + schema: + $ref: "#/components/schemas/LongType" + - name: boolType + in: query + required: true + schema: + $ref: "#/components/schemas/BooleanType" + - name: decimalType + in: query + required: true + schema: + $ref: "#/components/schemas/DecimalType" + - name: floatType + in: query + required: true + schema: + $ref: "#/components/schemas/FloatType" + - name: doubleType + in: query + required: true + schema: + $ref: "#/components/schemas/DoubleType" + - name: bytePrimitiveType + in: query + required: true + schema: + $ref: "#/components/schemas/BytePrimitiveType" + - name: bytePrimitiveType2 + in: query + required: true + schema: + $ref: "#/components/schemas/BytePrimitiveType2" + - name: shortPrimitiveType + in: query + required: true + schema: + $ref: "#/components/schemas/ShortPrimitiveType" + - name: shortPrimitiveType2 + in: query + required: true + schema: + $ref: "#/components/schemas/ShortPrimitiveType2" + - name: intPrimitiveType + in: query + required: true + schema: + $ref: "#/components/schemas/IntPrimitiveType" + - name: longPrimitiveType + in: query + required: true + schema: + $ref: "#/components/schemas/LongPrimitiveType" + - name: floatPrimitiveType + in: query + required: true + schema: + $ref: "#/components/schemas/FloatPrimitiveType" + - name: doublePrimitiveType + in: query + required: true + schema: + $ref: "#/components/schemas/DoublePrimitiveType" + - name: charPrimitiveType + in: query + required: true + schema: + $ref: "#/components/schemas/CharPrimitiveType" + - name: charPrimitiveType2 + in: query + required: true + schema: + $ref: "#/components/schemas/CharPrimitiveType2" + + - name: bytePrimitiveTypes + in: query + required: true + schema: + type: array + items: + type: byte + - name: shortPrimitiveTypes + in: query + required: true + schema: + type: array + items: + type: short + - name: intPrimitiveTypes + in: query + required: true + schema: + type: array + items: + type: int + - name: longPrimitiveTypes + in: query + required: true + schema: + type: array + items: + type: long + - name: floatPrimitiveTypes + in: query + required: true + schema: + type: array + items: + type: float + - name: doublePrimitiveTypes + in: query + required: true + schema: + type: array + items: + type: double + - name: charPrimitiveTypes + in: query + required: true + schema: + type: array + items: + type: char + - name: byteTypes + in: query + required: true + schema: + type: array + items: + type: integer + format: byte + - name: byteTypes2 + in: query + required: true + schema: + type: array + items: + type: integer + format: int8 + - name: shortTypes + in: query + required: true + schema: + type: array + items: + type: integer + format: int16 + - name: shortTypes2 + in: query + required: true + schema: + type: array + items: + type: integer + format: short + - name: intTypes + in: query + required: true + schema: + type: array + items: + type: integer + format: int32 + - name: longTypes + in: query + required: true + schema: + type: array + items: + type: integer + format: int64 + + responses: + 200: + description: Success + + /send: + post: + tags: [ parameters ] + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/Obj" + responses: + 200: + description: Success + content: + application/json: + schema: + $ref: "#/components/schemas/Obj" +components: + schemas: + StringType: + type: string + ByteType: + type: integer + format: byte + ByteType2: + type: integer + format: int8 + ByteType3: + type: integer + x-format: byte + ByteType4: + type: integer + x-format: int8 + ShortType: + type: integer + format: int16 + ShortType2: + type: integer + format: short + ShortType3: + type: integer + x-format: int16 + ShortType4: + type: integer + x-format: short + IntType: + type: integer + format: int32 + LongType: + type: integer + format: int64 + BooleanType: + type: boolean + FloatType: + type: number + format: float + DoubleType: + type: number + format: double + DecimalType: + type: number + BytePrimitiveType: + type: byte + ShortPrimitiveType: + type: short + IntPrimitiveType: + type: int + LongPrimitiveType: + type: long + FloatPrimitiveType: + type: float + DoublePrimitiveType: + type: double + CharPrimitiveType: + type: char + CharPrimitiveType2: + type: string + x-type: char + BytePrimitiveType2: + type: integer + x-type: byte + ShortPrimitiveType2: + type: integer + x-type: short + Obj: + type: object + properties: + name: + type: string + byteType: + type: integer + format: byte + byteType2: + type: integer + format: int8 + shortType: + type: integer + format: int16 + shortType2: + type: integer + format: short + intType: + type: integer + format: int32 + longType: + type: integer + format: int64 + boolType: + type: boolean + decimalType: + type: number + floatType: + type: number + format: float + doubleType: + type: number + format: double + bytePrimitiveType: + type: byte + shortPrimitiveType: + type: short + intPrimitiveType: + type: int + longPrimitiveType: + type: long + floatPrimitiveType: + type: float + doublePrimitiveType: + type: double + charPrimitiveType: + type: char + charPrimitiveType2: + type: string + x-type: char + bytePrimitiveType2: + type: integer + x-type: byte + shortPrimitiveType2: + type: integer + x-type: short + bytePrimitiveTypes: + type: array + items: + type: byte + shortPrimitiveTypes: + type: array + items: + type: short + intPrimitiveTypes: + type: array + items: + type: int + longPrimitiveTypes: + type: array + items: + type: long + floatPrimitiveTypes: + type: array + items: + type: float + doublePrimitiveTypes: + type: array + items: + type: double + charPrimitiveTypes: + type: array + items: + type: char + charPrimitiveTypes2: + type: array + items: + type: string + x-type: char + byteTypes: + type: array + items: + type: integer + format: byte + byteTypes2: + type: array + items: + type: integer + format: int8 + byteTypes3: + type: array + items: + type: integer + x-type: byte + shortTypes: + type: array + items: + type: integer + format: int16 + shortTypes2: + type: array + items: + type: integer + format: short + shortTypes3: + type: array + items: + type: integer + x-type: short + intTypes: + type: array + items: + type: integer + format: int32 + longTypes: + type: array + items: + type: integer + format: int64 diff --git a/openapi-generator/src/test/resources/3_0/openmeteo.yml b/openapi-generator/src/test/resources/3_0/openmeteo.yml index e8901de00a..812d4d17ac 100644 --- a/openapi-generator/src/test/resources/3_0/openmeteo.yml +++ b/openapi-generator/src/test/resources/3_0/openmeteo.yml @@ -129,7 +129,7 @@ paths: generationtime_ms: type: number example: 2.2119 - description: Generation time of the weather forecast in milli seconds. This is mainly used for performance monitoring and improvements. + description: Generation time of the weather forecast in milliseconds. This is mainly used for performance monitoring and improvements. utc_offset_seconds: type: integer example: 3600 @@ -144,7 +144,7 @@ paths: description: For each selected weather variable, the unit will be listed here. daily: type: DailyResponse - description: For each selected daily weather variable, data will be returned as a floating point array. Additionally a `time` array will be returned with ISO8601 timestamps. + description: For each selected daily weather variable, data will be returned as a floating point array. Additionally, a `time` array will be returned with ISO8601 timestamps. daily_units: type: object additionalProperties: