Skip to content

Commit

Permalink
[typescript-angular] fix: when model property name is sanitized, use …
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
wing328 and davidediak authored Sep 2, 2024
1 parent b228133 commit c733bb6
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
private boolean hasDiscriminatorWithNonEmptyMapping;
private CodegenComposedSchemas composedSchemas = null;
private boolean hasMultipleTypes = false;
/** true if the property's baseName != name, e.g. baseName = '_prop.value', name = 'propValue' after sanitization */
private boolean hasSanitizedName = false;
private Map<String, CodegenProperty> requiredVarsMap;
private String ref;
private boolean schemaIsFromAdditionalProperties;
Expand Down Expand Up @@ -843,6 +845,12 @@ public void setHasMultipleTypes(boolean hasMultipleTypes) {
this.hasMultipleTypes = hasMultipleTypes;
}

public boolean getHasSanitizedName() {
return hasSanitizedName;
}

public void setHasSanitizedName(boolean hasSanitizedName) { this.hasSanitizedName = hasSanitizedName; }

@Override
public boolean getIsUuid() {
return isUuid;
Expand Down Expand Up @@ -1061,6 +1069,7 @@ public String toString() {
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
sb.append(", composedSchemas=").append(composedSchemas);
sb.append(", hasMultipleTypes=").append(hasMultipleTypes);
sb.append(", hasSanitizedName=").append(hasSanitizedName);
sb.append(", requiredVarsMap=").append(requiredVarsMap);
sb.append(", ref=").append(ref);
sb.append(", schemaIsFromAdditionalProperties=").append(schemaIsFromAdditionalProperties);
Expand Down Expand Up @@ -1129,6 +1138,7 @@ public boolean equals(Object o) {
isNull == that.isNull &&
isVoid == that.isVoid &&
hasMultipleTypes == that.getHasMultipleTypes() &&
hasSanitizedName == that.getHasSanitizedName() &&
hasDiscriminatorWithNonEmptyMapping == that.hasDiscriminatorWithNonEmptyMapping &&
isBooleanSchemaTrue == that.getIsBooleanSchemaTrue() &&
isBooleanSchemaFalse == that.getIsBooleanSchemaFalse() &&
Expand Down Expand Up @@ -1207,7 +1217,7 @@ public int hashCode() {
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInPascalCase, nameInCamelCase,
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
xmlNamespace, isXmlWrapped, isNull, isVoid, additionalPropertiesIsAnyType, hasVars, hasRequired,
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, requiredVarsMap,
hasDiscriminatorWithNonEmptyMapping, composedSchemas, hasMultipleTypes, hasSanitizedName, requiredVarsMap,
ref, uniqueItemsBoolean, schemaIsFromAdditionalProperties, isBooleanSchemaTrue, isBooleanSchemaFalse,
format, dependentRequired, contains);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo

property.name = toVarName(name);
property.baseName = name;
property.setHasSanitizedName(!property.baseName.equals(property.name));
if (ModelUtils.getType(p) == null) {
property.openApiType = getSchemaType(p);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ private String getNameUsingParamNaming(String name) {
private String getNameUsingModelPropertyNaming(String name) {
switch (getModelPropertyNaming()) {
case original:
additionalProperties.put("modelPropertyNamingOriginal", true);
return name;
case camelCase:
return camelize(name, LOWERCASE_FIRST_LETTER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export interface {{classname}}{{#allParents}}{{#-first}} extends {{/-first}}{{{.
/** @deprecated */
{{/deprecated}}
{{/description}}
{{^modelPropertyNamingOriginal}}
{{#isReadOnly}}readonly {{/isReadOnly}}{{{name}}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}};
{{/modelPropertyNamingOriginal}}
{{#modelPropertyNamingOriginal}}
{{#isReadOnly}}readonly {{/isReadOnly}}{{#hasSanitizedName}}'{{{baseName}}}'{{/hasSanitizedName}}{{^hasSanitizedName}}{{{name}}}{{/hasSanitizedName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}};
{{/modelPropertyNamingOriginal}}
{{/vars}}
}{{>modelGenericEnums}}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ paths:

components:
schemas:
Dummy:
type: object
properties:
property.name:
type: string
DogBreed:
type: string
enum: [Dingo, Husky]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ model/dogBreed.ts
model/dogComposed.ts
model/dogInherited.ts
model/dogMapped.ts
model/dummy.ts
model/models.ts
model/petWithMappedDiscriminator.ts
model/petWithSimpleDiscriminator.ts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Schemas with different types of composition for testing models generation
*
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


export interface DummyModel {
'property.name'?: string;
}


Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './dogBreed';
export * from './dogComposed';
export * from './dogInherited';
export * from './dogMapped';
export * from './dummy';
export * from './petWithMappedDiscriminator';
export * from './petWithSimpleDiscriminator';
export * from './petWithoutDiscriminator';
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ model/dogBreed.ts
model/dogComposed.ts
model/dogInherited.ts
model/dogMapped.ts
model/dummy.ts
model/models.ts
model/petWithMappedDiscriminator.ts
model/petWithSimpleDiscriminator.ts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Schemas with different types of composition for testing models generation
*
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


export interface DummyModel {
'property.name'?: string;
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './dogBreed';
export * from './dogComposed';
export * from './dogInherited';
export * from './dogMapped';
export * from './dummy';
export * from './petWithMappedDiscriminator';
export * from './petWithSimpleDiscriminator';
export * from './petWithoutDiscriminator';
Loading

0 comments on commit c733bb6

Please sign in to comment.