question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@Body() don't transfer body object to dto object

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
XGHeavencommented, Apr 11, 2018

@unlight Thanks

It works for me.

// ...
async index(@Body(new ValidationPipe({transform: true})) body: BodyDTO) {
  console.log(body instanceof BodyDTO) // true
}
// ...

I suggest updating docs.

4reactions
unlightcommented, Apr 11, 2018

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 pipe ValidationPipe.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found