Skip to content

Commit ff57797

Browse files
committed
Added $ (dollar sign) escape for kotlin
Fixed #2379
1 parent 408983a commit ff57797

26 files changed

+989
-129
lines changed

openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
15821582
if (param.isEnumRef) {
15831583
param.isEnum = true;
15841584
}
1585-
processGenericAnnotations(param, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false);
1585+
processGenericAnnotations(param, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false, false);
15861586
if (useBeanValidation && !param.isContainer && param.isModel) {
15871587
param.vendorExtensions.put("withValid", true);
15881588
}
@@ -1609,7 +1609,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
16091609
}
16101610
}
16111611
if (op.returnProperty != null) {
1612-
processGenericAnnotations(op.returnProperty, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false);
1612+
processGenericAnnotations(op.returnProperty, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false, false);
16131613
op.returnType = op.returnProperty.vendorExtensions.get("typeWithEnumWithGenericAnnotations").toString();
16141614
}
16151615
}
@@ -2370,7 +2370,7 @@ private void processDiscriminatorProperty(CodegenProperty prop, CodegenDiscrimin
23702370
prop.dataType = discriminator.getPropertyType();
23712371
prop.datatypeWithEnum = discriminator.getPropertyType();
23722372
prop.baseType = discriminator.getPropertyType();
2373-
processGenericAnnotations(prop, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false);
2373+
processGenericAnnotations(prop, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false, false);
23742374
}
23752375

23762376
private void processProperty(CodegenProperty property, boolean isServer, CodegenModel model, Map<String, ModelsMap> models) {
@@ -2398,7 +2398,7 @@ private void processProperty(CodegenProperty property, boolean isServer, Codegen
23982398
property.maxLength = null;
23992399
}
24002400

2401-
processGenericAnnotations(property, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false);
2401+
processGenericAnnotations(property, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false, false);
24022402

24032403
normalizeExtraAnnotations(EXT_ANNOTATIONS_FIELD, false, property.vendorExtensions);
24042404
normalizeExtraAnnotations(EXT_ANNOTATIONS_SETTER, false, property.vendorExtensions);
@@ -2432,7 +2432,7 @@ private void processParentModel(CodegenModel model, List<CodegenProperty> requir
24322432
var parentVars = new ArrayList<CodegenProperty>();
24332433
for (var v : parent.allVars) {
24342434
if (notContainsProp(v, model.vars)) {
2435-
processGenericAnnotations(v, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false);
2435+
processGenericAnnotations(v, useBeanValidation, isGenerateHardNullable(), false, false, false, false, false, false);
24362436
parentVars.add(v);
24372437
}
24382438
}

openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautKotlinCodegen.java

Lines changed: 123 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
import static io.micronaut.openapi.generator.Utils.calcQueryValueFormat;
113113
import static io.micronaut.openapi.generator.Utils.isDateType;
114114
import static io.micronaut.openapi.generator.Utils.normalizeExtraAnnotations;
115+
import static io.micronaut.openapi.generator.Utils.normalizeStr;
116+
import static io.micronaut.openapi.generator.Utils.normalizeStrValue;
115117
import static io.micronaut.openapi.generator.Utils.processDuplicateVars;
116118
import static io.micronaut.openapi.generator.Utils.processGenericAnnotations;
117119
import static io.micronaut.openapi.generator.Utils.readListOfStringsProperty;
@@ -995,6 +997,7 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
995997
List<CodegenOperation> opList = operations.computeIfAbsent(basePath, k -> new ArrayList<>());
996998
opList.add(co);
997999
co.baseName = basePath;
1000+
co.vendorExtensions.put("normalizedBaseName", normalizeStr(basePath));
9981001
return;
9991002
}
10001003

@@ -1004,6 +1007,12 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
10041007
}
10051008

10061009
super.addOperationToGroup(superSanitizeTag(tag), resourcePath, operation, co, operations);
1010+
1011+
var foundTag = co.tags.stream().filter(t -> t.getName().equals(tag)).findFirst().orElse(null);
1012+
co.vendorExtensions.put("normalizedBaseTag", normalizeStr(tag));
1013+
if (foundTag != null) {
1014+
co.vendorExtensions.put("normalizedTagDesc", normalizeStr(foundTag.getDescription()));
1015+
}
10071016
}
10081017

10091018
@Override
@@ -1121,6 +1130,9 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
11211130

