tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

How to return integer from method call

Open szekelyisz opened this issue 2 years ago • 8 comments

Sorting

  • I'm submitting a ...

    • [ ] bug report
    • [ ] feature request
    • [x] support request
  • I confirm that I

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

Expected Behavior

I want a controller method to return an int32. I didn't find any documented way to do this. JSDoc annotation /** @isInt */ works for parameters and number fields of structures, but not for response bodies (or at least it's not documented).

"responses": {
  "200": {
    "description": "Ok",
    "content": {
      "application/json": {
        "schema": {
          "type": "integer",
          "format": "int32"
        }
      }
    }
}

How do you achieve this?

Context (Environment)

Version of the library: 4.1.1 Version of NodeJS: 12.22.12

  • Confirm you were using yarn not npm: [X]

szekelyisz avatar Jul 19 '22 22:07 szekelyisz

Hello there szekelyisz 👋

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

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

github-actions[bot] avatar Jul 19 '22 22:07 github-actions[bot]

Found a hacky workaround.

import { Controller, Get, Route } from "tsoa";

/**
 * @isInt
 */
type Integer = number;

@Route("/")
export class MyController extends Controller {
  @Get("/")
  public async get(): Promise<Integer> {
    return Promise.resolve(1);
  }
}

which generates

  "components": {
    "schemas": {
      "Integer": {
        "type": "integer",
        "format": "int32"
      },
    }
  }

  "paths": {
    "/": {
      "get": {
        "operationId": "Get",
        "responses": {
          "200": {
            "description": "Ok",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Integer"
                }
              }
            }
          }
        },
        "security": [],
        "parameters": []
      }
    }
  }

szekelyisz avatar Jul 27 '22 22:07 szekelyisz

Do I have to copy this Integer type into every controller file? I tried to add it into utils and import from there, but I'm then getting an error: Generate routes error. GenerateMetadataError: No declarations found for referenced type Integer.

It works the way you showed, @szekelyisz, but I'm just thinking how I could just define it only a single time.

harryn avatar Aug 05 '22 13:08 harryn

Do I have to copy this Integer type into every controller file? I tried to add it into utils and import from there, but I'm then getting an error: Generate routes error. GenerateMetadataError: No declarations found for referenced type Integer.

@harryn I think it's sufficient to put it in one of the controller files, then you can import it in other controllers as well. However, you can't put it outside of your controller files, otherwise tsoa won't see it. Check out #340, #377 and #1222 for details.

szekelyisz avatar Aug 06 '22 00:08 szekelyisz

Alright, thanks!

I got it to work by importing with relative path like this import { Integer } from "../../utils"; instead of import { Integer } from "src/utils";.

So far seems that this relative path approach works. But is there a reason that it isn't mentioned, any drawbacks? Because thanks to this I can use my preferred structure.

Thank you for your time!

harryn avatar Aug 08 '22 13:08 harryn

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

github-actions[bot] avatar Sep 08 '22 00:09 github-actions[bot]

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

The issue is still present.

szekelyisz avatar Sep 08 '22 09:09 szekelyisz

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

github-actions[bot] avatar Oct 09 '22 00:10 github-actions[bot]