core icon indicating copy to clipboard operation
core copied to clipboard

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

Open monitaurus opened this issue 1 year ago • 3 comments

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 process revolve around SchemaPropertyMetadataFactory::create
  • the calculated field returns an array, so $propertyMetadata->getBuiltinTypes() returns ["array"] (view file)
  • array is considered as a collection of items, and the code define items as string by default (view file)
  • and finally $this->getType returns a property type as array with items as string (view file)

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 avatar May 28 '24 08:05 monitaurus

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

monitaurus avatar May 28 '24 08:05 monitaurus

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)

soyuka avatar May 31 '24 08:05 soyuka

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

monitaurus avatar May 31 '24 14:05 monitaurus

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 31 '24 04:07 stale[bot]