OpenAPI PHP client giving Fatal Error with anyOf
Asked Answered
S

1

10

I'm trying to make an OpenAPI autogenerated PHP client using anyOf and allOf properties.

The goal is to be able to return an array with polymorphism in it: objects of different types.
Also those objects have a common base object as well.

In my example schema, Items is an array which items can be of types ItemOne or ItemTwo.
Both types of items have an own property (itemOneProperty and itemTwoProperty, respectively), and a common property baseItemProperty (which is inherited from BaseItem with the allOf keyword).

Here you have the API specification 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

This is the endpoint I'm sending the requests: https://api.myjson.com/bins/roxgd

And it returns this example json:

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

The PHP client is generated with no errors, but when it call the getItems method I get this 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

The same occurs if I use the oneOf property, but the error I get is: Uncaught Error: Class 'OneOfItemOneItemTwo' not found....


My setup works ok when I use any other valid yaml (without the polymorphism).

Also, I checked this related question already, but that is about UI, which I'm not using at all.

Do you know where could be the error? A mistake in my yaml doc? A bug in the PHP client generator?

Edit: I'm using openapi-generator v4.0.3 (latest release at this point).

Suite answered 1/8, 2019 at 16:22 Comment(4)
Is it feasible to share the generated code? Could you also verify the version of the openapi-generator and any flags or additional config used?Beguile
Did you verify the existence of the php class in the generated files? If the class exists it may be a PHP version issue, what version of PHP are you running the files with? If it does not it may be an issue with the generator.Malmsey
@Beguile Not necessary to share the code anymore. I'm using version 4.0.3, where inheritance is broken at the moment. Thanks for the help.Suite
@DempseyvanWissen The missing class doesn't even exist in the project. This is related with the issue I posted in the answer. Thanks for the help.Suite
S
3

After more researching I found there's an open issue with the inheritance in the openapi-generator from version 4.0.0 onwards.

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

Suite answered 10/8, 2019 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.