acacode / acacode/swagger-typescript-api

Nullable components with required values in complex schema types

未關閉
#497 1 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
TypeScript
星號
4.1k
分支
436
PR 合併指標
30 天內沒有已合併 PR

描述

Release 10.0.2 (https://github.com/acacode/swagger-typescript-api/pull/419) added the `UtilRequiredKeys` type that produces invalid generated code when you have a schema with complex schema types (anyOf/allOf/oneOf) that reference required fields of nullable components.

How to trigger in a schema:
* Have a nullable component (`myObject` is the schema below)
* Reference component with complex schema type (`TestObject` in the schema below)
* Mark a nested field as required (`"required": ["name"],` as marked in `TestObject`)

Example schema:

```json
{
"openapi": "3.0.2",
"info": {
"title": "Nullable Refs Example",
"version": "1.0.0"
},
"components": {
"schemas": {
"myObject": {
"type": "object",
"nullable": true,
"properties": {
"name": {
"type": "string"
}
},
"required": ["name"]
},
"TestObject": {
"required": ["name"],
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/myObject"
}
]
}
}
}
}
```

Generated code from schema (utilising the nullable-3.0 test setup)

```ts
type UtilRequiredKeys = Omit & Required>;

export type MyObject = {
name: string;
} | null;

export type TestObject = UtilRequiredKeys;
```

Which produces the following error

```bash
Type 'string' does not satisfy the constraint 'never'.ts(2344)
```

Potential workarounds:
* Adjust schema to avoid this scenario
* remove usage of `UtilRequiredKeys` via the `configuration.internalTemplateOptions.addUtilRequiredKeysType`
* override `UtilRequiredKeys` definition in template to handle null case via ts NonNullable

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。