routing-controllers-openapi icon indicating copy to clipboard operation
routing-controllers-openapi copied to clipboard

TypeError: Cannot read properties of undefined (reading 'split')

Open lyf7 opened this issue 3 years ago • 1 comments

There are a bug in the generateSpec.ts file. like

    const schema = getParamSchema(headersMeta) as oa.ReferenceObject
    headers.push({
      in: 'header',
      name: schema.$ref.split('/').pop() || '',
      required: isRequired(headersMeta, route),
      schema,
    })
    const paramSchema = getParamSchema(queriesMeta) as oa.ReferenceObject
    // the last segment after '/'
    const paramSchemaName = paramSchema.$ref.split('/').pop() || ''
    const currentSchema = schemas[paramSchemaName]

The getParamSchema function may return a empty object which result in an error. What's more, I change

    // example1
    name: schema.$ref.split('/').pop() || '',
    // example2
    const paramSchemaName = paramSchema.$ref.split('/').pop() || ''

to

    // example1
    name: (schema.$ref || '').split('/').pop() || '',
    // example2
    const paramSchemaName = (paramSchema.$ref || '').split('/').pop() || ''

And it works.

lyf7 avatar Jul 06 '23 02:07 lyf7

For me this error was caused because of 2 things:

  • Using a Partial type
  • Using a destructured object

Both of these only caused issues when used in methods annotated with @Get. I'd guess it didn't happen on other HTTP methods, because those have bodies, instead of query params.

martinmeow avatar Jul 24 '24 09:07 martinmeow