-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathspecGenerator.ts
232 lines (205 loc) · 8.99 KB
/
specGenerator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { ExtendedSpecConfig } from '../cli';
import { Tsoa, assertNever, Swagger } from '@tsoa/runtime';
import * as handlebars from 'handlebars';
export abstract class SpecGenerator {
constructor(protected readonly metadata: Tsoa.Metadata, protected readonly config: ExtendedSpecConfig) {}
protected buildAdditionalProperties(type: Tsoa.Type) {
return this.getSwaggerType(type);
}
protected buildOperationIdTemplate(inlineTemplate: string) {
handlebars.registerHelper('titleCase', (value: string) => (value ? value.charAt(0).toUpperCase() + value.slice(1) : value));
handlebars.registerHelper('replace', (subject: string, searchValue: string, withValue = '') => (subject ? subject.replace(searchValue, withValue) : subject));
return handlebars.compile(inlineTemplate, { noEscape: true });
}
protected getOperationId(controllerName: string, method: Tsoa.Method) {
return this.buildOperationIdTemplate(this.config.operationIdTemplate ?? '{{titleCase method.name}}')({
method,
controllerName,
});
}
public throwIfNotDataFormat(strToTest: string): Swagger.DataFormat {
const guiltyUntilInnocent = strToTest as Swagger.DataFormat;
if (
guiltyUntilInnocent === 'int32' ||
guiltyUntilInnocent === 'int64' ||
guiltyUntilInnocent === 'float' ||
guiltyUntilInnocent === 'double' ||
guiltyUntilInnocent === 'byte' ||
guiltyUntilInnocent === 'binary' ||
guiltyUntilInnocent === 'date' ||
guiltyUntilInnocent === 'date-time' ||
guiltyUntilInnocent === 'password'
) {
return guiltyUntilInnocent;
} else {
return assertNever(guiltyUntilInnocent);
}
}
public throwIfNotDataType(strToTest: string): Swagger.DataType {
const guiltyUntilInnocent = strToTest as Swagger.DataType;
if (
guiltyUntilInnocent === 'array' ||
guiltyUntilInnocent === 'boolean' ||
guiltyUntilInnocent === 'integer' ||
guiltyUntilInnocent === 'file' ||
guiltyUntilInnocent === 'number' ||
guiltyUntilInnocent === 'object' ||
guiltyUntilInnocent === 'string' ||
guiltyUntilInnocent === 'undefined'
) {
return guiltyUntilInnocent;
} else {
return assertNever(guiltyUntilInnocent);
}
}
protected getSwaggerType(type: Tsoa.Type, title?: string): Swagger.Schema | Swagger.BaseSchema {
if (type.dataType === 'void' || type.dataType === 'undefined') {
return this.getSwaggerTypeForVoid(type.dataType);
} else if (type.dataType === 'refEnum' || type.dataType === 'refObject' || type.dataType === 'refAlias') {
return this.getSwaggerTypeForReferenceType(type);
} else if (
type.dataType === 'any' ||
type.dataType === 'binary' ||
type.dataType === 'boolean' ||
type.dataType === 'buffer' ||
type.dataType === 'byte' ||
type.dataType === 'date' ||
type.dataType === 'datetime' ||
type.dataType === 'double' ||
type.dataType === 'float' ||
type.dataType === 'file' ||
type.dataType === 'integer' ||
type.dataType === 'long' ||
type.dataType === 'object' ||
type.dataType === 'string'
) {
return this.getSwaggerTypeForPrimitiveType(type.dataType);
} else if (type.dataType === 'array') {
return this.getSwaggerTypeForArrayType(type, title);
} else if (type.dataType === 'enum') {
return this.getSwaggerTypeForEnumType(type, title);
} else if (type.dataType === 'union') {
return this.getSwaggerTypeForUnionType(type, title);
} else if (type.dataType === 'intersection') {
return this.getSwaggerTypeForIntersectionType(type, title);
} else if (type.dataType === 'nestedObjectLiteral') {
return this.getSwaggerTypeForObjectLiteral(type, title);
} else {
return assertNever(type);
}
}
protected abstract getSwaggerTypeForUnionType(type: Tsoa.UnionType, title?: string): Swagger.Schema | Swagger.BaseSchema;
protected abstract getSwaggerTypeForIntersectionType(type: Tsoa.IntersectionType, title?: string): Swagger.Schema | Swagger.BaseSchema;
protected abstract buildProperties(properties: Tsoa.Property[]): { [propertyName: string]: Swagger.Schema | Swagger.Schema3 };
public getSwaggerTypeForObjectLiteral(objectLiteral: Tsoa.NestedObjectLiteralType, title?: string): Swagger.Schema {
const properties = this.buildProperties(objectLiteral.properties);
const additionalProperties = objectLiteral.additionalProperties && this.getSwaggerType(objectLiteral.additionalProperties);
const required = objectLiteral.properties.filter(prop => this.isRequiredWithoutDefault(prop) && !this.hasUndefined(prop)).map(prop => prop.name);
// An empty list required: [] is not valid.
// If all properties are optional, do not specify the required keyword.
return {
...(title && { title }),
properties,
...(additionalProperties && { additionalProperties }),
...(required && required.length && { required }),
type: 'object',
};
}
protected getSwaggerTypeForReferenceType(_referenceType: Tsoa.ReferenceType): Swagger.BaseSchema {
return {
// Don't set additionalProperties value here since it will be set within the $ref's model when that $ref gets created
};
}
protected getSwaggerTypeForVoid(_dataType: 'void' | 'undefined'): Swagger.BaseSchema {
// Described here: https://swagger.io/docs/specification/describing-responses/#empty
const voidSchema = {
// isn't allowed to have additionalProperties at all (meaning not a boolean or object)
};
return voidSchema;
}
protected determineImplicitAdditionalPropertiesValue = (): boolean => {
if (this.config.noImplicitAdditionalProperties === 'silently-remove-extras') {
return false;
} else if (this.config.noImplicitAdditionalProperties === 'throw-on-extras') {
return false;
} else if (this.config.noImplicitAdditionalProperties === 'ignore') {
return true;
} else {
return assertNever(this.config.noImplicitAdditionalProperties);
}
};
protected getSwaggerTypeForPrimitiveType(dataType: Tsoa.PrimitiveTypeLiteral): Swagger.Schema {
if (dataType === 'object') {
if (process.env.NODE_ENV !== 'tsoa_test') {
// eslint-disable-next-line no-console
console.warn(`The type Object is discouraged. Please consider using an interface such as:
export interface IStringToStringDictionary {
[key: string]: string;
}
// or
export interface IRecordOfAny {
[key: string]: any;
}
`);
}
}
const map: Record<Tsoa.PrimitiveTypeLiteral, Swagger.Schema> = {
any: {
// While the any type is discouraged, it does explicitly allows anything, so it should always allow additionalProperties
additionalProperties: true,
},
binary: { type: 'string', format: 'binary' },
boolean: { type: 'boolean' },
buffer: { type: 'string', format: 'byte' },
byte: { type: 'string', format: 'byte' },
date: { type: 'string', format: 'date' },
datetime: { type: 'string', format: 'date-time' },
double: { type: 'number', format: 'double' },
file: { type: 'file' },
float: { type: 'number', format: 'float' },
integer: { type: 'integer', format: 'int32' },
long: { type: 'integer', format: 'int64' },
object: {
additionalProperties: this.determineImplicitAdditionalPropertiesValue(),
type: 'object',
},
string: { type: 'string' },
};
return map[dataType];
}
protected getSwaggerTypeForArrayType(arrayType: Tsoa.ArrayType, title?: string): Swagger.Schema {
return {
items: this.getSwaggerType(arrayType.elementType, title),
type: 'array',
};
}
protected determineTypesUsedInEnum(anEnum: Array<string | number | boolean | null>) {
const typesUsedInEnum = anEnum.reduce((theSet, curr) => {
const typeUsed = curr === null ? 'number' : (typeof curr as 'string' | 'number' | 'boolean');
theSet.add(typeUsed);
return theSet;
}, new Set<'string' | 'number' | 'boolean'>());
return typesUsedInEnum;
}
protected abstract getSwaggerTypeForEnumType(enumType: Tsoa.EnumType, title?: string): Swagger.Schema2 | Swagger.Schema3;
protected hasUndefined(property: Tsoa.Property): boolean {
return property.type.dataType === 'undefined' || (property.type.dataType === 'union' && property.type.types.some(type => type.dataType === 'undefined'));
}
protected queriesPropertyToQueryParameter(property: Tsoa.Property): Tsoa.Parameter {
return {
parameterName: property.name,
example: [property.example as any],
description: property.description,
in: 'query',
name: property.name,
required: this.isRequiredWithoutDefault(property),
type: property.type,
default: property.default,
validators: property.validators,
deprecated: property.deprecated,
};
}
protected isRequiredWithoutDefault(prop: Tsoa.Property | Tsoa.Parameter): boolean | undefined {
return prop.required && prop.default == null;
}
}