openapi typescript-fetch generates non-nullable types as nullable

972 views Asked by At

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

1

There are 1 answers

0
William On

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"
          }
        }