Skip to content

Commit 891be8c

Browse files
committed
fix unhandled exception for index not found in reflected types
1 parent addba0b commit 891be8c

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/generateSpec.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export function getOperation(route: IRoute): oa.OperationObject {
3232
const operation: oa.OperationObject = {
3333
operationId: getOperationId(route),
3434
parameters: [
35-
...getHeaderParams(route),
36-
...getPathParams(route),
37-
...getQueryParams(route)
35+
... getHeaderParams(route),
36+
... getPathParams(route),
37+
... getQueryParams(route)
3838
],
3939
requestBody: getRequestBody(route) || undefined,
4040
responses: getResponses(route),
@@ -64,7 +64,7 @@ export function getPaths(routes: IRoute[]): oa.PathObject {
6464
}))
6565

6666
// @ts-ignore: array spread
67-
return _.merge(...routePaths)
67+
return _.merge(... routePaths)
6868
}
6969

7070
/**
@@ -127,7 +127,7 @@ export function getPathParams(route: IRoute): oa.ParameterObject[] {
127127
if (meta) {
128128
const metaSchema = getParamSchema(meta)
129129
param.schema =
130-
'type' in metaSchema ? { ...param.schema, ...metaSchema } : metaSchema
130+
'type' in metaSchema ? { ... param.schema, ... metaSchema }: metaSchema
131131
}
132132

133133
return param
@@ -173,26 +173,26 @@ export function getRequestBody(route: IRoute): oa.RequestBodyObject | void {
173173
const bodyParamsSchema: oa.SchemaObject | null =
174174
bodyParamMetas.length > 0
175175
? bodyParamMetas.reduce(
176-
(acc: oa.SchemaObject, d) => ({
177-
...acc,
178-
properties: {
179-
...acc.properties,
180-
[d.name!]: getParamSchema(d)
181-
},
182-
required: isRequired(d, route)
183-
? [...(acc.required || []), d.name!]
184-
: acc.required
185-
}),
186-
{ properties: {}, required: [], type: 'object' }
187-
)
176+
(acc: oa.SchemaObject, d) => ({
177+
... acc,
178+
properties: {
179+
... acc.properties,
180+
[d.name!]: getParamSchema(d)
181+
},
182+
required: isRequired(d, route)
183+
? [... (acc.required || []), d.name!]
184+
: acc.required
185+
}),
186+
{ properties: {}, required: [], type: 'object' }
187+
)
188188
: null
189189

190190
const bodyMeta = route.params.find(d => d.type === 'body')
191191

192192
if (bodyMeta) {
193193
const bodySchema = getParamSchema(bodyMeta)
194194
const { $ref } =
195-
'items' in bodySchema && bodySchema.items ? bodySchema.items : bodySchema
195+
'items' in bodySchema && bodySchema.items ? bodySchema.items: bodySchema
196196

197197
return {
198198
content: {
@@ -221,15 +221,15 @@ export function getContentType(route: IRoute): string {
221221
? 'application/json'
222222
: 'text/html; charset=utf-8'
223223
const contentMeta = _.find(route.responseHandlers, { type: 'content-type' })
224-
return contentMeta ? contentMeta.value : defaultContentType
224+
return contentMeta ? contentMeta.value: defaultContentType
225225
}
226226

227227
/**
228228
* Return the status code of given route.
229229
*/
230230
export function getStatusCode(route: IRoute): string {
231231
const successMeta = _.find(route.responseHandlers, { type: 'success-code' })
232-
return successMeta ? successMeta.value + '' : '200'
232+
return successMeta ? successMeta.value + '': '200'
233233
}
234234

235235
/**
@@ -279,7 +279,7 @@ export function getTags(route: IRoute): string[] {
279279
export function expressToOpenAPIPath(expressPath: string) {
280280
const tokens = pathToRegexp.parse(expressPath)
281281
return tokens
282-
.map(d => (_.isString(d) ? d : `${d.prefix}{${d.name}}`))
282+
.map(d => (_.isString(d) ? d: `${d.prefix}{${d.name}}`))
283283
.join('')
284284
}
285285

@@ -289,7 +289,7 @@ export function expressToOpenAPIPath(expressPath: string) {
289289
*/
290290
function isRequired(meta: { required?: boolean }, route: IRoute) {
291291
const globalRequired = _.get(route.options, 'defaults.paramOptions.required')
292-
return globalRequired ? meta.required !== false : !!meta.required
292+
return globalRequired ? meta.required !== false: !!meta.required
293293
}
294294

295295
/**
@@ -300,8 +300,9 @@ function getParamSchema(
300300
param: ParamMetadataArgs
301301
): oa.SchemaObject | oa.ReferenceObject {
302302
const { explicitType, index, object, method } = param
303-
304-
const type = Reflect.getMetadata('design:paramtypes', object, method)[index]
303+
const reflectedTypes = Reflect.getMetadata('design:paramtypes', object, method);
304+
if (reflectedTypes.length <= index) return {};
305+
const type = reflectedTypes[index]
305306
if (_.isFunction(type) && type.name === 'Array') {
306307
const items = explicitType
307308
? { $ref: '#/components/schemas/' + explicitType.name }

0 commit comments

Comments
 (0)