From 5629be50177c3ad5677dd9bbb5662a90bfc1ad6d Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Thu, 16 Jul 2015 08:57:29 -0500 Subject: [PATCH] Allow dash-separated keys Per a number of reports, some modules were using dash-separated keys instead of underscore_separated keys for content-negotiation options. zf-content-negotiation was ignoring these previously, but starting with 1.2.0 and the introduction of the ContentNegotiationOptions class, these are now invalid and raise an exception, breaking applications. The specific module in question is likely zf-apigility-doctrine, and there's a PR against that repo to fix it: zfcampus/zf-apigility-doctrine#212. This patch modifies the options class to munge dash-separated values to ensure they work. --- src/ContentNegotiationOptions.php | 35 +++++++++++++++++++++ test/ContentNegotiationOptionsTest.php | 42 ++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 test/ContentNegotiationOptionsTest.php diff --git a/src/ContentNegotiationOptions.php b/src/ContentNegotiationOptions.php index 48394e8..d747496 100644 --- a/src/ContentNegotiationOptions.php +++ b/src/ContentNegotiationOptions.php @@ -30,6 +30,41 @@ class ContentNegotiationOptions extends AbstractOptions */ protected $contentTypeWhitelist = array(); + /** + * {@inheritDoc} + * + * Normalizes dash-separated keys to underscore-separated to ensure + * backwards compatibility with old options (even though dash-separated + * were previously ignored!). + * + * @see \Zend\Stdlib\ParameterObject::__set() + * @param string $key + * @param mixed $value + * @throws \Zend\Stdlib\Exception\BadMethodCallException + * @return void + */ + public function __set($key, $value) + { + parent::__set(str_replace('-', '_', $key), $value); + } + + /** + * {@inheritDoc} + * + * Normalizes dash-separated keys to underscore-separated to ensure + * backwards compatibility with old options (even though dash-separated + * were previously ignored!). + * + * @see \Zend\Stdlib\ParameterObject::__get() + * @param string $key + * @throws \Zend\Stdlib\Exception\BadMethodCallException + * @return mixed + */ + public function __get($key) + { + return parent::__get(str_replace('-', '_', $key)); + } + /** * @param array $controllers */ diff --git a/test/ContentNegotiationOptionsTest.php b/test/ContentNegotiationOptionsTest.php new file mode 100644 index 0000000..70fc1a5 --- /dev/null +++ b/test/ContentNegotiationOptionsTest.php @@ -0,0 +1,42 @@ + ['accept-whitelist', 'accept_whitelist'], + 'content-type-whitelist' => ['content-type-whitelist', 'content_type_whitelist'], + ]; + } + + /** + * @dataProvider dashSeparatedOptions + */ + public function testSetNormalizesDashSeparatedKeysToUnderscoreSeparated($key, $normalized) + { + $options = new ContentNegotiationOptions(); + $options->{$key} = ['value']; + $this->assertEquals(['value'], $options->{$key}); + $this->assertEquals(['value'], $options->{$normalized}); + } + + /** + * @dataProvider dashSeparatedOptions + */ + public function testConstructorAllowsDashSeparatedKeys($key, $normalized) + { + $options = new ContentNegotiationOptions([$key => ['value']]); + $this->assertEquals(['value'], $options->{$key}); + $this->assertEquals(['value'], $options->{$normalized}); + } +}