11221131
for (CodegenOperation op : operationList) {
11231132

1133+
objs.put("normalizedBaseTag", op.vendorExtensions.get("normalizedBaseTag"));
1134+
objs.put("normalizedTagDesc", op.vendorExtensions.get("normalizedTagDesc"));
1135+
11241136
handleImplicitHeaders(op);
11251137
handleConstantParams(op);
11261138

@@ -1199,6 +1211,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
11991211
param.isBodyParam = true;
12001212
param.isFormParam = false;
12011213
param.vendorExtensions.put("isPart", true);
1214+
param.vendorExtensions.put("baseNameNormalized", normalizeStr(param.baseName));
12021215
if (param.isEnumRef) {
12031216
param.isEnum = true;
12041217
}
@@ -1211,6 +1224,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
12111224
p.isBodyParam = true;
12121225
p.isFormParam = false;
12131226
p.vendorExtensions.put("isPart", true);
1227+
p.vendorExtensions.put("baseNameNormalized", normalizeStr(p.baseName));
12141228
if (p.isEnumRef) {
12151229
p.isEnum = true;
12161230
}
@@ -1226,7 +1240,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
12261240
param.isEnum = true;
12271241
}
12281242
processGenericAnnotations(param, useBeanValidation, false, param.isNullable || !param.required,
1229-
param.required, false, true, ksp);
1243+
param.required, false, true, true, ksp);
12301244
param.vendorExtensions.put("isString", "string".equalsIgnoreCase(param.dataType));
12311245
param.vendorExtensions.put("withoutExample", param.example == null || param.example.equals(NULL_STRING));
12321246
if (useBeanValidation && ((!param.isContainer && param.isModel)
@@ -1237,7 +1251,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
12371251
// check pattern property for date types: if set, need use this pattern as `@Format` annotation value
12381252
if (isDateType(param.dataType)) {
12391253
if (StringUtils.isNotEmpty(param.pattern)) {
1240-
param.vendorExtensions.put("formatPattern", param.pattern);
1254+
param.vendorExtensions.put("formatPattern", normalizeStr(param.pattern));
12411255
param.pattern = null;
12421256
}
12431257
param.minItems = null;
@@ -1262,9 +1276,11 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
12621276
additionalProperties.put("enumParams", enumParams);
12631277

12641278
if (op.returnProperty != null) {
1265-
processGenericAnnotations(op.returnProperty, useBeanValidation, false, false, false, false, false, ksp);
1279+
processGenericAnnotations(op.returnProperty, useBeanValidation, false, false, false, false, false, true, ksp);
12661280
op.returnType = op.returnProperty.vendorExtensions.get("typeWithEnumWithGenericAnnotations").toString();
12671281
}
1282+
1283+
normalizeTextsInOperation(op);
12681284
}
12691285

12701286
if (needToAddImportFormat) {
@@ -1274,6 +1290,80 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
12741290
return objs;
12751291
}
12761292

1293+
private void normalizeTextsInOperation(CodegenOperation op) {
1294+
1295+
if (op.vendorExtensions.containsKey("x-deprecated-message")) {
1296+
op.vendorExtensions.put("x-deprecated-message-normalized", normalizeStr(op.vendorExtensions.get("x-deprecated-message").toString()));
1297+
}
1298+
1299+
if (op.vendorExtensions.containsKey("x-roles")) {
1300+
var roles = (List<String>) op.vendorExtensions.get("x-roles");
1301+
var normalizedRoles = new ArrayList<String>(roles.size());
1302+
for (var role : roles) {
1303+
normalizedRoles.add(normalizeStr(role));
1304+
}
1305+
op.vendorExtensions.put("x-roles-normalized", normalizedRoles);
1306+
}
1307+
1308+
if (op.summary != null) {
1309+
op.vendorExtensions.put("summaryNormalized", normalizeStr(op.summary));
1310+
}
1311+
if (op.notes != null) {
1312+
op.vendorExtensions.put("notesNormalized", normalizeStr(op.notes));
1313+
}
1314+
if (op.tags != null) {
1315+
for (var tag : op.tags) {
1316+
tag.setName(normalizeStr(tag.getName()));
1317+
}
1318+
}
1319+
if (op.responses != null) {
1320+
for (var response : op.responses) {
1321+
response.vendorExtensions.put("messageNormalized", normalizeStr(response.message));
1322+
}
1323+
}
1324+
if (op.produces != null) {
1325+
for (var produce : op.produces) {
1326+
produce.put("mediaTypeNormalized", normalizeStr(produce.get("mediaType")));
1327+
}
1328+
}
1329+
if (op.consumes != null) {
1330+
for (var consume : op.consumes) {
1331+
consume.put("mediaTypeNormalized", normalizeStr(consume.get("mediaType")));
1332+
}
1333+
}
1334+
if (op.vendorExtensions.containsKey("swaggerParams")) {
1335+
for (var swaggerParam : (List<CodegenParameter>) op.vendorExtensions.get("swaggerParams")) {
1336+
swaggerParam.vendorExtensions.put("baseNameNormalized", normalizeStr(swaggerParam.baseName));
1337+
swaggerParam.vendorExtensions.put("descriptionNormalized", normalizeStr(swaggerParam.description));
1338+
}
1339+
}
1340+
if (op.authMethods != null) {
1341+
for (var authMethod : op.authMethods) {
1342+
authMethod.vendorExtensions.put("nameNormalized", normalizeStr(authMethod.name));
1343+
if (authMethod.scopes != null) {
1344+
for (var scope : authMethod.scopes) {
1345+
scope.put("scopeNormalized", normalizeStr((String) scope.get("scope")));
1346+
}
1347+
}
1348+
}
1349+
}
1350+
op.vendorExtensions.put("pathNormalized", normalizeStr(op.path));
1351+
1352+
if (op.allParams != null) {
1353+
for (var param : op.allParams) {
1354+
param.vendorExtensions.put("baseNameNormalized", normalizeStr(param.baseName));
1355+
if (param.defaultValue != null) {
1356+
param.vendorExtensions.put("defaultValueNormalized", normalizeStr(param.defaultValue));
1357+
}
1358+
normalizeStrValue("x-pattern-message", param.vendorExtensions);
1359+
normalizeStrValue("x-size-message", param.vendorExtensions);
1360+
normalizeStrValue("x-not-null-message", param.vendorExtensions);
1361+
normalizeStrValue("x-minimum-message", param.vendorExtensions);
1362+
normalizeStrValue("x-maximum-message", param.vendorExtensions);
1363+
}
1364+
}
1365+
}
1366+
12771367
/**
12781368
* This method removes all implicit header parameters from the list of parameters.
12791369
*
@@ -1727,7 +1817,7 @@ public CodegenParameter fromParameter(Parameter p, Set<String> imports) {
17271817
if (realName.contains("`")) {
17281818
realName = realName.replace("`", "");
17291819
}
1730-
parameter.vendorExtensions.put("realName", realName);
1820+
parameter.vendorExtensions.put("realName", normalizeStr(realName));
17311821

17321822
Schema parameterSchema;
17331823
if (p.getSchema() != null) {
@@ -1766,7 +1856,7 @@ public CodegenParameter fromParameter(Parameter p, Set<String> imports) {
17661856
if (ksp && !defaultValueInit.equals(NULL_STRING)) {
17671857
parameter.isNullable = false;
17681858
}
1769-
parameter.vendorExtensions.put("defaultValueInit", defaultValueInit);
1859+
parameter.vendorExtensions.put("defaultValueInit", normalizeStr(defaultValueInit));
17701860
parameter.vendorExtensions.put("defaultValueIsNotNull", !defaultValueInit.equals(NULL_STRING));
17711861
}
17721862

@@ -1806,7 +1896,7 @@ public CodegenProperty fromProperty(String name, Schema schema, boolean required
18061896
property.nameInCamelCase = camelize(realName, LOWERCASE_FIRST_LETTER);
18071897
property.nameInSnakeCase = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, property.nameInCamelCase);
18081898
}
1809-
property.vendorExtensions.put("realName", realName);
1899+
property.vendorExtensions.put("realName", normalizeStr(realName));
18101900

18111901
if (schema != null && schema.get$ref() != null) {
18121902
var refSchema = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, schema.get$ref());
@@ -1838,7 +1928,7 @@ public CodegenProperty fromProperty(String name, Schema schema, boolean required
18381928
defaultValueInit = NULL_STRING;
18391929
}
18401930
if (defaultValueInit != null) {
1841-
property.vendorExtensions.put("defaultValueInit", defaultValueInit);
1931+
property.vendorExtensions.put("defaultValueInit", normalizeStr(defaultValueInit));
18421932
}
18431933

18441934
return property;
@@ -2173,6 +2263,10 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
21732263
model.vendorExtensions.put("requiredParentVarsWithoutDiscriminator", requiredParentVarsWithoutDiscriminator);
21742264
}
21752265

2266+
if (model.vendorExtensions.containsKey("x-deprecated-message")) {
2267+
model.vendorExtensions.put("x-deprecated-message-normalized", normalizeStr((String) model.vendorExtensions.get("x-deprecated-message")));
2268+
}
2269+
model.vendorExtensions.put("descriptionNormalized", normalizeStr(model.description));
21762270
model.vendorExtensions.put("requiredVarsWithoutDiscriminator", requiredVarsWithoutDiscriminator);
21772271
model.vendorExtensions.put("requiredVars", requiredVars);
21782272
model.vendorExtensions.put("withRequiredOrOptionalVars", !requiredVarsWithoutDiscriminator.isEmpty() || !optionalVars.isEmpty());
@@ -2340,10 +2434,11 @@ protected void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars,
23402434

23412435
var baseType = (String) vendorExtensions.get("baseType");
23422436
for (var enumVar : enumVars) {
2437+
var value = (String) enumVar.get("value");
23432438
if ((boolean) enumVar.get("isString")) {
2439+
enumVar.put("valueNormalized", normalizeStr(value));
23442440
continue;
23452441
}
2346-
var value = (String) enumVar.get("value");
23472442
value = value.replace("\"", "");
23482443
if ("char".equals(baseType) && !value.startsWith("'")) {
23492444
enumVar.put("value", "'" + value + "'");
@@ -2352,6 +2447,7 @@ protected void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars,
23522447
} else if ("byte".equalsIgnoreCase(baseType)) {
23532448
enumVar.put("value", value);
23542449
}
2450+
enumVar.put("valueNormalized", enumVar.get("value"));
23552451
}
23562452
}
23572453

@@ -2459,6 +2555,22 @@ private void processOneOfModels(CodegenModel model, Collection<ModelsMap> models
24592555

24602556
private void processProperty(CodegenProperty property, boolean isServer, CodegenModel model, Map<String, ModelsMap> models) {
24612557

2558+
property.vendorExtensions.put("baseNameNormalized", normalizeStr(property.baseName));
2559+
if (property.example != null) {
2560+
property.vendorExtensions.put("exampleNormalized", normalizeStr(property.example));
2561+
}
2562+
if (property.description != null) {
2563+
property.vendorExtensions.put("descriptionNormalized", normalizeStr(property.description));
2564+
}
2565+
if (property.vendorExtensions.containsKey("x-deprecated-message")) {
2566+
property.vendorExtensions.put("x-deprecated-message-normalized", normalizeStr((String) property.vendorExtensions.get("x-deprecated-message")));
2567+
}
2568+
normalizeStrValue("x-pattern-message", property.vendorExtensions);
2569+
normalizeStrValue("x-size-message", property.vendorExtensions);
2570+
normalizeStrValue("x-not-null-message", property.vendorExtensions);
2571+
normalizeStrValue("x-minimum-message", property.vendorExtensions);
2572+
normalizeStrValue("x-maximum-message", property.vendorExtensions);
2573+
24622574
property.vendorExtensions.put("withRequiredAndOptionalVars", model.vendorExtensions.get("withRequiredAndOptionalVars"));
24632575
property.vendorExtensions.put("inRequiredArgsConstructor", !property.isReadOnly || isServer);
24642576
property.vendorExtensions.put("isServer", isServer);
@@ -2469,6 +2581,7 @@ private void processProperty(CodegenProperty property, boolean isServer, Codegen
24692581
property.vendorExtensions.put("x-implements", model.vendorExtensions.get("x-implements"));
24702582
if (NULL_STRING.equals(property.example)) {
24712583
property.example = null;
2584+
property.vendorExtensions.remove("exampleNormalized");
24722585
}
24732586
if (useBeanValidation && (
24742587
(!property.isContainer && property.isModel)
@@ -2480,7 +2593,7 @@ private void processProperty(CodegenProperty property, boolean isServer, Codegen
24802593
// check pattern property for date types: if set, need use this pattern as `@Format` annotation value
24812594
if (isDateType(property.dataType)) {
24822595
if (StringUtils.isNotEmpty(property.pattern)) {
2483-
property.vendorExtensions.put("formatPattern", property.pattern);
2596+
property.vendorExtensions.put("formatPattern", normalizeStr(property.pattern));
24842597
property.pattern = null;
24852598
}
24862599
property.minItems = null;
@@ -2494,7 +2607,7 @@ private void processProperty(CodegenProperty property, boolean isServer, Codegen
24942607
}
24952608

24962609
processGenericAnnotations(property, useBeanValidation, false, property.isNullable || property.isDiscriminator,
2497-
property.required, property.isReadOnly, true, ksp);
2610+
property.required, property.isReadOnly, true, true, ksp);
24982611

24992612
normalizeExtraAnnotations(EXT_ANNOTATIONS_FIELD, true, property.vendorExtensions);
25002613
normalizeExtraAnnotations(EXT_ANNOTATIONS_SETTER, true, property.vendorExtensions);

0 commit comments

Comments
 (0)