Why are integers serialized as strings?

22 Views Asked by At

I got a simple endpoint which returns a simple DTO:

interface Topic {
  /**
   * @isInt ID must be an integer.
   */
  id: number;
  name: string;
}

Generating the swagger.yaml works as expected:

Topic:
    properties:
        id:
            type: integer
            format: int32
        name:
            type: string
    required:
        - id
        - name
    type: object
    additionalProperties: false

the problem is that the actual serialization is wrong. Instead of an integer, id will be serialized as a string:

{"id": "1", "name": "Science"}

Is this expected/normal/standard or am I doing something wrong here?

I'd expect basic types such as boolean, integer, decimal numbers and strings to be serialized accordingly.

1

There are 1 best solutions below

0
Filip Seman On

It should serialize according to your interface. This appears to be a bug. Which version are you currently running? I'm unable to replicate this issue on the latest version, v6.2.

Here is my controller.

import {
    Body,
    Controller,
    Post,
    Route,
} from "tsoa";

interface Topic {
    /**
     * @isInt ID must be an integer.
     */
    id: number;
    name: string;
}

@Route("/")
export class TopicController extends Controller {
    @Post()
    public async getTopic(@Body() requestBody: Topic) {
        console.log(requestBody);
        //  { id: 1, name: 'foo' }
    }
}
curl  -X POST \
  'localhost:8000/' \
  --header 'Accept: */*' \
  --header 'Content-Type: application/json' \
  --data-raw '{
  "id": 1,
  "name": "foo"
}'