Skip to content

Commit

Permalink
Improve refX type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
WoH committed Sep 28, 2019
1 parent cd5329c commit a91dae8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/metadataGeneration/tsoa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ export namespace Tsoa {
description?: string;
dataType: RefTypeLiteral;
refName: string;
properties?: Property[];
additionalProperties?: Type;
enums?: string[];
example?: any;
}

export type ReferenceType = ReferenceObject | ReferenceEnum | ReferenceTypeAlias;

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<Property, 'name' | 'required'>, ReferenceTypeBase {
Expand Down
16 changes: 8 additions & 8 deletions src/metadataGeneration/typeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -430,7 +430,7 @@ export class TypeResolver {
}

private createCircularDependencyResolver(refName: string) {
const referenceType = {
let referenceType = {
dataType: 'refObject',
refName,
} as Tsoa.ReferenceType;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down
25 changes: 15 additions & 10 deletions src/routeGeneration/routeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/swagger/specGenerator2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 &&
Expand Down
4 changes: 2 additions & 2 deletions src/swagger/specGenerator3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit a91dae8

Please sign in to comment.