FastAPI:openapi.json 的请求正文无效(OAS 3.0.3)

问题描述 投票:0回答:1

我正在尝试使用 openapi.json,它是由 FastAPI(版本 0.109.0)自动创建的。 openapi 版本是 3.0.3,我需要将其上传到应用程序,该应用程序不支持使用

anyOf
allOf
OneOf
定义的架构。

openapi.json 看起来像这样:

{
    "openapi": "3.0.3",
    "info": {
        "title": "SomeTitle",
        "description": "SomeDescription",
        "version": "1.2.0"
    },
    "servers": [
        {
            "url": "http://localhost:8080",
            "description": "Local Environment for testing"
        },
        {
            "url": "Productive URL",
            "description": "Productive Instance"
        }
    ],
    "paths": {
        "/api/v1/rag/prepare-index": {
            "post": {
                "tags": [
                    "Retrieval Augmented Generation"
                ],
                "summary": "Build Index",
                "operationId": "Build_Index",
                "security": [
                    {
                        "APIKeyHeader": []
                    }
                ],
                "parameters": [
                    {
                        "name": "project_id",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string",
                            "title": "Project Id"
                        }
                    },
                    {
                        "name": "max_docs",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "title": "Max Docs"
                        }
                    },
                    {
                        "name": "vector_store",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/components/schemas/VectorStores"
                                }
                            ],
                            "default": "in_memory",
                            "title": "Vector Store"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "title": "Response Build Index"
                                }
                            }
                        }
                    },
                    "404": {
                        "description": "Not found"
                    },
                    "422": {
                        "description": "Validation error"
                    }
                }
            }
        },
        ...
    },
    "components":
        "schemas": {
            ...
            "VectorStores": {
                "type": "string",
                "enum": [
                    "redis",
                    "in_memory"
                ],
                "title": "VectorStores"
            },
            ...
        }
    }
}

上传时,文档显示正确,但在尝试完成导入应用程序时,我收到以下错误:

Import failed
Please adjust your OpenAPI specification and try again. Reason: Invalid Request Body
/specification/body/paths/~1api~1v1~1rag~1prepare-index/post/parameters/2/schema
must NOT have additional properties
/specification/body/paths/~1api~1v1~1rag~1prepare-index/post/parameters/2/schema/allOf
schema cannot contain anyOf,oneOf,allOf
/specification/body/paths/~1api~1v1~1rag~1prepare-index/post/parameters/2/schema/oneOf
schema cannot contain anyOf,oneOf,allOf
/specification/body/paths/~1api~1v1~1rag~1prepare-index/post/parameters/2/schema
must match exactly one schema in oneOf
/specification/body/paths/~1api~1v1~1rag~1prepare-index/post/parameters/2
must have required property '$ref'
/specification/body/paths/~1api~1v1~1rag~1prepare-index/post/parameters/2
must NOT have additional properties
/specification/body/paths/~1api~1v1~1rag~1prepare-index/post/parameters/2
must match exactly one schema in oneOf

所以我尝试删除路径参数

allOf
的模式中的
vector_store
部分。现在
$ref
是该参数的
schema
属性的直接子级。但是,它仍然抛出相同的错误。此外,当删除
title
default
属性或在与
$ref
相同的级别提供
is_required
时,也没有帮助。此外,我尝试使用
swagger-editor
使其成为有效的 openapi.json,但不幸的是,这些都没有成功......

我在这里缺少什么?

fastapi openapi jsonschema swagger-editor
1个回答
0
投票

看起来错误来自第三个

parameter
定义和
allOf
的使用。该错误表明您不能拥有
anyOf
allOf
oneOf

我还想知道 FastAPI 是否正在寻找

requestBody
属性,因为它是一个
POST
操作。您没有定义一个,并且有一个与此相关的错误。

试试这个...

{
    "openapi": "3.0.3",
    "info": {
        "title": "SomeTitle",
        "description": "SomeDescription",
        "version": "1.2.0"
    },
    "servers": [
        {
            "url": "http://localhost:8080",
            "description": "Local Environment for testing"
        },
        {
            "url": "Productive URL",
            "description": "Productive Instance"
        }
    ],
    "paths": {
        "/api/v1/rag/prepare-index": {
            "post": {
                "tags": [
                    "Retrieval Augmented Generation"
                ],
                "summary": "Build Index",
                "operationId": "Build_Index",
                "security": [
                    {
                        "APIKeyHeader": []
                    }
                ],
                "parameters": [
                    {
                        "name": "project_id",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string",
                            "title": "Project Id"
                        }
                    },
                    {
                        "name": "max_docs",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "integer",
                            "title": "Max Docs"
                        }
                    },
                    {
                        "name": "vector_store",
                        "in": "query",
                        "required": false,
                        "schema": {
                            "$ref": "#/components/schemas/VectorStores"
                        }
                    }
                ],
                "requestBody": {
                    "description": "a request body",
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object"
                            }
                        }
                    },
                    "required": true
                },
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "title": "Response Build Index"
                                }
                            }
                        }
                    },
                    "404": {
                        "description": "Not found"
                    },
                    "422": {
                        "description": "Validation error"
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "VectorStores": {
                "type": "string",
                "enum": [
                    "redis",
                    "in_memory"
                ],
                "title": "VectorStores",
                "default": "in_memory"
            }
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.