-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[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
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,6 +293,40 @@ public String toEnumValue(String value, String datatype) { | |
} | ||
} | ||
|
||
/** | ||
* 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]", "*key*") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
can be constant with PLACEHOLDER name. #PLACEHOLDER# or something else. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually using regular expression with replaceAll might help avoiding placeholder. We always looking to replace last occurence There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it always the last occurrence? Wondering whether there are more complicated examples where this does not hold - mainly thinking about cases where the same type appears twice (e.g., In cases like this, our model is anyway broken because we lose type information when generating the initial type. |
||
.replace(baseItem.baseType, toEnumName(baseItem)) | ||
.replace("*key*", "[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 ModelsMap postProcessModels(ModelsMap objs) { | ||
// process enum in models | ||
|
@@ -303,13 +337,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); | ||
} | ||
} | ||
|
@@ -422,8 +458,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"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Extract to AbstractTSClientCodegen