core
core copied to clipboard
OpenAPI on calculated field add an extra `items` fields even if `openApiContext` define an object
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.
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) arrayis considered as a collection of items, and the code define items asstringby default (view file)- and finally
$this->getTypereturns a property type asarraywithitemsasstring(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.
On a personal note, if it's really issue, I can take time to try fix it, with some insights or guidance.
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)
I'll take a look next week, many thanks for the details 🙇
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.