Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypedBody Decorator Not Converting Request Body into Instance of Classes #431

Open
benoit-scnd opened this issue Jun 26, 2023 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@benoit-scnd
Copy link

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.

@samchon
Copy link
Owner

samchon commented Jun 26, 2023

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 samchon self-assigned this Jun 26, 2023
@samchon samchon added the question Further information is requested label Jun 26, 2023
@benoit-scnd
Copy link
Author

The new feature request has been created: samchon/typia#683

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants