Skip to content

Commit c733bb6

Browse files
wing328davidediak
andauthored
[typescript-angular] fix: when model property name is sanitized, use instead original property name within quotes in mustache template (#19508)
* fix: typescript-angular -> when model property name is sanitized, use instead original property name within quotes in mustache template * fix: updated samples * chore: added comment for hasSanitizedName * add original tag, add dummy model for test * update samples --------- Co-authored-by: Davide Diaconu <[email protected]>
1 parent b228133 commit c733bb6

File tree

12 files changed

+61
-4
lines changed

12 files changed

+61
-4
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
243243
private boolean hasDiscriminatorWithNonEmptyMapping;
244244
private CodegenComposedSchemas composedSchemas = null;
245245
private boolean hasMultipleTypes = false;
246+
/** true if the property's baseName != name, e.g. baseName = '_prop.value', name = 'propValue' after sanitization */
247+
private boolean hasSanitizedName = false;
246248
private Map<String, CodegenProperty> requiredVarsMap;
247249
private String ref;
248250
private boolean schemaIsFromAdditionalProperties;
@@ -843,6 +845,12 @@ public void setHasMultipleTypes(boolean hasMultipleTypes) {
843845
this.hasMultipleTypes = hasMultipleTypes;
844846
}
845847

848+
public boolean getHasSanitizedName() {
849+
return hasSanitizedName;
850+
}
851+
852+
public void setHasSanitizedName(boolean hasSanitizedName) { this.hasSanitizedName = hasSanitizedName; }
853+
846854
@Override
847855
public boolean getIsUuid() {
848856
return isUuid;
@@ -1061,6 +1069,7 @@ public String toString() {
10611069
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
10621070
sb.append(", composedSchemas=").append(composedSchemas);
10631071
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
1072+
sb.append(", hasSanitizedName=").append(hasSanitizedName);
10641073
sb.append(", requiredVarsMap=").append(requiredVarsMap);
10651074
sb.append(", ref=").append(ref);
10661075
sb.append(", schemaIsFromAdditionalProperties=").append(schemaIsFromAdditionalProperties);
@@ -1129,6 +1138,7 @@ public boolean equals(Object o) {
11291138
isNull == that.isNull &&
11301139
isVoid == that.isVoid &&
11311140
hasMultipleTypes == that.getHasMultipleTypes() &&
1141+
hasSanitizedName == that.getHasSanitizedName() &&
11321142
hasDiscriminatorWithNonEmptyMapping == that.hasDiscriminatorWithNonEmptyMapping &&
11331143
isBooleanSchemaTrue == that.getIsBooleanSchemaTrue() &&
11341144
isBooleanSchemaFalse == that.getIsBooleanSchemaFalse() &&
@@ -1207,7 +1217,7 @@ public int hashCode() {
12071217
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInPascalCase, nameInCamelCase,
12081218
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
12091219
xmlNamespace, isXmlWrapped, isNull, isVoid, additionalPropertiesIsAnyType, hasVars, hasRequired,
1210-
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, requiredVarsMap,
1220+
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, hasSanitizedName, requiredVarsMap,
12111221
ref, uniqueItemsBoolean, schemaIsFromAdditionalProperties, isBooleanSchemaTrue, isBooleanSchemaFalse,
12121222
format, dependentRequired, contains);
12131223
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,6 +3884,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
38843884

38853885
property.name = toVarName(name);
38863886
property.baseName = name;
3887+
property.setHasSanitizedName(!property.baseName.equals(property.name));
38873888
if (ModelUtils.getType(p) == null) {
38883889
property.openApiType = getSchemaType(p);
38893890
} else {

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ private String getNameUsingParamNaming(String name) {
856856
private String getNameUsingModelPropertyNaming(String name) {
857857
switch (getModelPropertyNaming()) {
858858
case original:
859+
additionalProperties.put("modelPropertyNamingOriginal", true);
859860
return name;
860861
case camelCase:
861862
return camelize(name, LOWERCASE_FIRST_LETTER);

modules/openapi-generator/src/main/resources/typescript-angular/modelGeneric.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export interface {{classname}}{{#allParents}}{{#-first}} extends {{/-first}}{{{.
1313
/** @deprecated */
1414
{{/deprecated}}
1515
{{/description}}
16+
{{^modelPropertyNamingOriginal}}
1617
{{#isReadOnly}}readonly {{/isReadOnly}}{{{name}}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}};
18+
{{/modelPropertyNamingOriginal}}
19+
{{#modelPropertyNamingOriginal}}
20+
{{#isReadOnly}}readonly {{/isReadOnly}}{{#hasSanitizedName}}'{{{baseName}}}'{{/hasSanitizedName}}{{^hasSanitizedName}}{{{name}}}{{/hasSanitizedName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}};
21+
{{/modelPropertyNamingOriginal}}
1722
{{/vars}}
1823
}{{>modelGenericEnums}}

modules/openapi-generator/src/test/resources/3_0/typescript-angular/composed-schemas.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ paths:
2626

2727
components:
2828
schemas:
29+
Dummy:
30+
type: object
31+
properties:
32+
property.name:
33+
type: string
2934
DogBreed:
3035
type: string
3136
enum: [Dingo, Husky]

samples/client/others/typescript-angular/builds/composed-schemas-tagged-unions/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ model/dogBreed.ts
1414
model/dogComposed.ts
1515
model/dogInherited.ts
1616
model/dogMapped.ts
17+
model/dummy.ts
1718
model/models.ts
1819
model/petWithMappedDiscriminator.ts
1920
model/petWithSimpleDiscriminator.ts
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Schemas with different types of composition for testing models generation
3+
*
4+
*
5+
*
6+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
7+
* https://openapi-generator.tech
8+
* Do not edit the class manually.
9+
*/
10+
11+
12+
export interface DummyModel {
13+
'property.name'?: string;
14+
}
15+
16+

samples/client/others/typescript-angular/builds/composed-schemas-tagged-unions/model/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './dogBreed';
55
export * from './dogComposed';
66
export * from './dogInherited';
77
export * from './dogMapped';
8+
export * from './dummy';
89
export * from './petWithMappedDiscriminator';
910
export * from './petWithSimpleDiscriminator';
1011
export * from './petWithoutDiscriminator';

samples/client/others/typescript-angular/builds/composed-schemas/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ model/dogBreed.ts
1414
model/dogComposed.ts
1515
model/dogInherited.ts
1616
model/dogMapped.ts
17+
model/dummy.ts
1718
model/models.ts
1819
model/petWithMappedDiscriminator.ts
1920
model/petWithSimpleDiscriminator.ts
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Schemas with different types of composition for testing models generation
3+
*
4+
*
5+
*
6+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
7+
* https://openapi-generator.tech
8+
* Do not edit the class manually.
9+
*/
10+
11+
12+
export interface DummyModel {
13+
'property.name'?: string;
14+
}
15+

samples/client/others/typescript-angular/builds/composed-schemas/model/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './dogBreed';
55
export * from './dogComposed';
66
export * from './dogInherited';
77
export * from './dogMapped';
8+
export * from './dummy';
89
export * from './petWithMappedDiscriminator';
910
export * from './petWithSimpleDiscriminator';
1011
export * from './petWithoutDiscriminator';

0 commit comments

Comments
 (0)