OpenAPI PHP客户端使用anyOf给出致命错误

时间:2019-08-01 16:22:39

标签: php polymorphism swagger openapi openapi-generator

我正在尝试使用anyOfallOf属性创建OpenAPI自动生成的PHP客户端。

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

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

这里有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方法时,会出现此致命错误:

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(目前最新版本)。

1 个答案:

答案 0 :(得分:3)

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

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

相关问题