Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/State/Util/HttpResponseHeadersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,26 @@ trait HttpResponseHeadersTrait
private function getHeaders(Request $request, HttpOperation $operation, array $context): array
{
$status = $this->getStatus($request, $operation, $context);
$method = $request->getMethod();
$output = $operation->getOutput();
$outputMetadata = $output ?? ['class' => $operation->getClass()];
$hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
$outputExplicitlyDisabled = \is_array($output) && \array_key_exists('class', $output) && null === $output['class'];
// RFC 7230 §3.3.2 / §3.3.3: 204, 205 and 304 responses MUST NOT include a payload body,
// and a sender MUST NOT generate a Content-Type field for a message without a body.
$isBodylessStatus = \in_array($status, [Response::HTTP_NO_CONTENT, Response::HTTP_RESET_CONTENT, Response::HTTP_NOT_MODIFIED], true);
$hasBody = !$outputExplicitlyDisabled && !$isBodylessStatus;

$headers = [
'Content-Type' => \sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
'Vary' => 'Accept',
'X-Content-Type-Options' => 'nosniff',
'X-Frame-Options' => 'deny',
];

if ($hasBody) {
$headers['Content-Type'] = \sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat()));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but the charset attribute unconditionally may not be a good idea. For instance JSON, doesn't have a charset attribute and including it is not valid, and can break some strict clients.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this is to avoid breaking tests we had that before. Lets investigate this in another issue.

}

$exception = $request->attributes->get('exception');
if (($exception instanceof HttpExceptionInterface || $exception instanceof SymfonyHttpExceptionInterface) && $exceptionHeaders = $exception->getHeaders()) {
$headers = array_merge($headers, $exceptionHeaders);
Expand All @@ -76,10 +89,7 @@ private function getHeaders(Request $request, HttpOperation $operation, array $c
$headers['Accept-Patch'] = $acceptPatch;
}

$method = $request->getMethod();
$originalData = $context['original_data'] ?? null;
$outputMetadata = $operation->getOutput() ?? ['class' => $operation->getClass()];
$hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
$hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));

if ($hasData) {
Expand Down
76 changes: 76 additions & 0 deletions tests/State/RespondProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Tests\State;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
Expand Down Expand Up @@ -162,6 +163,81 @@ public function testAddsLinkedDataPlatformHeaders(): void
$this->assertSame('application/ld+json', $response->headers->get('Accept-Post'));
}

public function testDoesNotSetContentTypeWhenOutputIsFalse(): void
{
$operation = new Post(class: Employee::class, output: ['class' => null], status: 204);

$resourceClassResolver = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolver->isResourceClass(Employee::class)->willReturn(true);

$respondProcessor = new RespondProcessor(null, $resourceClassResolver->reveal());

$req = new Request();
$req->setMethod('POST');
$response = $respondProcessor->process(null, $operation, context: [
'request' => $req,
'original_data' => new Employee(),
]);

$this->assertSame(204, $response->getStatusCode());
$this->assertFalse($response->headers->has('Content-Type'));
}

public function testDoesNotSetContentTypeWhenOutputIsFalseWithCreatedStatus(): void
{
$operation = new Post(class: Employee::class, output: ['class' => null], status: 201);

$resourceClassResolver = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolver->isResourceClass(Employee::class)->willReturn(true);

$respondProcessor = new RespondProcessor(null, $resourceClassResolver->reveal());

$req = new Request();
$req->setMethod('POST');
$response = $respondProcessor->process(null, $operation, context: [
'request' => $req,
'original_data' => new Employee(),
]);

$this->assertSame(201, $response->getStatusCode());
$this->assertFalse($response->headers->has('Content-Type'));
}

public function testDoesNotSetContentTypeOnBodylessStatusCodes(): void
{
$operation = new Delete(class: Employee::class);

$resourceClassResolver = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolver->isResourceClass(Employee::class)->willReturn(true);

$respondProcessor = new RespondProcessor(null, $resourceClassResolver->reveal());

$req = new Request();
$req->setMethod('DELETE');
$response = $respondProcessor->process(null, $operation, context: [
'request' => $req,
]);

$this->assertSame(204, $response->getStatusCode());
$this->assertFalse($response->headers->has('Content-Type'));
}

public function testKeepsContentTypeForOperationWithoutOutputAndClass(): void
{
$operation = new Get(outputFormats: ['jsonld' => ['application/ld+json']], serialize: false, read: true);

$respondProcessor = new RespondProcessor();

$req = new Request();
$req->setRequestFormat('jsonld');
$response = $respondProcessor->process('{"@context":{}}', $operation, context: [
'request' => $req,
]);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('application/ld+json; charset=utf-8', $response->headers->get('Content-Type'));
}

public function testDoesNotAddLinkedDataPlatformHeadersWithoutFactory(): void
{
$operation = new Get(uriTemplate: '/employees/{id}', class: Employee::class);
Expand Down
Loading