From 649b67924023050b191abc0d0cf0df12d3327bc7 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 15 Nov 2017 10:36:55 -0600 Subject: [PATCH 1/2] Fixes uninitialized string offset error As reported in #96, since #94 and the 1.3.1 release, if the body content for a PUT, PATCH, or DELETE call is empty, an "Uninitialized string offset: 0" notice is raised. This patch adds tests missing from the original patch created for #94, verifying that JSON deserialization occurs for those HTTP methods if the content looks like JSON but no Content-Type header was present. The new cases also include scenarios where no content is present. A fix is introduced to the `ContentTypeListener` to ensure the notice is no longer raised. --- src/ContentTypeListener.php | 2 +- test/ContentTypeListenerTest.php | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/ContentTypeListener.php b/src/ContentTypeListener.php index 058a96d..88418db 100644 --- a/src/ContentTypeListener.php +++ b/src/ContentTypeListener.php @@ -102,7 +102,7 @@ public function __invoke(MvcEvent $e) } // Try to assume JSON if content starts like JSON and no explicit Content-Type was provided - if (! $bodyParams && in_array($content[0], ['{', '['], true)) { + if (! $bodyParams && strlen($content) > 0 && in_array($content[0], ['{', '['], true)) { $bodyParams = $this->decodeJson($content); } diff --git a/test/ContentTypeListenerTest.php b/test/ContentTypeListenerTest.php index 353d7f1..6d59d1d 100644 --- a/test/ContentTypeListenerTest.php +++ b/test/ContentTypeListenerTest.php @@ -647,4 +647,64 @@ public function testStringContentIsParsedCorrectlyToAnArray($method, $data, $key $this->assertArrayHasKey($key, $array); $this->assertSame('', $array[$key]); } + + public function nonPostMethodsContent() + { + $dataSets = [ + 'object' => [ + 'data' => '{"key": "value"}', + 'expected' => ['key' => 'value'], + ], + 'array' => [ + 'data' => '["first", "second"]', + 'expected' => ['first', 'second'], + ], + /** @see https://github.com/zfcampus/zf-content-negotiation/pull/96 */ + 'empty' => [ + 'data' => '', + 'expected' => [], + ], + ]; + + foreach (['PUT', 'PATCH', 'DELETE'] as $method) { + foreach ($dataSets as $type => $set) { + $name = sprintf('%s-%s', $type, $method); + yield $name => [$method, $set['data'], $set['expected']]; + } + } + } + + /** + * @dataProvider nonPostMethodsContent + * + * @see https://github.com/zfcampus/zf-content-negotiation/pull/94 + * @see https://github.com/zfcampus/zf-content-negotiation/pull/96 + * @param string $method HTTP method + * @param string $data HTTP body content + * @param mixed $expected Expected body params + */ + public function testMissingContentTypeHeaderResultsInParsingAsJsonIfInitialCharacterIndicatesObjectOrArray( + $method, + $data, + $expected + ) { + $listener = $this->listener; + + $request = new Request(); + $request->setMethod($method); + $request->setContent($data); + + $event = new MvcEvent(); + $event->setRequest($request); + $event->setRouteMatch($this->createRouteMatch([])); + + $result = $listener($event); + $this->assertNull($result); + + /** @var \ZF\ContentNegotiation\ParameterDataContainer $params */ + $params = $event->getParam('ZFContentNegotiationParameterData'); + $test = $params->getBodyParams(); + + $this->assertSame($expected, $test); + } } From 8f3cccfb87687d1001b667a56cc6f2bad54a0a68 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 15 Nov 2017 10:56:41 -0600 Subject: [PATCH 2/2] Adds CHANGELOG for #97 --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67c09be..5797483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 1.3.2 - TBD +## 1.3.2 - 2017-11-15 ### Added @@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#97](https://github.com/zfcampus/zf-content-negotiation/pull/97) fixes an + issue in the `ContentTypeListener` whereby empty content was leading to an + uninitialized string offset notice . ## 1.3.1 - 2017-11-14