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

Array of union types does not generate anyOf field #2142

Closed
DjordyKoert opened this issue Nov 2, 2023 · 2 comments
Closed

Array of union types does not generate anyOf field #2142

DjordyKoert opened this issue Nov 2, 2023 · 2 comments

Comments

@DjordyKoert
Copy link
Collaborator

Example

Given a DTO with an union typed array property:

class ExampleDTO
{
    /**
     * @param (int|string)[] $exampleList
     */
    public function __construct(
        private array $exampleList
    ) {
    }

    /**
     * @return (int|string)[]
     */
    public function getExampleList(): array
    {
        return $this->exampleList;
    }
}

Result in Nelmio generating the following OpenApi schema for the DTO:

"ExampleDTO": {
    "properties": {
        "exampleList": {
            "type": "array",
            "items": {
                "type": "integer"
            }
        }
    },
    "type": "object"
},

Expected result

"ExampleDTO": {
    "properties": {
        "exampleList": {
            "type": "array",
            "items": {
                "anyOf": [
                    {
                        "type": "integer"
                    },
                    {
                        "type": "string"
                    }
                ]
            }
        }
    },
    "type": "object"
},

This can be fixed manually by describing the exampleList property like so:

use OpenApi\Attributes as OA;

class ExampleDTO
{
    /**
     * @param (int|string)[] $exampleList
     */
    public function __construct(
        #[OA\Property(
            type: 'array',
            items: new OA\Items(
                anyOf: [
                    new OA\Schema(type: 'integer'),
                    new OA\Schema(type: 'string'),
            ])
        )]
        private array $exampleList
    ) {
    }

    /**
     * @return (int|string)[]
     */
    public function getExampleList(): array
    {
        return $this->exampleList;
    }
}

Ideally NelmioApiDocBundle should generate a correct schema instead of having to manually define it.

@DerManoMann
Copy link
Contributor

Sounds more like a feature request for the underlying swagger-php library.

There is a long outstanding issue to improve type handling that should cover this.
zircote/swagger-php#1310

@DjordyKoert
Copy link
Collaborator Author

Ah thanks for answering.

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

No branches or pull requests

2 participants