nestia icon indicating copy to clipboard operation
nestia copied to clipboard

TypedBody Decorator Not Converting Request Body into Instance of Classes

Open benoit-scnd opened this issue 1 year ago • 2 comments

Bug Report

When using the @TypedBody() decorator, it appears that it's not properly transforming the incoming request body into an instance of the given Request class.

Steps to reproduce

Define a route handler method in a controller and use the @TypedBody() decorator to inject the request body, which should be an instance of a certain class (e.g., Request). Define a class with a method (e.g., getTest), and try to call this method on the request body in the route handler method. Send a request to the route.

import { Controller, Post } from '@nestjs/common';
import { TypedBody } from '@nestia/core';

class Request {
  getTest() {
    return 'request';
  }
}

@Controller('tests')
export class PostTestAction {
  @Post()
  async execute(@TypedBody() request: Request): Promise<void> {
    console.log(request.getTest());
  }
}

See error: "TypeError: request.getTest is not a function"

Expected behavior

The @TypedBody() decorator should correctly convert the incoming request body into an instance of the Request class. The method (e.g., getTest) defined in the Request class should then be callable on the request object in the route handler.

Additional context

This issue is critical as it prevents proper type validation and transformation of the incoming request body, a common requirement in type-safe server-side applications.

benoit-scnd avatar Jun 26 '23 09:06 benoit-scnd

Current nestia does not performan class conversion, and recommends to use pure interface type instead.

If nestia try to support class conversion, it may possible to convert the top level object, because the instance would be imported in the controller class. However, children instances are not. It is hard to compile them, because finding source of children class files are not easy thing.

Therefore, if you want such feature, I'll try but cannot sure of its success. If you want to keep going, would be write an issue in the typia repo? Also, think proper function name please.

import typia from "typia";

typia.XXX<T>(input: Primitive<T>): T;

samchon avatar Jun 26 '23 09:06 samchon

The new feature request has been created: https://github.com/samchon/typia/issues/683

benoit-scnd avatar Jun 26 '23 10:06 benoit-scnd