From b341535558945105172df13a95ef55055377959b Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 5 Jan 2024 16:42:05 +0100 Subject: [PATCH 1/2] deal with fields for which no constraints have been configured --- .../Validator/Constraints/Collection.php | 29 ++++++++++++++- .../Tests/Constraints/CollectionTest.php | 35 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/Collection.php b/src/Symfony/Component/Validator/Constraints/Collection.php index 316033508a62e..ac35a27d052c7 100644 --- a/src/Symfony/Component/Validator/Constraints/Collection.php +++ b/src/Symfony/Component/Validator/Constraints/Collection.php @@ -42,7 +42,7 @@ class Collection extends Composite */ public function __construct($fields = null, ?array $groups = null, $payload = null, ?bool $allowExtraFields = null, ?bool $allowMissingFields = null, ?string $extraFieldsMessage = null, ?string $missingFieldsMessage = null) { - if (\is_array($fields) && ([] === $fields || ($firstField = reset($fields)) instanceof Constraint || ($firstField[0] ?? null) instanceof Constraint)) { + if (self::isFieldsOption($fields)) { $fields = ['fields' => $fields]; } @@ -87,4 +87,31 @@ protected function getCompositeOption() { return 'fields'; } + + private static function isFieldsOption($options): bool + { + if (!\is_array($options)) { + return false; + } + + if ([] === $options) { + return true; + } + + foreach ($options as $optionOrField) { + if ($optionOrField instanceof Constraint) { + return true; + } + + if (!\is_array($optionOrField)) { + return false; + } + + if ([] !== $optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) { + return false; + } + } + + return true; + } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php index 2b9acb8d43fa4..4ff8b6d6aac6a 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php @@ -170,6 +170,41 @@ public function testEmptyFieldsInOptions() 'extraFieldsMessage' => 'foo bar baz', ]); + $this->assertSame([], $constraint->fields); + $this->assertTrue($constraint->allowExtraFields); + $this->assertSame('foo bar baz', $constraint->extraFieldsMessage); + } + + public function testEmptyConstraintListFor() + { + $constraint = new Collection([ + 'foo' => [], + ], + null, + null, + true, + null, + 'foo bar baz' + ); + + $this->assertArrayHasKey('foo', $constraint->fields); + $this->assertInstanceOf(Required::class, $constraint->fields['foo']); + $this->assertTrue($constraint->allowExtraFields); + $this->assertSame('foo bar baz', $constraint->extraFieldsMessage); + } + + public function testEmptyConstraintListForFieldInOptions() + { + $constraint = new Collection([ + 'fields' => [ + 'foo' => [], + ], + 'allowExtraFields' => true, + 'extraFieldsMessage' => 'foo bar baz', + ]); + + $this->assertArrayHasKey('foo', $constraint->fields); + $this->assertInstanceOf(Required::class, $constraint->fields['foo']); $this->assertTrue($constraint->allowExtraFields); $this->assertSame('foo bar baz', $constraint->extraFieldsMessage); } From f6217d87e66d950a8c61b9ea54af09282aa4734e Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sun, 4 Feb 2024 17:24:55 +0100 Subject: [PATCH 2/2] [Validator] Fix fields without constraints in `Collection` --- .../Validator/Constraints/Collection.php | 10 +++++----- .../Tests/Constraints/CollectionTest.php | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Collection.php b/src/Symfony/Component/Validator/Constraints/Collection.php index ac35a27d052c7..d7a1a8f69ade5 100644 --- a/src/Symfony/Component/Validator/Constraints/Collection.php +++ b/src/Symfony/Component/Validator/Constraints/Collection.php @@ -94,20 +94,20 @@ private static function isFieldsOption($options): bool return false; } - if ([] === $options) { - return true; - } - foreach ($options as $optionOrField) { if ($optionOrField instanceof Constraint) { return true; } + if (null === $optionOrField) { + continue; + } + if (!\is_array($optionOrField)) { return false; } - if ([] !== $optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) { + if ($optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) { return false; } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php index 4ff8b6d6aac6a..19cffc693158f 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CollectionTest.php @@ -175,10 +175,15 @@ public function testEmptyFieldsInOptions() $this->assertSame('foo bar baz', $constraint->extraFieldsMessage); } - public function testEmptyConstraintListFor() + /** + * @testWith [[]] + * [null] + */ + public function testEmptyConstraintListForField(?array $fieldConstraint) { - $constraint = new Collection([ - 'foo' => [], + $constraint = new Collection( + [ + 'foo' => $fieldConstraint, ], null, null, @@ -193,11 +198,15 @@ public function testEmptyConstraintListFor() $this->assertSame('foo bar baz', $constraint->extraFieldsMessage); } - public function testEmptyConstraintListForFieldInOptions() + /** + * @testWith [[]] + * [null] + */ + public function testEmptyConstraintListForFieldInOptions(?array $fieldConstraint) { $constraint = new Collection([ 'fields' => [ - 'foo' => [], + 'foo' => $fieldConstraint, ], 'allowExtraFields' => true, 'extraFieldsMessage' => 'foo bar baz',