From f800cd6b6dc0e8dd678a57446a0f6d05c1daf82d Mon Sep 17 00:00:00 2001 From: healerz Date: Mon, 11 Dec 2017 14:36:10 +0100 Subject: [PATCH 1/2] feat: enhance allowed additional fields feature --- composer.json | 2 +- src/Denormalizer/Denormalizer.php | 6 ++++-- src/Denormalizer/DenormalizerContext.php | 12 ++++++------ src/Denormalizer/DenormalizerContextBuilder.php | 8 ++++---- .../DenormalizerContextBuilderInterface.php | 4 ++-- src/Denormalizer/DenormalizerContextInterface.php | 4 ++-- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/composer.json b/composer.json index a51b031..ad64dba 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "php": "~7.0", + "php": "~7.1", "psr/http-message": "~1.0", "psr/log": "~1.0" }, diff --git a/src/Denormalizer/Denormalizer.php b/src/Denormalizer/Denormalizer.php index 4d1a6ff..a717946 100644 --- a/src/Denormalizer/Denormalizer.php +++ b/src/Denormalizer/Denormalizer.php @@ -70,8 +70,10 @@ public function denormalize($object, array $data, DenormalizerContextInterface $ unset($data[$denormalizationFieldMapping->getName()]); } - if ([] !== $data && !$context->isAllowedAdditionalFields()) { - $this->handleNotAllowedAdditionalFields($path, array_keys($data)); + if (null !== $context->getAllowedAdditionalFields() + && [] !== $fields = array_diff(array_keys($data), $context->getAllowedAdditionalFields()) + ) { + $this->handleNotAllowedAdditionalFields($path, $fields); } return $object; diff --git a/src/Denormalizer/DenormalizerContext.php b/src/Denormalizer/DenormalizerContext.php index 6949d40..9ec090d 100644 --- a/src/Denormalizer/DenormalizerContext.php +++ b/src/Denormalizer/DenormalizerContext.php @@ -9,9 +9,9 @@ final class DenormalizerContext implements DenormalizerContextInterface { /** - * @var bool + * @var array|null */ - private $allowedAdditionalFields = false; + private $allowedAdditionalFields; /** * @var string[] @@ -24,12 +24,12 @@ final class DenormalizerContext implements DenormalizerContextInterface private $request; /** - * @param bool $allowedAdditionalFields + * @param array|null $allowedAdditionalFields * @param string[] $groups * @param ServerRequestInterface|null $request */ public function __construct( - bool $allowedAdditionalFields = false, + ?array $allowedAdditionalFields = [], array $groups = [], ServerRequestInterface $request = null ) { @@ -39,9 +39,9 @@ public function __construct( } /** - * @return bool + * @return array|null */ - public function isAllowedAdditionalFields(): bool + public function getAllowedAdditionalFields(): ?array { return $this->allowedAdditionalFields; } diff --git a/src/Denormalizer/DenormalizerContextBuilder.php b/src/Denormalizer/DenormalizerContextBuilder.php index 0595918..b6a216c 100644 --- a/src/Denormalizer/DenormalizerContextBuilder.php +++ b/src/Denormalizer/DenormalizerContextBuilder.php @@ -9,7 +9,7 @@ final class DenormalizerContextBuilder implements DenormalizerContextBuilderInterface { /** - * @var bool + * @var array|null */ private $allowedAdditionalFields; @@ -33,18 +33,18 @@ private function __construct() public static function create(): DenormalizerContextBuilderInterface { $self = new self(); - $self->allowedAdditionalFields = false; + $self->allowedAdditionalFields = []; $self->groups = []; return $self; } /** - * @param bool $allowedAdditionalFields + * @param array|null $allowedAdditionalFields * * @return DenormalizerContextBuilderInterface */ - public function setAllowedAdditionalFields(bool $allowedAdditionalFields): DenormalizerContextBuilderInterface + public function setAllowedAdditionalFields(?array $allowedAdditionalFields): DenormalizerContextBuilderInterface { $this->allowedAdditionalFields = $allowedAdditionalFields; diff --git a/src/Denormalizer/DenormalizerContextBuilderInterface.php b/src/Denormalizer/DenormalizerContextBuilderInterface.php index 73fcdb4..20ea303 100644 --- a/src/Denormalizer/DenormalizerContextBuilderInterface.php +++ b/src/Denormalizer/DenormalizerContextBuilderInterface.php @@ -14,11 +14,11 @@ interface DenormalizerContextBuilderInterface public static function create(): DenormalizerContextBuilderInterface; /** - * @param bool $allowedAdditionalFields + * @param array|null $allowedAdditionalFields * * @return self */ - public function setAllowedAdditionalFields(bool $allowedAdditionalFields): self; + public function setAllowedAdditionalFields(?array $allowedAdditionalFields): self; /** * @param string[] $groups diff --git a/src/Denormalizer/DenormalizerContextInterface.php b/src/Denormalizer/DenormalizerContextInterface.php index 1efdfa9..de0259d 100644 --- a/src/Denormalizer/DenormalizerContextInterface.php +++ b/src/Denormalizer/DenormalizerContextInterface.php @@ -9,9 +9,9 @@ interface DenormalizerContextInterface { /** - * @return bool + * @return array|null */ - public function isAllowedAdditionalFields(): bool; + public function getAllowedAdditionalFields(): ?array; /** * @return string[] From 15b32680aa46c4a0610aa5c039fcbb9daca7607e Mon Sep 17 00:00:00 2001 From: healerz Date: Mon, 11 Dec 2017 15:35:04 +0100 Subject: [PATCH 2/2] feat: enhance allowed additional fields feature. Reverse default behaviour of allow additional fields --- composer.json | 2 +- doc/Denormalizer/DenormalizerContext.md | 6 +++--- .../DenormalizerContextBuilder.md | 6 +++--- src/Denormalizer/DenormalizerContext.php | 4 ++-- .../DenormalizerContextBuilder.php | 7 ++++--- .../DenormalizerContextBuilderInterface.php | 2 +- .../DenormalizerContextInterface.php | 2 +- .../DenormalizerContextBuilderTest.php | 6 +++--- .../Denormalizer/DenormalizerContextTest.php | 6 +++--- tests/Denormalizer/DenormalizerTest.php | 19 +++++++++++-------- tests/DeserializerIntegrationTest.php | 10 +++++++--- 11 files changed, 39 insertions(+), 31 deletions(-) diff --git a/composer.json b/composer.json index ad64dba..a51b031 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "php": "~7.1", + "php": "~7.0", "psr/http-message": "~1.0", "psr/log": "~1.0" }, diff --git a/doc/Denormalizer/DenormalizerContext.md b/doc/Denormalizer/DenormalizerContext.md index 5296413..bf7c81c 100644 --- a/doc/Denormalizer/DenormalizerContext.md +++ b/doc/Denormalizer/DenormalizerContext.md @@ -8,10 +8,10 @@ use Psr\Http\Message\ServerRequestInterface; $request = ...; -$context = new DenormalizerContext(true, ['group1'], $request); +$context = new DenormalizerContext(['allowed_additional_field'], ['group1'], $request); -echo $context->isAllowedAdditionalFields(); -// true +echo $context->getAllowedAdditionalFields(); +// ['allowed_additional_field'] print_r($context->getGroups()); // ['group1'] diff --git a/doc/Denormalizer/DenormalizerContextBuilder.md b/doc/Denormalizer/DenormalizerContextBuilder.md index 738e649..efeec81 100644 --- a/doc/Denormalizer/DenormalizerContextBuilder.md +++ b/doc/Denormalizer/DenormalizerContextBuilder.md @@ -9,13 +9,13 @@ use Psr\Http\Message\ServerRequestInterface; $request = ...; $context = DenormalizerContextBuilder::create() - ->setAllowedAdditionalFields(true) + ->setAllowedAdditionalFields(['allowed_additional_field']) ->setGroups(['group1']) ->setRequest($request) ->getContext(); -echo $context->isAllowedAdditionalFields(); -// true +echo $context->getAllowedAdditionalFields(); +// ['allowed_additional_field'] print_r($context->getGroups()); // ['group1'] diff --git a/src/Denormalizer/DenormalizerContext.php b/src/Denormalizer/DenormalizerContext.php index 9ec090d..eab4484 100644 --- a/src/Denormalizer/DenormalizerContext.php +++ b/src/Denormalizer/DenormalizerContext.php @@ -29,7 +29,7 @@ final class DenormalizerContext implements DenormalizerContextInterface * @param ServerRequestInterface|null $request */ public function __construct( - ?array $allowedAdditionalFields = [], + array $allowedAdditionalFields = null, array $groups = [], ServerRequestInterface $request = null ) { @@ -41,7 +41,7 @@ public function __construct( /** * @return array|null */ - public function getAllowedAdditionalFields(): ?array + public function getAllowedAdditionalFields() { return $this->allowedAdditionalFields; } diff --git a/src/Denormalizer/DenormalizerContextBuilder.php b/src/Denormalizer/DenormalizerContextBuilder.php index b6a216c..f73990a 100644 --- a/src/Denormalizer/DenormalizerContextBuilder.php +++ b/src/Denormalizer/DenormalizerContextBuilder.php @@ -33,7 +33,7 @@ private function __construct() public static function create(): DenormalizerContextBuilderInterface { $self = new self(); - $self->allowedAdditionalFields = []; + $self->allowedAdditionalFields = null; $self->groups = []; return $self; @@ -44,8 +44,9 @@ public static function create(): DenormalizerContextBuilderInterface * * @return DenormalizerContextBuilderInterface */ - public function setAllowedAdditionalFields(?array $allowedAdditionalFields): DenormalizerContextBuilderInterface - { + public function setAllowedAdditionalFields( + array $allowedAdditionalFields = null + ): DenormalizerContextBuilderInterface { $this->allowedAdditionalFields = $allowedAdditionalFields; return $this; diff --git a/src/Denormalizer/DenormalizerContextBuilderInterface.php b/src/Denormalizer/DenormalizerContextBuilderInterface.php index 20ea303..433f07a 100644 --- a/src/Denormalizer/DenormalizerContextBuilderInterface.php +++ b/src/Denormalizer/DenormalizerContextBuilderInterface.php @@ -18,7 +18,7 @@ public static function create(): DenormalizerContextBuilderInterface; * * @return self */ - public function setAllowedAdditionalFields(?array $allowedAdditionalFields): self; + public function setAllowedAdditionalFields(array $allowedAdditionalFields = null): self; /** * @param string[] $groups diff --git a/src/Denormalizer/DenormalizerContextInterface.php b/src/Denormalizer/DenormalizerContextInterface.php index de0259d..221dc39 100644 --- a/src/Denormalizer/DenormalizerContextInterface.php +++ b/src/Denormalizer/DenormalizerContextInterface.php @@ -11,7 +11,7 @@ interface DenormalizerContextInterface /** * @return array|null */ - public function getAllowedAdditionalFields(): ?array; + public function getAllowedAdditionalFields(); /** * @return string[] diff --git a/tests/Denormalizer/DenormalizerContextBuilderTest.php b/tests/Denormalizer/DenormalizerContextBuilderTest.php index a7fb8ad..5a27721 100644 --- a/tests/Denormalizer/DenormalizerContextBuilderTest.php +++ b/tests/Denormalizer/DenormalizerContextBuilderTest.php @@ -20,7 +20,7 @@ public function testCreate() self::assertInstanceOf(DenormalizerContextInterface::class, $context); - self::assertSame(false, $context->isAllowedAdditionalFields()); + self::assertSame(null, $context->getAllowedAdditionalFields()); self::assertSame([], $context->getGroups()); self::assertNull($context->getRequest()); } @@ -30,14 +30,14 @@ public function testCreateWithOverridenSettings() $request = $this->getRequest(); $context = DenormalizerContextBuilder::create() - ->setAllowedAdditionalFields(true) + ->setAllowedAdditionalFields(['allowed_field']) ->setGroups(['group1']) ->setRequest($request) ->getContext(); self::assertInstanceOf(DenormalizerContextInterface::class, $context); - self::assertSame(true, $context->isAllowedAdditionalFields()); + self::assertSame(['allowed_field'], $context->getAllowedAdditionalFields()); self::assertSame(['group1'], $context->getGroups()); self::assertSame($request, $context->getRequest()); } diff --git a/tests/Denormalizer/DenormalizerContextTest.php b/tests/Denormalizer/DenormalizerContextTest.php index 3bc7a1d..2f92f0a 100644 --- a/tests/Denormalizer/DenormalizerContextTest.php +++ b/tests/Denormalizer/DenormalizerContextTest.php @@ -17,7 +17,7 @@ public function testCreate() { $context = new DenormalizerContext(); - self::assertSame(false, $context->isAllowedAdditionalFields()); + self::assertSame(null, $context->getAllowedAdditionalFields()); self::assertSame([], $context->getGroups()); self::assertNull($context->getRequest()); } @@ -26,9 +26,9 @@ public function testCreateWithOverridenSettings() { $request = $this->getRequest(); - $context = new DenormalizerContext(true, ['group1'], $request); + $context = new DenormalizerContext(['allowed_field'], ['group1'], $request); - self::assertSame(true, $context->isAllowedAdditionalFields()); + self::assertSame(['allowed_field'], $context->getAllowedAdditionalFields()); self::assertSame(['group1'], $context->getGroups()); self::assertSame($request, $context->getRequest()); } diff --git a/tests/Denormalizer/DenormalizerTest.php b/tests/Denormalizer/DenormalizerTest.php index 8749fe7..992aef1 100644 --- a/tests/Denormalizer/DenormalizerTest.php +++ b/tests/Denormalizer/DenormalizerTest.php @@ -62,7 +62,7 @@ public function testDenormalizeWithDataContainsNumericKeys() $this->getDenormalizationObjectMapping(), ])); - $denormalizer->denormalize(get_class($this->getObject()), ['test']); + $denormalizer->denormalize(get_class($this->getObject()), ['test'], $this->getDenormalizerContext([])); } public function testDenormalizeWithNotWorkingFactory() @@ -91,7 +91,11 @@ public function testDenormalizeWithAdditionalData() $this->getDenormalizationObjectMapping(), ])); - $denormalizer->denormalize(get_class($this->getObject()), ['name' => 'name', 'value' => 'value']); + $denormalizer->denormalize( + get_class($this->getObject()), + ['name' => 'name', 'value' => 'value'], + $this->getDenormalizerContext([]) + ); } public function testDenormalizeWithAdditionalDataAndAllowIt() @@ -102,8 +106,7 @@ public function testDenormalizeWithAdditionalDataAndAllowIt() $object = $denormalizer->denormalize( get_class($this->getObject()), - ['name' => 'name', 'value' => 'value'], - $this->getDenormalizerContext(true) + ['name' => 'name', 'value' => 'value'] ); self::assertSame('name', $object->getName()); @@ -138,7 +141,7 @@ public function testDenormalizeWithGroups() $object = $denormalizer->denormalize( get_class($this->getObject()), ['name' => 'name'], - $this->getDenormalizerContext(false, ['read']) + $this->getDenormalizerContext(null, ['read']) ); self::assertSame('name', $object->getName()); @@ -153,7 +156,7 @@ public function testDenormalizeWithGroupsButNoGroupOnField() $object = $denormalizer->denormalize( get_class($this->getObject()), ['name' => 'name'], - $this->getDenormalizerContext(false, ['read']) + $this->getDenormalizerContext(null, ['read']) ); self::assertNull($object->getName()); @@ -279,7 +282,7 @@ private function getFieldDenormalizer(): FieldDenormalizerInterface * @return DenormalizerContextInterface */ private function getDenormalizerContext( - bool $allowedAdditionalFields = false, + array $allowedAdditionalFields = null, array $groups = [] ): DenormalizerContextInterface { /** @var DenormalizerContextInterface|\PHPUnit_Framework_MockObject_MockObject $context */ @@ -287,7 +290,7 @@ private function getDenormalizerContext( ->setMethods([]) ->getMockForAbstractClass(); - $context->expects(self::any())->method('isAllowedAdditionalFields')->willReturn($allowedAdditionalFields); + $context->expects(self::any())->method('getAllowedAdditionalFields')->willReturn($allowedAdditionalFields); $context->expects(self::any())->method('getGroups')->willReturn($groups); return $context; diff --git a/tests/DeserializerIntegrationTest.php b/tests/DeserializerIntegrationTest.php index b7841ec..b1bcd91 100644 --- a/tests/DeserializerIntegrationTest.php +++ b/tests/DeserializerIntegrationTest.php @@ -324,7 +324,12 @@ public function testDenormalizeWithAdditionalFieldsExpectsException() $data = json_encode(['name' => 'Name', 'unknownField' => 'value']); - $deserializer->deserialize(Model::class, $data, 'application/json'); + $deserializer->deserialize( + Model::class, + $data, + 'application/json', + DenormalizerContextBuilder::create()->setAllowedAdditionalFields([])->getContext() + ); } public function testDenormalizeWithAllowedAdditionalFields() @@ -347,8 +352,7 @@ public function testDenormalizeWithAllowedAdditionalFields() $object = $deserializer->deserialize( Model::class, $data, - 'application/json', - DenormalizerContextBuilder::create()->setAllowedAdditionalFields(true)->getContext() + 'application/json' ); self::assertSame('Name', $object->getName());