diff --git a/src/metadataGeneration/tsoa.ts b/src/metadataGeneration/tsoa.ts index 8313cd664..e3003e809 100644 --- a/src/metadataGeneration/tsoa.ts +++ b/src/metadataGeneration/tsoa.ts @@ -124,9 +124,6 @@ export namespace Tsoa { description?: string; dataType: RefTypeLiteral; refName: string; - properties?: Property[]; - additionalProperties?: Type; - enums?: string[]; example?: any; } @@ -134,10 +131,13 @@ export namespace Tsoa { export interface ReferenceObject extends ReferenceTypeBase { dataType: 'refObject'; + properties: Property[]; + additionalProperties?: Type; } export interface ReferenceEnum extends ReferenceTypeBase { dataType: 'refEnum'; + enums: string[]; } export interface ReferenceTypeAlias extends Omit, ReferenceTypeBase { diff --git a/src/metadataGeneration/typeResolver.ts b/src/metadataGeneration/typeResolver.ts index 8823514e8..a9f09821d 100644 --- a/src/metadataGeneration/typeResolver.ts +++ b/src/metadataGeneration/typeResolver.ts @@ -393,14 +393,14 @@ export class TypeResolver { const inheritedProperties = this.getModelInheritedProperties(modelType) || []; const example = this.getNodeExample(modelType); - const referenceType = { + const referenceType: Tsoa.ReferenceObject = { additionalProperties, dataType: 'refObject', description: this.getNodeDescription(modelType), properties: inheritedProperties, refName: this.getRefTypeName(name), ...(example && { example }), - } as Tsoa.ReferenceType; + }; referenceType.properties = (referenceType.properties as Tsoa.Property[]).concat(properties); @@ -430,7 +430,7 @@ export class TypeResolver { } private createCircularDependencyResolver(refName: string) { - const referenceType = { + let referenceType = { dataType: 'refObject', refName, } as Tsoa.ReferenceType; @@ -440,10 +440,10 @@ export class TypeResolver { if (!realReferenceType) { return; } - referenceType.description = realReferenceType.description; - referenceType.properties = realReferenceType.properties; - referenceType.dataType = realReferenceType.dataType; - referenceType.refName = referenceType.refName; + referenceType = { + ...referenceType, + ...realReferenceType, + }; }); return referenceType; @@ -711,7 +711,7 @@ export class TypeResolver { } const referenceType = this.getReferenceType(t); - if (referenceType.properties) { + if (referenceType.dataType === 'refObject') { referenceType.properties.forEach(property => properties.push(property)); } diff --git a/src/routeGeneration/routeGenerator.ts b/src/routeGeneration/routeGenerator.ts index 18c7a01bf..5a9283eed 100644 --- a/src/routeGeneration/routeGenerator.ts +++ b/src/routeGeneration/routeGenerator.ts @@ -138,24 +138,29 @@ export class RouteGenerator { const referenceType = this.metadata.referenceTypeMap[name]; const properties: { [name: string]: TsoaRoute.PropertySchema } = {}; - if (referenceType.properties) { + if (referenceType.dataType === 'refObject') { referenceType.properties.map(property => { properties[property.name] = this.buildPropertySchema(property); }); } + const modelSchema = { - enums: referenceType.enums, - properties: Object.keys(properties).length === 0 ? undefined : properties, + ...(referenceType.dataType === 'refEnum' && { enums: referenceType.enums }), + ...(referenceType.dataType === 'refObject' && Object.keys(properties).length > 0 && { properties }), ...(referenceType.dataType === 'refType' && { type: this.buildProperty(referenceType.type) }), } as TsoaRoute.ModelSchema; - if (referenceType.additionalProperties) { - modelSchema.additionalProperties = this.buildProperty(referenceType.additionalProperties); - } else if (this.minimalSwaggerConfig.noImplicitAdditionalProperties) { - modelSchema.additionalProperties = false; - } else { - // Since Swagger allows "excess properties" (to use a TypeScript term) by default - modelSchema.additionalProperties = true; + + if (referenceType.dataType === 'refObject') { + if (referenceType.additionalProperties) { + modelSchema.additionalProperties = this.buildProperty(referenceType.additionalProperties); + } else if (this.minimalSwaggerConfig.noImplicitAdditionalProperties) { + modelSchema.additionalProperties = false; + } else { + // Since Swagger allows "excess properties" (to use a TypeScript term) by default + modelSchema.additionalProperties = true; + } } + models[name] = modelSchema; }); return models; diff --git a/src/swagger/specGenerator2.ts b/src/swagger/specGenerator2.ts index 95e58d3ff..3ab1eaa90 100644 --- a/src/swagger/specGenerator2.ts +++ b/src/swagger/specGenerator2.ts @@ -64,7 +64,7 @@ export class SpecGenerator2 extends SpecGenerator { const referenceType = this.metadata.referenceTypeMap[typeName]; // Object definition - if (referenceType.properties) { + if (referenceType.dataType === 'refObject') { const required = referenceType.properties.filter(p => p.required).map(p => p.name); definitions[referenceType.refName] = { description: referenceType.description, @@ -87,7 +87,7 @@ export class SpecGenerator2 extends SpecGenerator { } // Enum definition - if (referenceType.enums) { + if (referenceType.dataType === 'refEnum') { definitions[referenceType.refName] = { description: referenceType.description, enum: referenceType.enums, @@ -321,7 +321,7 @@ export class SpecGenerator2 extends SpecGenerator { const properties = type.types.reduce((acc, type) => { if (type.dataType === 'refObject') { let refType = type as Tsoa.ReferenceType; - refType = this.metadata.referenceTypeMap[refType.refName]; + refType = this.metadata.referenceTypeMap[refType.refName] as Tsoa.ReferenceObject; const props = refType && diff --git a/src/swagger/specGenerator3.ts b/src/swagger/specGenerator3.ts index 61c9f255e..01176e964 100644 --- a/src/swagger/specGenerator3.ts +++ b/src/swagger/specGenerator3.ts @@ -107,7 +107,7 @@ export class SpecGenerator3 extends SpecGenerator { const referenceType = this.metadata.referenceTypeMap[typeName]; // Object definition - if (referenceType.properties) { + if (referenceType.dataType === 'refObject') { const required = referenceType.properties.filter(p => p.required).map(p => p.name); schema[referenceType.refName] = { description: referenceType.description, @@ -130,7 +130,7 @@ export class SpecGenerator3 extends SpecGenerator { } // Enum definition - if (referenceType.enums) { + if (referenceType.dataType === 'refEnum') { schema[referenceType.refName] = { description: referenceType.description, enum: referenceType.enums,