openapi typescript-fetch 将不可空类型生成为可空类型

jjj*_*jzi 6 typescript openapi openapi-generator

基本上我的所有 DTO 都是使用可空字段生成的,例如:id?: number;。如何为未标记为可为空的类型创建不可为空的类型。

我的 DTO 架构如下所示:

 "UserDTO": {
        "type": "object",
        "properties": {
          "firstName": {
            "type": "string",
            "nullable": true
          },
          "lastName": {
            "type": "string",
            "nullable": true
          },
          "id": {
            "type": "integer",
            "format": "int32"
          }
        }
Run Code Online (Sandbox Code Playgroud)

我生成这样的代码: 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

将其添加到较旧的问题中,因为我不得不花费大量时间研究这个问题。不幸的是,OpenAPI 打字稿生成器忽略了响应模型的可为空属性,而是使用“必需”列表。

为了生成具有不可为空属性的模型接口,您需要根据需要列出属性。

在上面的例子中应该是:

 "UserDTO": {
        "type": "object",
        "required": [
          "id"
        ],
        "properties": {
          "firstName": {
            "type": "string",
            "nullable": true
          },
          "lastName": {
            "type": "string",
            "nullable": true
          },
          "id": {
            "type": "integer",
            "format": "int32"
          }
        }
Run Code Online (Sandbox Code Playgroud)