使用openapi-typescript-codegen,为什么这个Swagger模式会产生一个Record< string,any>的模型?

nr9pn0ug  于 5个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(66)

我的swagger.json包含这个模式:

"schemas": {
            "BuildFruitBody": {
                "properties": {
                    "id": {
                        "type": "number",
                        "format": "double"
                    },
                    "name": {
                        "type": "string"
                    }
                },
                "type": "object",
                "additionalProperties": true
            }
        },

字符串
然而,当我通过npx openapi-typescript-codegen -i ./swagger.json -o src/services/api -c axios生成axios客户端时,生成的模型如下所示:

/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

export type BuildFruitBody = Record<string, any>;


我在这里做错了什么吗?为什么模型没有反映模式?
完整的swagger.json如下:

{
    "components": {
        "examples": {},
        "headers": {},
        "parameters": {},
        "requestBodies": {},
        "responses": {},
        "schemas": {
            "BuildFruitBody": {
                "properties": {
                    "id": {
                        "type": "number",
                        "format": "double"
                    },
                    "name": {
                        "type": "string"
                    }
                },
                "type": "object",
                "additionalProperties": true
            }
        },
        "securitySchemes": {}
    },
    "info": {
        "title": "backend",
        "contact": {}
    },
    "openapi": "3.0.0",
    "paths": {
        "/swagger.json": {
            "get": {
                "operationId": "Get",
                "responses": {
                    "200": {
                        "description": "Ok",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                },
                "tags": [
                    "swagger"
                ],
                "security": [],
                "parameters": []
            }
        },
        "/fruit": {
            "post": {
                "operationId": "Get",
                "responses": {
                    "200": {
                        "description": "Ok",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                },
                "tags": [
                    "fruit"
                ],
                "security": [],
                "parameters": [],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/BuildFruitBody"
                            }
                        }
                    }
                }
            }
        }
    },
    "servers": [
        {
            "url": "/"
        }
    ]
}

b5buobof

b5buobof1#

我是不是做错了什么?
据我所知不在代码范围内。
为什么模型不反映模式?
事实上,它确实反映了图式,但也许是心智模型以不同的方式反映了图式模型,或者是尚未被很好地理解?
当在JSON模式中给定以下内容时(参见additionalProperties JSON模式核心草案-2020-12)

"type": "object",
            "additionalProperties": true

字符串
Record<Keys, Type>实用程序类型

Record<string, any>


看起来是合法的,因为在JSON文本中,属性/特性名称是JSON字符串,可以是以下七个中的任何一个:JSON对象、JSON数组、JSON字符串、JSON编号、"true""false""null"(参见json.org)。
在TypeScript中,这些属性值可以表示为any
我能给予的最好的建议是将其置于自动化测试之下,然后面对第一次测试意外失败(或者没有因为预期的原因失败)的问题,然后修复它。
模式或期望值取决于您的要求。
正如 * defraggled * 根据以下备注所确认的:
啊,是的,设置additionalProperties: false解决了这个问题。谢谢
该要求确实是没有额外的属性(可能被排除在确认之外)。
这是一个强烈的信号,表明它可能已经被排除在模式之外,additionalProperties最近已经从JSON模式验证移动到JSON模式核心(DRAFT-2020-12)。

c9x0cxw0

c9x0cxw02#

您可能偶然发现了JSON Schema的预期行为,但不是您自己的预期,因为additionalProperties:true * 不会 * 限制其他属性在您的数据示例中发送。
additionalProperties: true或缺少additionalProperties定义作为约束是等价的。
这两个模式是等效的,并且将匹配Record<string, any>

{
    "BuildFruitBody": {
        "properties": {
            "id": {
                "type": "number",
                "format": "double"
            },
            "name": {
                "type": "string"
            }
        },
        "type": "object",
        "additionalProperties": true
    }
}

个字符
JSON Schema是一种基于约束的数据验证语言。它只验证提供的约束。由于您定义了additionalProperties: true,因此Record<string,any>的TypeScript模型是正确的注解,因为属性不受您使用additionalProperties:true的约束
只有当您将模式约束为已定义的属性时,才会找到预期的TypeScript定义

{
    "BuildFruitBody": {
        "properties": {
            "id": {
                "type": "number",
                "format": "double"
            },
            "name": {
                "type": "string"
            }
        },
        "type": "object",
        "additionalProperties": false
    }
}
/* generated using openapi-typescript-codegen -- do no edit */
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

export type BuildFruitBody = {
    id?: number;
    name?: string;
};

的字符串

相关问题