Skip to content

Commit

Permalink
fix(state): allow application/x-www-form-urlencoded content-type when…
Browse files Browse the repository at this point in the history
… no input
  • Loading branch information
alexndlm committed Nov 14, 2024
1 parent 17c916c commit 46e9bcb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/State/Provider/ContentNegotiationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ private function getInputFormat(HttpOperation $operation, Request $request): ?st
return $format;
}

$supportedMimeTypes = [];
foreach ($formats as $mimeTypes) {
foreach ($mimeTypes as $mimeType) {
$supportedMimeTypes[] = $mimeType;
if ($operation->canDeserialize() && !$request->isMethodSafe() && 'DELETE' !== $request->getMethod()) {
$supportedMimeTypes = [];
foreach ($formats as $mimeTypes) {
foreach ($mimeTypes as $mimeType) {
$supportedMimeTypes[] = $mimeType;
}
}
}

if (!$request->isMethodSafe() && 'DELETE' !== $request->getMethod()) {
throw new UnsupportedMediaTypeHttpException(\sprintf('The content-type "%s" is not supported. Supported MIME types are "%s".', $contentType, implode('", "', $supportedMimeTypes)));
}

Expand Down
36 changes: 36 additions & 0 deletions tests/State/Provider/ContentNegotiationProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,40 @@ public function testRequestWithEmptyContentType(): void

$this->assertSame($expectedResult, $result);
}

public function testRequestWithApplicationXWwwFormUrlencodedContentType(): void
{
$expectedResult = new \stdClass();

$decorated = $this->prophesize(ProviderInterface::class);
$decorated->provide(Argument::cetera())->willReturn($expectedResult);

$negotiator = new Negotiator();
$formats = ['jsonld' => ['application/ld+json']];
$errorFormats = ['jsonld' => ['application/ld+json']];

$provider = new ContentNegotiationProvider($decorated->reveal(), $negotiator, $formats, $errorFormats);

// in Symfony (at least up to 7.0.2, 6.4.2, 6.3.11, 5.4.34), a request
// without a content-type and content-length header will result in the
// variables set to an empty string, not null

$request = new Request(
server: [
'REQUEST_METHOD' => 'POST',
'REQUEST_URI' => '/',
'CONTENT_TYPE' => '',
'CONTENT_LENGTH' => '',
'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded',
],
content: ''
);

$operation = new Post();
$context = ['request' => $request];

$result = $provider->provide($operation, [], $context);

$this->assertSame($expectedResult, $result);
}
}

0 comments on commit 46e9bcb

Please sign in to comment.