Skip to content

Commit

Permalink
Added support deprecated extension for enum values.
Browse files Browse the repository at this point in the history
Added support primitive types in enum values
Fixes for numeric type generating.
  • Loading branch information
altro3 committed Sep 22, 2024
1 parent c0e4562 commit 02c94a6
Show file tree
Hide file tree
Showing 9 changed files with 729 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1288,6 +1340,57 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
@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 = "Character";
} 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 = "Integer";
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);
Expand Down Expand Up @@ -1421,22 +1524,23 @@ private void processParametersWithAdditionalMappings(List<CodegenParameter> 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);
}
}

Expand Down Expand Up @@ -1668,6 +1772,72 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
return objs;
}

@Override
protected void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions, String dataType) {
super.updateEnumVarsWithExtensions(enumVars, vendorExtensions, dataType);
if (vendorExtensions == null) {
return;
}

var xDeprecated = (List<Object>) vendorExtensions.get("x-deprecated");
if (xDeprecated != null && !xDeprecated.isEmpty()) {
for (var deprecatedItem : xDeprecated) {
Map<String, Object> 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<ModelsMap> models) {

if (!model.vendorExtensions.containsKey("x-is-one-of-interface")
Expand Down
Loading

0 comments on commit 02c94a6

Please sign in to comment.