gpt4 book ai didi

php - OpenAPI PHP 客户端给出 Fatal Error with anyOf

转载 作者:可可西里 更新时间:2023-10-31 22:50:38 26 4
gpt4 key购买 nike

我正在尝试使用 anyOfallOf 属性制作一个 OpenAPI 自动生成的 PHP 客户端。

目标是能够返回一个包含多态性的数组:不同类型的对象。
这些对象也有一个共同的基础对象。

在我的示例架构中,Items 是一个数组,其中的项目可以是 ItemOneItemTwo 类型。
两种类型的项目都有一个自己的属性(分别为 itemOnePropertyitemTwoProperty)和一个公共(public)属性 baseItemProperty(继承自 带有 allOf 关键字的 BaseItem

这里有 API 规范 yaml:

openapi: 3.0.0
info:
title: Test API
version: 1.0.0
servers:
- url: https://api.myjson.com/bins

paths:
/roxgd:
get:
operationId: getItems
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/Items'
components:
schemas:

Items:
type: array
items:
anyOf:
- $ref: '#/components/schemas/ItemOne'
- $ref: '#/components/schemas/ItemTwo'

BaseItem:
type: object
properties:
baseItemProperty:
type: string

ItemOne:
allOf:
- $ref: '#/components/schemas/BaseItem'
- type: object
properties:
itemOneProperty:
type: string

ItemTwo:
allOf:
- $ref: '#/components/schemas/BaseItem'
- type: object
properties:
itemTwoProperty:
type: string

这是我发送请求的端点:https://api.myjson.com/bins/roxgd

它返回这个例子 json:

[
{
type: "ItemOne",
baseItemProperty: "foo1",
itemOneProperty: "bar1"
},
{
type: "ItemTwo",
baseItemProperty: "foo2",
itemTwoProperty: "bar2"
}
]

PHP 客户端生成时没有错误,但是当它调用 getItems 方法时,我得到了这个 fatal error :

PHP Fatal error:  Uncaught Error: Class 'AnyOfItemOneItemTwo' not found in /home/user/projects/openapi-test/lib/ObjectSerializer.php:309
Stack trace:
#0 /home/user/projects/openapi-test/lib/ObjectSerializer.php(261): MyRepo\OpenApiTest\ObjectSerializer::deserialize(Object(stdClass), 'AnyOfItemOneIte...', NULL)
#1 /home/user/projects/openapi-test/lib/Api/DefaultApi.php(182): MyRepo\OpenApiTest\ObjectSerializer::deserialize(Array, 'AnyOfItemOneIte...', Array)
#2 /home/user/projects/openapi-test/lib/Api/DefaultApi.php(128): MyRepo\OpenApiTest\Api\DefaultApi->getItemsWithHttpInfo()
#3 /home/user/projects/tests-for-openapi-test/test.php(10): MyRepo\OpenApiTest\Api\DefaultApi->getItems()
#4 {main}
thrown in /home/user/projects/openapi-test/lib/ObjectSerializer.php on line 309

如果我使用 oneOf 属性,也会发生同样的情况,但我得到的错误是:Uncaught Error: Class 'OneOfItemOneItemTwo' not found...


当我使用任何其他有效的 yaml(没有多态性)时,我的设置工作正常。

此外,我检查了 this related question已经,但那是关于 UI 的,我根本没有使用它。

你知道错误在哪里吗?我的 yaml 文档中有错误吗? PHP 客户端生成器中的错误?

编辑:我使用的是 openapi-generator v4.0.3(此时的最新版本)。

最佳答案

经过更多研究后,我发现从 4.0.0 版开始,openapi-generator 中的继承存在一个 Unresolved 问题。

https://github.com/OpenAPITools/openapi-generator/issues/2845

关于php - OpenAPI PHP 客户端给出 Fatal Error with anyOf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57313269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com