Skip to content

Call to a member function getContent() on array when generating openapi schema #7427

@fcharrier

Description

@fcharrier

API Platform version(s) affected: 4.2.1

Description
When I update my app with api-platform 4.2.1 (was 4.1.24 before), I can't generate openapi schema anymore (with bin/console api:openapi:export)

Possible Solution
The bug occurs in api-platform/openapi/Factory/OpenApiFactory.php line 508:

/**
 * @param array<Response> $existingResponses
 */
private function buildOpenApiResponse(array $existingResponses, int|string $status, string $description, Operation $openapiOperation, ?HttpOperation $operation = null, ?array $responseMimeTypes = null, ?array $operationOutputSchemas = null, ?ResourceMetadataCollection $resourceMetadataCollection = null): Operation
{
    $noOutput = \is_array($operation?->getOutput()) && null === $operation->getOutput()['class'];

    $response = $existingResponses[$status] ?? new Response($description);

    if (!$response->getContent() && $responseMimeTypes && $operationOutputSchemas && !$noOutput) {
        $response = $response->withContent($this->buildContent($responseMimeTypes, $operationOutputSchemas));
    }

=> With api-platform/openapi 4.2.1, $response is an array on my app. So $response->getContent() crashes

With api-platform/openapi 4.1.24, the code was different:

private function buildOpenApiResponse(array $existingResponses, int|string $status, string $description, ?Operation $openapiOperation = null, ?HttpOperation $operation = null, ?array $responseMimeTypes = null, ?array $operationOutputSchemas = null, ?ResourceMetadataCollection $resourceMetadataCollection = null): Operation
    {
        if (isset($existingResponses[$status])) {
            return $openapiOperation;
        }

in this case, if existingResponses is an array, it works properly.

Additional Context

bin/console api:openapi:export --output=var/openapi.json
18:54:35 CRITICAL  [console] Error thrown while running command "api:openapi:export --output='var/openapi.json'". Message: "Call to a member function getContent() on array" ["exception" => Error { …},"command" => "api:openapi:export --output='var/openapi.json'","message" => "Call to a member function getContent() on array"]

In OpenApiFactory.php line 508:
                                                   
  Call to a member function getContent() on array  
                                                   

api:openapi:export [-y|--yaml] [-o|--output OUTPUT] [--spec-version SPEC-VERSION] [--api-gateway] [--filter-tags FILTER-TAGS]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions