Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openapi typescript-fetch generates non-nullable types as nullable

Basically all my DTOs are generated with nullable fields like: id?: number;. How can I create non-nullable types for types which are not flagged as nullable.

My DTO schema looks like this:

 "UserDTO": {
        "type": "object",
        "properties": {
          "firstName": {
            "type": "string",
            "nullable": true
          },
          "lastName": {
            "type": "string",
            "nullable": true
          },
          "id": {
            "type": "integer",
            "format": "int32"
          }
        }

I generate the code like this: openapi-generator generate --additional-properties=prefixParameterInterfaces=true,typescriptThreePlus=true --remove-operation-id-prefix -i libs/services/defs/swagger.json -g typescript-fetch -o libs/services/src/api

like image 484
jjjzi Avatar asked Aug 14 '26 12:08

jjjzi


1 Answers

Adding this to older question as I had to spend a lot of time researching this. Unfortunately OpenAPI typescript generator ignores the nullable property for response model and instead uses the 'required' list.

In order to generate model interfaces with non-nullable properties you need to list the properties as required.

In the case above it should be:

 "UserDTO": {
        "type": "object",
        "required": [
          "id"
        ],
        "properties": {
          "firstName": {
            "type": "string",
            "nullable": true
          },
          "lastName": {
            "type": "string",
            "nullable": true
          },
          "id": {
            "type": "integer",
            "format": "int32"
          }
        }
like image 88
William Avatar answered Aug 16 '26 01:08

William