Why are integers serialized as strings?

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.

Leave a Comment