Compare commits

...

5 Commits

Author SHA1 Message Date
Tino Fuhrmann
4f788e7ae7 Extract enum type fix to abstract client codegen 2023-04-29 08:44:23 +02:00
Tino Fuhrmann
6f1d1c0084 Remove space from "type" 2023-04-20 20:41:19 +02:00
Tino Fuhrmann
9546218bb8 Regenerated samples 2023-04-20 17:37:42 +02:00
Tino Fuhrmann
cd79f5eff9 Remove printlns 2023-04-20 17:36:10 +02:00
Tino Fuhrmann
7527911393 Fix invalid discrimnator value and enum type 2023-04-20 17:32:57 +02:00
58 changed files with 130 additions and 16 deletions

View File

@ -610,6 +610,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
} else if (ModelUtils.isBinarySchema(p)) {
return "ArrayBuffer";
}
return super.getTypeDeclaration(p);
}
@ -885,7 +886,8 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
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) {
@ -962,6 +964,40 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
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);

View File

@ -303,13 +303,15 @@ public class TypeScriptClientCodegen extends AbstractTypeScriptClientCodegen imp
// 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 +424,8 @@ public class TypeScriptClientCodegen extends AbstractTypeScriptClientCodegen imp
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";

View File

@ -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 {

View File

@ -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;
@ -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}}
@ -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}}
}
}

View File

@ -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 {

View File

@ -37,6 +37,7 @@ export class Response {
}
public constructor() {
}
}

View File

@ -47,6 +47,7 @@ export class ApiResponse {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Category {
}
public constructor() {
}
}

View File

@ -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 {

View File

@ -71,6 +71,7 @@ export class Order {
}
public constructor() {
}
}

View File

@ -73,6 +73,7 @@ export class Pet {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Tag {
}
public constructor() {
}
}

View File

@ -85,6 +85,7 @@ export class User {
}
public constructor() {
}
}

View File

@ -37,6 +37,7 @@ export class Cat {
}
public constructor() {
}
}

View File

@ -37,6 +37,7 @@ export class CatAllOf {
}
public constructor() {
}
}

View File

@ -37,6 +37,7 @@ export class Dog {
}
public constructor() {
}
}

View File

@ -37,6 +37,7 @@ export class DogAllOf {
}
public constructor() {
}
}

View File

@ -30,6 +30,7 @@ export class FilePostRequest {
}
public constructor() {
}
}

View File

@ -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 {

View File

@ -37,6 +37,7 @@ export class PetByAge {
}
public constructor() {
}
}

View File

@ -37,6 +37,7 @@ export class PetByType {
}
public constructor() {
}
}

View File

@ -53,6 +53,7 @@ export class PetsFilteredPatchRequest {
}
public constructor() {
}
}

View File

@ -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}> = [
{
@ -53,6 +55,7 @@ export class PetsPatchRequest {
}
public constructor() {
this.petType = "PetsPatchRequest";
}
}

View File

@ -47,6 +47,7 @@ export class ApiResponse {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Category {
}
public constructor() {
}
}

View File

@ -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 {

View File

@ -71,6 +71,7 @@ export class Order {
}
public constructor() {
}
}

View File

@ -73,6 +73,7 @@ export class Pet {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Tag {
}
public constructor() {
}
}

View File

@ -85,6 +85,7 @@ export class User {
}
public constructor() {
}
}

View File

@ -47,6 +47,7 @@ export class ApiResponse {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Category {
}
public constructor() {
}
}

View File

@ -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 {

View File

@ -71,6 +71,7 @@ export class Order {
}
public constructor() {
}
}

View File

@ -73,6 +73,7 @@ export class Pet {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Tag {
}
public constructor() {
}
}

View File

@ -85,6 +85,7 @@ export class User {
}
public constructor() {
}
}

View File

@ -47,6 +47,7 @@ export class ApiResponse {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Category {
}
public constructor() {
}
}

View File

@ -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 {

View File

@ -71,6 +71,7 @@ export class Order {
}
public constructor() {
}
}

View File

@ -73,6 +73,7 @@ export class Pet {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Tag {
}
public constructor() {
}
}

View File

@ -85,6 +85,7 @@ export class User {
}
public constructor() {
}
}

View File

@ -47,6 +47,7 @@ export class ApiResponse {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Category {
}
public constructor() {
}
}

View File

@ -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 {

View File

@ -71,6 +71,7 @@ export class Order {
}
public constructor() {
}
}

View File

@ -73,6 +73,7 @@ export class Pet {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Tag {
}
public constructor() {
}
}

View File

@ -85,6 +85,7 @@ export class User {
}
public constructor() {
}
}

View File

@ -47,6 +47,7 @@ export class ApiResponse {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Category {
}
public constructor() {
}
}

View File

@ -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 {

View File

@ -71,6 +71,7 @@ export class Order {
}
public constructor() {
}
}

View File

@ -73,6 +73,7 @@ export class Pet {
}
public constructor() {
}
}

View File

@ -40,6 +40,7 @@ export class Tag {
}
public constructor() {
}
}

View File

@ -85,6 +85,7 @@ export class User {
}
public constructor() {
}
}