api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.38k stars 844 forks source link

OpenAPI on calculated field add an extra `items` fields even if `openApiContext` define an object #6391

Open monitaurus opened 4 weeks ago

monitaurus commented 4 weeks ago

API Platform version(s) affected: 3.2.22, 3.3.2

Tested on 3.2.22 and replicated on 3.3.2

Description
On a calculated field that returns an array, if we add a ApiProperty to change the type into object, the generated OpenAPI does generate a "type": "object" but there is also an extra "items": {"type": "string"}.

How to reproduce

On a resource, add the following calculated field:

    #[ApiProperty(
        openapiContext: [
            'type' => 'object',
            'properties' => [
                'alpha' => ['type' => 'integer'],
                'beta' => ['type' => 'string'],
            ],
        ]
    )]
    public function getCalculatedField(): array
    {
        return [
            'alpha' => 123,
            'beta' => 'hello',
        ];
    }

Then access OpenAPI via Swagger, or generate a openapi json to find the extra items.

swagger_screenshot

Possible Solution

None found at the moment.

Additional Context

Investigation on the factory code:

The type object seems to override the type array partially. And as builtin types are computed from PHP type (see doc), I don't think editing ApiProperty is enough to fix the issue.

monitaurus commented 4 weeks ago

On a personal note, if it's really issue, I can take time to try fix it, with some insights or guidance.

soyuka commented 3 weeks ago

IMO as you customized the schema it should return early at:

https://github.com/api-platform/core/blob/e867d07f59b82d5f1bdca69e096ddf452dd7efc8/src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php#L107

Definitely a bug, would love for this to get fixed, lmk if I can assist you (target 3.2 branch)

monitaurus commented 3 weeks ago

I'll take a look next week, many thanks for the details 🙇