Skip to content

Bug Report: Crash when using @QueryParams with type Object #159

@manimorris

Description

@manimorris

Library version: 5.0.0
Node version: 20.12.1

Description

When using the @QueryParams() decorator to inject a parameter of type Object, the OpenAPI generation crashes. The error occurs in generateSpec.js, specifically in the getParamSchema() function. At the end of this function, when type.name === 'Object', no $ref is assigned. As a result, the getQueryParams() function (line 125) attempts to reference an undefined schema and crashes the process.
The following line: const paramSchemaName = paramSchema.$ref.split('/').pop() || ''; causes the crash because paramSchema.$ref is undefined.

Steps to Reproduce

  1. Define a controller route like this:

    @Get('/some-route')
    someAction(@QueryParams() query: Object) {  // or @QueryParams() query: any
      // ...
    }
  2. Call routingControllersToSpec(...) to generate the OpenAPI schema.

  3. The generator crashes due to $ref being undefined for the Object-typed query parameter.

Expected Behavior

When encountering a query parameter of a generic Object type - provide a default fallback schema (e.g. type: 'object'), or

Additional Context / Workarounds

  • There's an older issue around parsing @QueryParams() in the repository, but it refers to ignoring multiple query params—not this crash behavior ([github.com][1], [npmjs.com][2]).
  • Providing a class-based DTO for query parameters (with decorators like @IsInt(), etc.) works safely and avoids crashes.

Proposed Fix

In getParamSchema():

  • Detect when type.name === 'Object'.

In getQueryParams():

  • Use guard clauses to check if paramSchema.$ref is defined before attempting to split it.
  • Or use ?. to safely access paramSchema.$ref. like this: const paramSchemaName = paramSchema.$ref?.split('/').pop() || '';

Another Issue:

In the 'OpenApi.js' file, the isSchemaObject function may also cause a crash when the schema is undefined. This can happen if the schema is not properly defined or if the type is not recognized.

Proposed Fix for isSchemaObject

In OpenApi.js, modify the isSchemaObject function to handle undefined schemas gracefully:

function isSchemaObject(schema) {
    return schema && typeof schema === "object" && !Array.isArray(schema);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions