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

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

Closed
monitaurus opened this issue May 28, 2024 · 4 comments
Labels

Comments

@monitaurus
Copy link
Contributor

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
Copy link
Contributor Author

monitaurus commented May 28, 2024

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

@soyuka
Copy link
Member

soyuka commented May 31, 2024

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

return $propertyMetadata->withSchema($propertySchema);

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

@monitaurus
Copy link
Contributor Author

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

Copy link

stale bot commented Jul 31, 2024

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 stale bot added the stale label Jul 31, 2024
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

2 participants