Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Type required for oneOf defined property #2284

Open
bobvandevijver opened this issue May 9, 2024 · 1 comment
Open

[Bug]: Type required for oneOf defined property #2284

bobvandevijver opened this issue May 9, 2024 · 1 comment
Labels

Comments

@bobvandevijver
Copy link

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:

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

@bobvandevijver
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant