[Bug] - Property/Field Overwriting Doesn't Manifest In Swagger When Extending A Class
See original GitHub issueI’m submitting a…
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
When I extend a class and try overwrite an inherited property/field. The result of the overwriting doesn’t manifest in the Swagger documentation. See code below how to reproduce.
Expected behavior
The desired behaviour would be how the overwriting manifests in the TypeScript code: The property info of EntityPutDTO should be of type InfoPutDTO, not InfoPostDTO. (See code below)
Minimal reproduction of the problem with instructions
export class InfoPostDTO {
@ApiProperty()
name: string;
}
export class InfoPutDTO extends InfoPostDTO {
@ApiProperty()
id: number;
}
export class EntityPostDTO {
@ApiProperty()
id: number;
@ApiProperty()
info: InfoPostDTO;
}
export class EntityPutDTO extends EntityPostDTO {
@ApiProperty()
info: InfoPutDTO;
}
What is the motivation / use case for changing the behavior?
The motivation is that the classes manifest themselves the same way into the documentation that they do in the TypeScript code.
Environment
Nest version: 7.6.15
"@nestjs/common": "^7.6.15",
"@nestjs/core": "^7.6.15",
"@nestjs/platform-express": "^7.6.15",
"@nestjs/swagger": "^4.8.0",
For Tooling issues:
- Node version: v15.12.0
- Platform: Linux (Ubuntu 20.04)
Others:
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Swagger Inheritance and Composition - Stack Overflow
In my "simplified" API, all responses are derived (inherit) from a base "response" class.
Read more >.NET – Bruno Sonnino - Msmvps
Open MainViewModel.cs, you will see an error in ColletionViewSource: That's because the CollectionViewSource class doesn't exist in Avalonia and we need to ...
Read more >The Non-Developer's Guide to Building Business Applications
tables, we cannot connect model-driven apps to other data sources, ... example of a Dynamics 365 business that extends the Customer Service app...
Read more >Release Notes - Documentation ・ CoreMedia
CMS-20302: Updated documentation of extensions and their dependencies ... workflowconverter checks only for class incompatibilities but does not detect ...
Read more >BIG-IP 15.0.1.3 Fixes and Known Issues - AskF5 - F5 Networks
Error response from SMTP server, and the report is not sent. Workaround: ... 745465-2, 4-Minor, The tcpdump file does not provide the correct...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found a workaround/solution for the issue. The key is to not overwrite any fields. I solved this by creating an abstract base entity DTO with all the fields that will be not overwritten. Then all the other DTOs inherit that base entity. This way no fields are overwritten, thus avoiding the bug.
The bug is still there but with this workaround you can avoid it.
I worked on the bug and I came to the conclusion that the issue can be found in the file
lib/services/schema-object-factory.ts
, in the functionmergePropertyWithMetadata
where themetadata
variable is set with the help ofReflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, prototype, key)
.In the example issue above (and in the minimun reproduction repository), when schema objects are gathered for the
EntityPutDTO
and when looking for the type for the problematicinfo
field, after setting themetadata
variable, the variables in the function are as follows: Themetadata.type
is the type of theinfo
field in theEntityPostDTO
from which theEntityPutDTO
inherits. You can also see that theprototype
isEntityPostDTO
and notEntityPutDTO
.I couldn’t figure out if the
Reflect.getMetadata
doesn’t function correctly or if theprototype
is not what it should be BUT, if theprototype
is not what is should be, the problem can traced to the filelib/explorers/api-response.explorer.ts
to the functionexploreApiResponseMetadata
where theresponses
array is defined with the help ofReflect.getMetadata(constants_2.DECORATORS.API_RESPONSE, method)
. Theprototype
is then get asresponse.type.prototype
. So in any case, in my very unprofessional opinion, the issue stems fromReflect.getMetadata
.I don’t really have energy to debug the issue further but if someone else wants to give it a try, this is a good place to start.