[BUGFIX] ActionController must not register superfluous validators 01/56901/6
authorAlexander Schnitzler <git@alexanderschnitzler.de>
Wed, 9 May 2018 13:43:36 +0000 (15:43 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 17 May 2018 08:25:30 +0000 (10:25 +0200)
\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::initializeActionMethodValidators
registers a superfluous property validator for simple types. As these
kinds of validators are added through method getBaseValidatorConjunction,
the superfluous registration is removed.

Releases: master
Resolves: #84953
Change-Id: I02fe7863b9014aa6072653d536dfeed67a01deda
Reviewed-on: https://review.typo3.org/56901
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/extbase/Classes/Mvc/Controller/ActionController.php
typo3/sysext/extbase/Tests/Functional/Mvc/Controller/ActionControllerTest.php

index 82854de..4ace0e0 100644 (file)
@@ -20,8 +20,8 @@ use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
 use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
 use TYPO3\CMS\Extbase\Mvc\Web\Request as WebRequest;
-use TYPO3\CMS\Extbase\Validation\Validator\AbstractCompositeValidator;
 use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
+use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
 use TYPO3Fluid\Fluid\View\TemplateView;
 
 /**
@@ -251,40 +251,31 @@ class ActionController extends AbstractController
      */
     protected function initializeActionMethodValidators()
     {
-        $methodParameters = $this->reflectionService->getMethodParameters(static::class, $this->actionMethodName);
-
-        /** @var ConjunctionValidator[] $validatorConjunctions */
-        $validatorConjunctions = [];
-        foreach ($methodParameters as $parameterName => $methodParameter) {
-            /** @var ConjunctionValidator $validatorConjunction */
-            $validatorConjunction = $this->objectManager->get(ConjunctionValidator::class);
-
-            // @todo: remove check for old underscore model name syntax once it's possible
-            if (strpbrk($methodParameter['type'], '_\\') === false) {
-                // this checks if the type is a simply type and then adds a
-                // validator. StringValidator and such for example.
-                $typeValidator = $this->validatorResolver->createValidator($methodParameter['type']);
+        if ($this->arguments->count() === 0) {
+            return;
+        }
 
-                if ($typeValidator !== null) {
-                    $validatorConjunction->addValidator($typeValidator);
-                }
-            }
+        $classSchema = $this->reflectionService->getClassSchema(static::class);
 
-            $validatorConjunctions[$parameterName] = $validatorConjunction;
+        /** @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument */
+        foreach ($this->arguments as $argument) {
+            $validator = $this->objectManager->get(ConjunctionValidator::class);
+            $validatorDefinitions = $classSchema->getMethod($this->actionMethodName)['params'][$argument->getName()]['validators'] ?? [];
+
+            foreach ($validatorDefinitions as $validatorDefinition) {
+                /** @var ValidatorInterface $validatorInstance */
+                $validatorInstance = $this->objectManager->get(
+                    $validatorDefinition['className'],
+                    $validatorDefinition['options']
+                );
 
-            foreach ($methodParameter['validators'] as $validator) {
-                $validatorConjunctions[$parameterName]->addValidator(
-                    $this->objectManager->get($validator['className'], $validator['options'])
+                $validator->addValidator(
+                    $validatorInstance
                 );
             }
-        }
-
-        /** @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument */
-        foreach ($this->arguments as $argument) {
-            $validator = $validatorConjunctions[$argument->getName()];
 
             $baseValidatorConjunction = $this->validatorResolver->getBaseValidatorConjunction($argument->getDataType());
-            if (!empty($baseValidatorConjunction) && $validator instanceof AbstractCompositeValidator) {
+            if ($baseValidatorConjunction->count() > 0) {
                 $validator->addValidator($baseValidatorConjunction);
             }
             $argument->setValidator($validator);
index 18839e0..af99292 100644 (file)
@@ -140,9 +140,6 @@ class ActionControllerTest extends \TYPO3\TestingFramework\Core\Functional\Funct
         static::assertInstanceOf(\SplObjectStorage::class, $validators);
 
         $validators->rewind();
-        static::assertInstanceOf(StringValidator::class, $validators->current());
-
-        $validators->next();
         static::assertInstanceOf(Fixture\Validation\Validator\CustomValidator::class, $validators->current());
 
         $validators->next();
@@ -151,10 +148,6 @@ class ActionControllerTest extends \TYPO3\TestingFramework\Core\Functional\Funct
         $subConjunctionValidator = $validators->current();
         static::assertInstanceOf(ConjunctionValidator::class, $subConjunctionValidator);
 
-        // todo: It doesn't make sense that there is another conjunction
-        // todo: valididator with a StringValidator attached. We need to
-        // todo: find out what causes that and how to fix this.
-
         /** @var \SplObjectStorage $subValidators */
         $subValidators = $subConjunctionValidator->getValidators();
         static::assertInstanceOf(\SplObjectStorage::class, $subValidators);
@@ -193,19 +186,9 @@ class ActionControllerTest extends \TYPO3\TestingFramework\Core\Functional\Funct
         /** @var \SplObjectStorage $validators */
         $validators = $conjunctionValidator->getValidators();
         static::assertInstanceOf(\SplObjectStorage::class, $validators);
+        static::assertCount(1, $validators);
 
         $validators->rewind();
         static::assertInstanceOf(NotEmptyValidator::class, $validators->current());
-
-        $validators->next();
-
-        /** @var ConjunctionValidator $subConjunctionValidator */
-        $subConjunctionValidator = $validators->current();
-        static::assertInstanceOf(ConjunctionValidator::class, $subConjunctionValidator);
-
-        /** @var \SplObjectStorage $subValidators */
-        $subValidators = $subConjunctionValidator->getValidators();
-        static::assertInstanceOf(\SplObjectStorage::class, $subValidators);
-        static::assertEmpty($subValidators);
     }
 }