Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/Request/RequestTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public function transform(Request $request)

protected function acceptJsonBody(Request $request)
{
if (str_starts_with($request->headers->get('Content-Type', ''), 'application/json')) {
$contentType = $request->headers->get('Content-Type', '');

if (
str_starts_with($contentType, 'application/json') ||
str_starts_with($contentType, 'application/merge-patch+json')
) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : []);
}
Expand Down
26 changes: 20 additions & 6 deletions tests/Request/RequestTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,27 @@ public function getSubject()
return new RequestTransformer($serializer);
}

public function testTransformChangesRequestParameters()
/**
* @dataProvider provideJsonMimeTypes
*/
public function testTransformChangesRequestParameters(string $contentType): void
{
$subject = $this->getSubject();
$content = ['Hello', 'World!'];
$request = $this->getRequest($content);
$request = $this->getRequest(content: $content, contentType: $contentType);

$subject->transform($request);

$this->assertEquals($content, iterator_to_array($request->request->getIterator()));
}

public function testTransformChangesRequestFormatDefault()
/**
* @dataProvider provideJsonMimeTypes
*/
public function testTransformChangesRequestFormatDefault(string $contentType)
{
$subject = $this->getSubject();
$request = $this->getRequest([]);
$request = $this->getRequest(content: [], contentType: $contentType);

$subject->transform($request);

Expand Down Expand Up @@ -65,11 +71,19 @@ public function testTransformChangesRequestFormatUnknown()
$this->assertEquals(Format::getDefault(), $request->getRequestFormat());
}

protected function getRequest($content)
protected function getRequest(mixed $content, string $contentType = 'application/json'): Request
{
$request = Request::create('/');
$request->initialize([], [], [], [], [], [], json_encode($content));
$request->headers->add(['Content-type' => 'application/json']);
$request->headers->add(['Content-type' => $contentType]);
return $request;
}

public static function provideJsonMimeTypes(): array
{
return [
'application/json' => ['application/json'],
'application/merge-patch+json' => ['application/merge-patch+json'],
];
}
}