@Body() don't transfer body object to dto object
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
@Controller()
export class ApiController {
@Post()
async index(@Body() body: BodyDTO) {
// `body` is not a BodyDTO, just a normal object
console.log(body instanceof BodyDTO) // false
}
}
Expected behavior
@Controller()
export class ApiController {
@Post()
async index(@Body() body: BodyDTO) {
console.log(body instanceof BodyDTO) // true
}
}
What is the motivation / use case for changing the behavior?
Sometimes, I need to do something with dto object.
class BodyDTO {
readonly firstName: string
readonly lastName: string
get name(): string { return this.firstName + ' ' + this.lastName }
}
It’s not convenience without dto object. And it’s not a intuitive way.
Environment
Nest version: 4.6.6
For Tooling issues:
- Node version: 9.11.1
- Platform: Mac
Issue Analytics
- State:
- Created 5 years ago
- Comments:17 (2 by maintainers)
Top Results From Across the Web
How to construct DTO in Nest.js for @Body - Stack Overflow
The answer is: You don't need to modify your DTO . @Body() decorator also takes in an optional argument: @Body(path?: string) .
Read more >Data Transfer Object (DTO) - Oat++
DTO is any object of the class which extends oatpp::DTO. It is a special object which can be Serialized and Deserialized with the...
Read more >Entity To DTO Conversion for a Spring REST API - Baeldung
In this tutorial, we'll handle the conversions that need to happen between the internal entities of a Spring application and the external DTOs...
Read more >Converting DTO and Entity - Medium
Talking about Request-Response Body, we usually serialize and deserialize that body into an object class. The question is, what kind of ...
Read more >Response Body with Data Transfer Object - DTO in Spring Boot
In this tutorial we will see how we can create our response model with dto Spring Boot.The source code of this tutorial is...
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
@unlight Thanks
It works for me.
I suggest updating docs.
Transforming plain object to specific class is not applying automatically. You need to use
pipes
(see docs) You can write your own, or you can use built-in pipeValidationPipe
.