-
Notifications
You must be signed in to change notification settings - Fork 64
Description
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
-
Define a controller route like this:
@Get('/some-route') someAction(@QueryParams() query: Object) { // or @QueryParams() query: any // ... }
-
Call
routingControllersToSpec(...)to generate the OpenAPI schema. -
The generator crashes due to
$refbeing undefined for theObject-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.$refis defined before attempting to split it. - Or use
?.to safely accessparamSchema.$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);
}