Custom `type` is not retained as type, but rewritten as `allOf: - $ref: ...` breaking `nullable: true` spec in `@ApiProperty`
gregoryorton-ws opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
Custom type
is not retained as type, but rewritten as allOf: - $ref: ...
breaking nullable: true
spec in @ApiProperty
In @ApiProperty
when using something like:
@ApiProperty({ nullable: true, type: MyCustomDto })
readonly myVar: MyCustomDto | null;
...
the rendered openapi.yaml is like:
myVar:
nullable: true
allOf:
- $ref: "#/components/schemas/MyCustomDto"
tools like Redoc.ly then start to throw errors:
![image](https://private-user-images.githubusercontent.com/159014773/330133192-a58cdd28-3a82-48cc-abd1-e817da2c22e5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg3OTQwMzYsIm5iZiI6MTcxODc5MzczNiwicGF0aCI6Ii8xNTkwMTQ3NzMvMzMwMTMzMTkyLWE1OGNkZDI4LTNhODItNDhjYy1hYmQxLWU4MTdkYTJjMjJlNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNjE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDYxOVQxMDQyMTZaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jZDRiMWIwNmMxYzQ5MmFiNDg3YjU4NWYxNjlkZjU0ZTViNTUxODQ5OTdlOTQyYzA3NTkzY2QyMzkwNGQ4YWYzJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.SkiwE5gJqFeEYZrXijSQ9yTqnI8W5kMwOYZ8tFzIfhY)
Minimum reproduction code
Steps to reproduce
see above
Expected behavior
myVar:
type: object
properties:
id:
type: integer
Package version
7.3.0
NestJS version
10.3.3
Node.js version
20
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
Would you like to create a PR for this issue?
Sure, but I don't know what the solution would be here -
what should the value of type
be? Would we need to put the custom DTO into a schemas
and then reference it?
One solution looks like:
myVar:
nullable: true
allOf:
- $ref: "#/components/schemas/MyCustomDto"
- type: object
I will test with redoc.ly to see if it's acceptable. If it is, then I think we can just add this type
to the output for the custom type.
The only think redoc.ly will accept is:
myVar:
nullable: true
type: object
allOf:
- $ref: "#/components/schemas/MyCustomDto"
so it seems like an easy fix
Here is the source of our woes:
swagger/lib/services/schema-object-factory.ts
Lines 368 to 379 in 5404905
Extra keys that are not in the set of ['type', 'isArray', 'required']
will result in an allOf
instead of a direct $ref
. In this case (and #2645), the key is nullable
. In my case, it's name
.
The list of supported keys needs to be expanded, or removed altogether. I'm not sure which one as I haven't read the OpenAPI spec to any level of detail.