diff --git a/src/Request/RequestTransformer.php b/src/Request/RequestTransformer.php index 1a7e770..030de39 100644 --- a/src/Request/RequestTransformer.php +++ b/src/Request/RequestTransformer.php @@ -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 : []); } diff --git a/tests/Request/RequestTransformerTest.php b/tests/Request/RequestTransformerTest.php index 8750e8e..164a78f 100644 --- a/tests/Request/RequestTransformerTest.php +++ b/tests/Request/RequestTransformerTest.php @@ -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); @@ -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'], + ]; + } }