-
Notifications
You must be signed in to change notification settings - Fork 516
Description
Is there an existing issue for this?
- I have searched the existing issues
#2948 seems similar but was closed as fixed and I'm presuming it was incorporated into the 8.x branch.
Current behavior
With the plugin enabled and looking at the Typescript code a property like:
@ApiProperty({
description: 'The user that owns the cat',
})
nullableOwner: UserDto | null;, it generates the schema's property like:
"nullableOwner": {
"nullable": true,
"description": "The user that owns the cat",
"allOf": [
{
"$ref": "#/components/schemas/UserDto"
}
]
}Redocly's linter flags as an error:
[4] dist/api-spec.json:84:25 at #/components/schemas/CreateCatDto/properties/nullableOwner/nullable
The `type` field must be defined when the `nullable` field is used.
82 | },
83 | "nullableOwner": {
84 | "nullable": true,
85 | "description": "The user that owns the cat",
86 | "allOf": [
Error was generated by the struct rule.
Minimum reproduction code
https://github.com/drewish/swagger-nullable-poc
Steps to reproduce
Setup the repro repo:
git clone https://github.com/drewish/swagger-nullable-poc
cd swagger-nullable-poc
npm i
Start it up to generate the swagger file (main.ts is saving a copy)
npm start
Then use redocly to lint the file:
npx @redocly/cli@latest lint dist/api-spec.json
There are unrelated errors I didn't bother cleaning up. The relevant one is:
The `type` field must be defined when the `nullable` field is used.
Expected behavior
One workaround I've see for this (microsoft/typespec#4584) was to just add type: 'object' as a sibling:
"nullableOwner": {
"type": "object",
"nullable": true,
"description": "The user that owns the cat",
"allOf": [
{
"$ref": "#/components/schemas/UserDto"
}
]
}
That renders nicely in Redocly's UI:

And looks the same in SwaggerUI:

It seems like in ModelClassVisitor.createTypePropertyAssignments(), when we create the nullable property:
swagger/lib/plugin/visitors/model-class.visitor.ts
Lines 437 to 443 in 8cb1ee8
| return [ | |
| ...propertyAssignments, | |
| factory.createPropertyAssignment( | |
| 'nullable', | |
| createBooleanLiteral(factory, true) | |
| ) | |
| ]; |
We could also add
type: 'object'.
But, we'd also need a change in SchemaObjectFactory.extractPropertiesFromType() to keep it from deleting the type:
swagger/lib/services/schema-object-factory.ts
Line 214 in 8cb1ee8
| delete schemaObjectMetadata.type; |
Package version
11.0.3
NestJS version
11.0.1
Node.js version
v20.16.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response