tsoa
tsoa copied to clipboard
How to return integer from method call
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]
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.👀
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": []
}
}
}
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.
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.
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!
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
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.
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