NelmioApiDocBundle
NelmioApiDocBundle copied to clipboard
[Bug]: Type required for oneOf defined property
Version
4.26.1
Description
Consider the following definition:
<?php
namespace App\Model\EApi\Equipment;
use JMS\Serializer\Annotation\Type;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;
readonly class EquipmentReservation
{
private function __construct(
#[Property(description: 'The parent reservation, if any. Will be false to break loops.',
nullable: true,
oneOf: [
new Schema(ref: new Model(type: self::class)),
new Schema(type: 'false'),
]
)]
public self|false|null $parent)
{
}
}
With this definition, the property is excluded from the OpenAPI documentation, which is due to the following branch:
https://github.com/nelmio/NelmioApiDocBundle/blob/2d45e536b0005bdc7607d71716ec453d11677cbf/src/ModelDescriber/JMSModelDescriber.php#L195
I can add a type: object to the Property attribute to force the property to be added, but that includes the type field which should not be there:
"parent": {
"description": "The parent reservation, if any. Will be false to break loops.",
"type": "object",
"nullable": true,
"oneOf": [
{
"$ref": "#/components/schemas/EquipmentReservation"
},
{
"type": "false"
}
]
},
I can also add a #[JMS\Type('object')] (or one with the class name) attribute to the property itself, but that ignores the oneOf specification I manually supplied:
"parent": {
"description": "The parent reservation, if any. Will be false to break loops.",
"nullable": true,
"oneOf": [
{
"$ref": "#/components/schemas/object"
}
]
},
Note that this is a serialization object only, so there is no need to deserialize: which means I do not necessarily want to define the JMS type, without it it should work just fine.
I tried adding an exclusion to the referenced implementation, but that would only throw further in the code due to the type being null.
Is there anything that can be done here, or do I want something that is too complex? 😄
Additional context
No response
Note: I solved the issue for now by changing the models themselves (by excluding the parent and children properties with JMS groups instead of setting them to false). While this might no longer be relevant for me, it might still be worth looking into this.