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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With