openapi typescript-fetch generates non-nullable types as nullable
Asked Answered
D

2

6

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

Disk answered 8/10, 2020 at 12:22 Comment(0)
C
1

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"
          }
        }
Chaunce answered 17/8, 2023 at 15:34 Comment(0)
F
0

Add the [Required] data annotation

Farceur answered 29/10 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.