[BUGFIX] Extbase: Optional arguments must not throw an Exception 63/42663/2
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sun, 16 Aug 2015 16:45:32 +0000 (18:45 +0200)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Mon, 17 Aug 2015 10:09:17 +0000 (12:09 +0200)
Example:
public function barAction(\Vendor\Ext\Domain\Model\Foo $foo = NULL)

If the property mapper receives NULL, 0 or a valid value
the property mapping does not fail. But if the value is
a valid uid (>0) and there is no record with that uid
the whole property mapping fails.

While this is totally valid for required arguments,
optional arguments must be skipped.

Resolves: #55861
Resolves: #55837
Resolves: #62951
Releases: master, 6.2
Change-Id: I4c954bac50be1d34ea09bfc21f8978e9165683d1
Reviewed-on: http://review.typo3.org/42663
Reviewed-by: Alexander Schnitzler <typo3@alexanderschnitzler.de>
Tested-by: Alexander Schnitzler <typo3@alexanderschnitzler.de>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
typo3/sysext/extbase/Classes/Mvc/Controller/Argument.php
typo3/sysext/extbase/Classes/Property/PropertyMapper.php

index 75c616b..1fc8a10 100644 (file)
@@ -13,6 +13,9 @@ namespace TYPO3\CMS\Extbase\Mvc\Controller;
  *
  * The TYPO3 project - inspiring people to share!
  */
+
+use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException;
+
 /**
  * A controller argument
  *
@@ -359,7 +362,7 @@ class Argument {
         *
         * @param mixed $rawValue The value of this argument
         * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument
-        * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException if the argument is not a valid object of type $dataType
+        * @throws \TYPO3\CMS\Extbase\Property\Exception
         */
        public function setValue($rawValue) {
                if ($this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
@@ -371,7 +374,14 @@ class Argument {
                                $this->value = $rawValue;
                                return $this;
                        }
-                       $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
+                       try {
+                               $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
+                       } catch (TargetNotFoundException $e) {
+                               // for optional arguments no exeption is thrown.
+                               if ($this->isRequired()) {
+                                       throw $e;
+                               }
+                       }
                        $this->validationResults = $this->propertyMapper->getMessages();
                        if ($this->validator !== NULL) {
                                // TODO: Validation API has also changed!!!
index 7bbc804..015c82c 100644 (file)
@@ -21,6 +21,7 @@ namespace TYPO3\CMS\Extbase\Property;
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
 
+use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException;
 use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
 
 /**
@@ -105,6 +106,8 @@ class PropertyMapper implements \TYPO3\CMS\Core\SingletonInterface {
                        }
 
                        return $result;
+               } catch (TargetNotFoundException $e) {
+                       throw $e;
                } catch (\Exception $e) {
                        throw new \TYPO3\CMS\Extbase\Property\Exception('Exception while property mapping at property path "' . implode('.', $currentPropertyPath) . '":' . $e->getMessage(), 1297759968, $e);
                }