Skip to content

[TypeScript-Consolidated] Fixes invalid type enum array & incorrect discriminator value if mapping is present #15272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ public String getTypeDeclaration(Schema p) {
} else if (ModelUtils.isBinarySchema(p)) {
return "ArrayBuffer";
}

return super.getTypeDeclaration(p);
}

Expand Down Expand Up @@ -885,7 +886,8 @@ public String toEnumVarName(String name, String datatype) {
public String toEnumName(CodegenProperty property) {
String enumName = property.name;
enumName = addSuffix(enumName, enumSuffix);
return toTypescriptTypeName(enumName, "_");
String tsName = toTypescriptTypeName(enumName, "_");
return tsName;
}

protected void setEnumPropertyNaming(String naming) {
Expand Down Expand Up @@ -962,6 +964,40 @@ public ModelsMap postProcessModels(ModelsMap objs) {
return objs;
}

/**
* Update datatypeWithEnum for array container
*
* @param property Codegen property
*/
protected void updateDataTypeWithEnumForArray(CodegenProperty property) {
CodegenProperty baseItem = property.items;
while (baseItem != null && (Boolean.TRUE.equals(baseItem.isMap)
|| Boolean.TRUE.equals(baseItem.isArray))) {
baseItem = baseItem.items;
}
if (baseItem != null) {
/*
* Note: There are cases where we have datatypeWithEnum == Array<{ [key: string]: string}
* In these cases, we then have Array <{ [key: EnumName]: EnumName}> - which is invalid typescript
* To protect agains this we first replace [key: string] with a special/reserved placeholder (i.e. *[key]* )
*/
property.datatypeWithEnum = property.datatypeWithEnum.replace("[key: string]", "*PLACEHOLDER*")
.replace(baseItem.baseType, toEnumName(baseItem))
.replace("*PLACEHOLDER*", "[key: string]");

// naming the enum with respect to the language enum naming convention
// e.g. remove [], {} from array/map of enum
property.enumName = toEnumName(property);

// set default value for variable with inner enum
if (property.defaultValue != null) {
property.defaultValue = property.defaultValue.replace(baseItem.baseType, toEnumName(baseItem));
}

updateCodegenPropertyEnum(property);
}
}

@Override
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
Map<String, ModelsMap> result = super.postProcessAllModels(objs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,15 @@ public ModelsMap postProcessModels(ModelsMap objs) {
// name enum with model name, e.g. StatusEnum => Pet.StatusEnum
for (CodegenProperty var : cm.vars) {
if (Boolean.TRUE.equals(var.isEnum)) {
String replaceName = var.isInnerEnum ? "Inner" + super.enumSuffix : var.enumName;
var.datatypeWithEnum = var.datatypeWithEnum.replace(replaceName, var.enumName);
var.datatypeWithEnum = var.datatypeWithEnum.replace(var.enumName, cm.classname + var.enumName);
}
}
if (cm.parent != null) {
for (CodegenProperty var : cm.allVars) {
if (Boolean.TRUE.equals(var.isEnum)) {
var.datatypeWithEnum = var.datatypeWithEnum
var.datatypeWithEnum = var.datatypeWithEnum
.replace(var.enumName, cm.classname + var.enumName);
}
}
Expand Down Expand Up @@ -422,8 +424,8 @@ public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
inner = ((ArraySchema) p).getItems();
return this.getSchemaType(p) + "<" + this.getTypeDeclaration(unaliasSchema(inner)) + ">";
} else if (ModelUtils.isMapSchema(p)) {
inner = getSchemaAdditionalProperties(p);
} else if (ModelUtils.isMapSchema(p)) { // it is an object schema
inner = getSchemaAdditionalProperties(p); // additional properties?
String postfix = "";
if (Boolean.TRUE.equals(inner.getNullable())) {
postfix = " | null";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export class ObjectSerializer {
return expectedType; // the type does not have a discriminator. use it.
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
var discriminatorValue = data[discriminatorProperty];
// if it has a mapping we need to map from discriminatorvalue to the actual type name
var discriminatorType = data.discriminatorTypeMap[discriminatorValue] || discriminatorValue;
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{

{{#discriminator}}
static readonly discriminator: string | undefined = "{{discriminatorName}}";
static readonly discriminatorTypeMap = {
{{#mappedModels}}
"{{mappingName}}": "{{modelName}}"{{^-last}},{{/-last}}
{{/mappedModels}}
}
{{/discriminator}}
{{^discriminator}}
static readonly discriminator: string | undefined = undefined;
Expand All @@ -35,7 +40,7 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
{
"name": "{{name}}",
"baseName": "{{baseName}}",
"type": "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}",
"type": "{{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}} | null{{/isNullable}}",
"format": "{{dataFormat}}"
}{{^-last}},
{{/-last}}
Expand All @@ -61,9 +66,14 @@ export class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{
this.{{name}} = "{{discriminatorValue}}";
{{/discriminatorValue}}
{{/allVars}}
{{#discriminatorName}}

{{#discriminator}}
{{^hasDiscriminatorWithNonEmptyMapping}}
{{#discriminatorName}}
this.{{discriminatorName}} = "{{classname}}";
{{/discriminatorName}}
{{/discriminatorName}}
{{/hasDiscriminatorWithNonEmptyMapping}}
{{/discriminator}}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export class ObjectSerializer {
return expectedType; // the type does not have a discriminator. use it.
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
var discriminatorValue = data[discriminatorProperty];
// if it has a mapping we need to map from discriminatorvalue to the actual type name
var discriminatorType = data.discriminatorTypeMap[discriminatorValue] || discriminatorValue;
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Response {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class ApiResponse {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Category {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export class ObjectSerializer {
return expectedType; // the type does not have a discriminator. use it.
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
var discriminatorValue = data[discriminatorProperty];
// if it has a mapping we need to map from discriminatorvalue to the actual type name
var discriminatorType = data.discriminatorTypeMap[discriminatorValue] || discriminatorValue;
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class Order {
}

public constructor() {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class Pet {
}

public constructor() {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Tag {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class User {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Cat {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class CatAllOf {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class Dog {
}

public constructor() {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class DogAllOf {
}

public constructor() {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class FilePostRequest {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export class ObjectSerializer {
return expectedType; // the type does not have a discriminator. use it.
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
var discriminatorValue = data[discriminatorProperty];
// if it has a mapping we need to map from discriminatorvalue to the actual type name
var discriminatorType = data.discriminatorTypeMap[discriminatorValue] || discriminatorValue;
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class PetByAge {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class PetByType {
}

public constructor() {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class PetsFilteredPatchRequest {
}

public constructor() {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class PetsPatchRequest {
'breed'?: PetsPatchRequestBreedEnum;

static readonly discriminator: string | undefined = "petType";
static readonly discriminatorTypeMap = {
}

static readonly attributeTypeMap: Array<{name: string, baseName: string, type: string, format: string}> = [
{
Expand Down Expand Up @@ -53,6 +55,7 @@ export class PetsPatchRequest {
}

public constructor() {

this.petType = "PetsPatchRequest";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class ApiResponse {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Category {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export class ObjectSerializer {
return expectedType; // the type does not have a discriminator. use it.
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
var discriminatorValue = data[discriminatorProperty];
// if it has a mapping we need to map from discriminatorvalue to the actual type name
var discriminatorType = data.discriminatorTypeMap[discriminatorValue] || discriminatorValue;
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class Order {
}

public constructor() {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class Pet {
}

public constructor() {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Tag {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class User {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class ApiResponse {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Category {
}

public constructor() {

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export class ObjectSerializer {
return expectedType; // the type does not have a discriminator. use it.
} else {
if (data[discriminatorProperty]) {
var discriminatorType = data[discriminatorProperty];
var discriminatorValue = data[discriminatorProperty];
// if it has a mapping we need to map from discriminatorvalue to the actual type name
var discriminatorType = data.discriminatorTypeMap[discriminatorValue] || discriminatorValue;
if(typeMap[discriminatorType]){
return discriminatorType; // use the type given in the discriminator
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class Order {
}

public constructor() {

}
}

Expand Down
Loading