From a0b160a7fb041ce036cefa69fb0d559194e3a5ee Mon Sep 17 00:00:00 2001 From: Thijs-jan Veldhuizen Date: Wed, 23 Jul 2025 10:18:51 +0200 Subject: [PATCH] Fix deprecations in symfony/validator, including the fix in #2417 Fixes #2416 --- .../Annotations/AbstractScalarParam.php | 25 +++++++++++-------- Controller/Annotations/FileParam.php | 10 +++++--- .../Annotations/AbstractScalarParamTest.php | 4 +-- .../Controller/Annotations/FileParamTest.php | 16 ++++++------ 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Controller/Annotations/AbstractScalarParam.php b/Controller/Annotations/AbstractScalarParam.php index 5243f48c1..646f32388 100644 --- a/Controller/Annotations/AbstractScalarParam.php +++ b/Controller/Annotations/AbstractScalarParam.php @@ -41,19 +41,19 @@ public function getConstraints() if ($this->requirements instanceof Constraint) { $constraints[] = $this->requirements; } elseif (is_scalar($this->requirements)) { - $constraints[] = new Regex([ - 'pattern' => '#^(?:'.$this->requirements.')$#xsu', - 'message' => sprintf( + $constraints[] = new Regex( + '#^(?:'.$this->requirements.')$#xsu', + sprintf( 'Parameter \'%s\' value, does not match requirements \'%s\'', $this->getName(), $this->requirements ), - ]); + ); } elseif (is_array($this->requirements) && isset($this->requirements['rule']) && $this->requirements['error_message']) { - $constraints[] = new Regex([ - 'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu', - 'message' => $this->requirements['error_message'], - ]); + $constraints[] = new Regex( + '#^(?:'.$this->requirements['rule'].')$#xsu', + $this->requirements['error_message'], + ); } elseif (is_array($this->requirements)) { foreach ($this->requirements as $index => $requirement) { if ($requirement instanceof Constraint) { @@ -75,9 +75,12 @@ public function getConstraints() // If the user wants to map the value, apply all constraints to every // value of the map if ($this->map) { - $constraints = [ - new All(['constraints' => $constraints]), - ]; + if ([] !== $constraints) { + $constraints = [ + new All($constraints), + ]; + } + if (false === $this->nullable) { $constraints[] = new NotNull(); } diff --git a/Controller/Annotations/FileParam.php b/Controller/Annotations/FileParam.php index 55e624db3..8b157475f 100644 --- a/Controller/Annotations/FileParam.php +++ b/Controller/Annotations/FileParam.php @@ -79,15 +79,19 @@ public function getConstraints() $options = is_array($this->requirements) ? $this->requirements : []; if ($this->image) { - $constraints[] = new Image($options); + $constraint = new Image(); } else { - $constraints[] = new File($options); + $constraint = new File(); } + foreach ($options as $name => $value) { + $constraint->$name = $value; + } + $constraints[] = $constraint; // If the user wants to map the value if ($this->map) { $constraints = [ - new All(['constraints' => $constraints]), + new All($constraints), ]; } diff --git a/Tests/Controller/Annotations/AbstractScalarParamTest.php b/Tests/Controller/Annotations/AbstractScalarParamTest.php index b5d35894f..03e7391f6 100644 --- a/Tests/Controller/Annotations/AbstractScalarParamTest.php +++ b/Tests/Controller/Annotations/AbstractScalarParamTest.php @@ -133,8 +133,6 @@ public function testArrayWithNoConstraintsDoesNotCreateInvalidConstraint() { $this->param->nullable = true; $this->param->map = true; - $this->assertEquals([new All([ - 'constraints' => [], - ])], $this->param->getConstraints()); + $this->assertEquals([], $this->param->getConstraints()); } } diff --git a/Tests/Controller/Annotations/FileParamTest.php b/Tests/Controller/Annotations/FileParamTest.php index 81f8bdd77..fe7773a71 100644 --- a/Tests/Controller/Annotations/FileParamTest.php +++ b/Tests/Controller/Annotations/FileParamTest.php @@ -75,19 +75,19 @@ public function testComplexRequirements() public function testFileRequirements() { $this->param->nullable = true; - $this->param->requirements = $requirements = ['mimeTypes' => 'application/json']; + $this->param->requirements = ['mimeTypes' => 'application/json']; $this->assertEquals([ - new File($requirements), + new File(null, null, null, 'application/json'), ], $this->param->getConstraints()); } public function testImageRequirements() { $this->param->image = true; - $this->param->requirements = $requirements = ['mimeTypes' => 'image/gif']; + $this->param->requirements = ['mimeTypes' => ['image/*']]; $this->assertEquals([ new NotNull(), - new Image($requirements), + new Image(null, null, null, ['image/*']), ], $this->param->getConstraints()); } @@ -95,20 +95,20 @@ public function testImageConstraintsTransformWhenParamIsAnArray() { $this->param->image = true; $this->param->map = true; - $this->param->requirements = $requirements = ['mimeTypes' => 'image/gif']; + $this->param->requirements = ['mimeTypes' => ['image/*']]; $this->assertEquals([new All([ new NotNull(), - new Image($requirements), + new Image(null, null, null, ['image/*']), ])], $this->param->getConstraints()); } public function testFileConstraintsWhenParamIsAnArray() { $this->param->map = true; - $this->param->requirements = $requirements = ['mimeTypes' => 'application/pdf']; + $this->param->requirements = ['mimeTypes' => 'application/pdf']; $this->assertEquals([new All([ new NotNull(), - new File($requirements), + new File(null, null, null, 'application/pdf'), ])], $this->param->getConstraints()); } }