Skip to content

Miss an easy way to override a response description without changing the content #7405

@VincentLanglet

Description

@VincentLanglet

Description
With the Operation override

openapi: new Operation(
     description: 'Foo',
     summary: 'Foo',
     requestBody: new RequestBody('Bar'),
     responses: [
         200 => new Response('Bar'),
     ]
),

I'm getting
Image

Which means

  • I can override the description and the summary of an operation and still having the OpenApiFactory filling the requestBody/responses
  • I can override the description of the request body and still having the OpenApiFactory filling the content of the requestBody
  • I can override a response code 200 and still having other responses code in the api doc
  • As soon as I override a response code description I'm loosing the content

This is because of this check https://github.com/api-platform/core/blame/510fa5523802f9c173d852c218317f95f9ef2d90/src/OpenApi/Factory/OpenApiFactory.php#L503-L505

While a null check on the content is done for the request body

if (
\in_array($method, ['PATCH', 'PUT', 'POST'], true)
&& !(false === ($input = $operation->getInput()) || (\is_array($input) && null === $input['class']))
) {
$content = $openapiOperation->getRequestBody()?->getContent();
if (null === $content) {
$operationInputSchemas = [];
foreach ($requestMimeTypes as $operationFormat) {
$operationInputSchema = null;
if (str_starts_with($operationFormat, 'json')) {
$operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operation, $schema, null, $forceSchemaCollection);
$this->appendSchemaDefinitions($schemas, $operationInputSchema->getDefinitions());
}
$operationInputSchemas[$operationFormat] = $operationInputSchema;
}
$content = $this->buildContent($requestMimeTypes, $operationInputSchemas);
}
$openapiOperation = $openapiOperation->withRequestBody(new RequestBody(
description: $openapiOperation->getRequestBody()?->getDescription() ?? \sprintf('The %s %s resource', 'POST' === $method ? 'new' : 'updated', $resourceShortName),
content: $content,
required: $openapiOperation->getRequestBody()?->getRequired() ?? true,
));
}

since the PR #6374

Would you be open to a similar behavior @soyuka for Response ?

Example
Code should be changed to something like

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
    {
        $noOutput = \is_array($operation?->getOutput()) && null === $operation->getOutput()['class'];

        $response = $existingResponses[$status] ?? null;
        $responseContent = $response?->getContent();
        if (null === $responseContent && $responseMimeTypes && $operationOutputSchemas && !$noOutput) {
            $responseContent = $this->buildContent($responseMimeTypes, $operationOutputSchemas);
        }

        $responseLinks = $response?->getLinks();
        if (null === $responseLinks && $resourceMetadataCollection && $operation) {
            $responseLinks = $this->getLinks($resourceMetadataCollection, $operation);
        }

        return $openapiOperation->withResponse($status, new Response($description, $responseContent, $response?->getHeaders(), $responseLinks));
    }

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