Skip to content

Commit

Permalink
[BUGFIX] Reset Type Conversion Messages after usage
Browse files Browse the repository at this point in the history
PropertyMapper (which is a singleton) previously created a new
$messages instance every time "convert" was called.

This works for most cases, however when using "getMessages()"
only the last message was returned.

It would be very useful to keep the messages in place
until "resetMessages()" is called, which is used within each
action of the Extbase controller validation
to avoid some edge-case issues when using multiple actions on one request.

Resolves: #70872
Releases: master
Change-Id: I2fddc4261567cad2ebb12f91296ff3ec8d681ad8
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65114
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
  • Loading branch information
bmack authored and maddy2101 committed Aug 4, 2020
1 parent 48b8474 commit 2f05c8d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions typo3/sysext/extbase/Classes/Mvc/Controller/Argument.php
Expand Up @@ -273,6 +273,7 @@ public function setValue($rawValue)
}
}
$this->validationResults->merge($this->propertyMapper->getMessages());
$this->propertyMapper->resetMessages();
return $this;
}

Expand Down
12 changes: 10 additions & 2 deletions typo3/sysext/extbase/Classes/Property/PropertyMapper.php
Expand Up @@ -91,6 +91,7 @@ public function injectConfigurationBuilder(PropertyMappingConfigurationBuilder $
*/
public function initializeObject()
{
$this->resetMessages();
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] as $typeConverterClassName) {
$typeConverter = $this->objectManager->get($typeConverterClassName);
foreach ($typeConverter->getSupportedSourceTypes() as $supportedSourceType) {
Expand All @@ -117,7 +118,6 @@ public function convert($source, $targetType, PropertyMappingConfigurationInterf
$configuration = $this->configurationBuilder->build();
}
$currentPropertyPath = [];
$this->messages = new Result();
try {
$result = $this->doMapping($source, $targetType, $configuration, $currentPropertyPath);
if ($result instanceof Error) {
Expand All @@ -133,7 +133,7 @@ public function convert($source, $targetType, PropertyMappingConfigurationInterf
}

/**
* Get the messages of the last Property Mapping
* Get the messages of the last Property Mapping.
*
* @return \TYPO3\CMS\Extbase\Error\Result
*/
Expand All @@ -142,6 +142,14 @@ public function getMessages()
return $this->messages;
}

/**
* Resets the messages of the last Property Mapping.
*/
public function resetMessages(): void
{
$this->messages = new Result();
}

/**
* Internal function which actually does the property mapping.
*
Expand Down
Expand Up @@ -31,14 +31,14 @@ class DateTimeConverterTest extends FunctionalTestCase
public function convertFromReturnsAnErrorWhenConvertingIntegersToDateTime()
{
$propertyMapper = $this->getContainer()->get(PropertyMapper::class);

$dateTime = $propertyMapper->convert(0, \DateTime::class);

self::assertNull($dateTime);
self::assertTrue($propertyMapper->getMessages()->hasErrors());
$messages = $propertyMapper->getMessages();
self::assertTrue($messages->hasErrors());
self::assertSame(
'The date "%s" was not recognized (for format "%s").',
$propertyMapper->getMessages()->getFirstError()->getMessage()
$messages->getFirstError()->getMessage()
);
}

Expand Down Expand Up @@ -304,10 +304,11 @@ public function convertFromReturnsErrorIfSourceIsAnArrayAndEitherDayMonthOrYearA
);

self::assertNull($dateTime);
self::assertTrue($propertyMapper->getMessages()->hasErrors());
$messages = $propertyMapper->getMessages();
self::assertTrue($messages->hasErrors());
self::assertSame(
'Could not convert the given date parts into a DateTime object because one or more parts were 0.',
$propertyMapper->getMessages()->getFirstError()->getMessage()
$messages->getFirstError()->getMessage()
);
}

Expand Down
1 change: 1 addition & 0 deletions typo3/sysext/form/Classes/Mvc/ProcessingRule.php
Expand Up @@ -168,6 +168,7 @@ public function process($value)
if ($this->dataType !== null) {
$value = $this->propertyMapper->convert($value, $this->dataType, $this->propertyMappingConfiguration);
$messages = $this->propertyMapper->getMessages();
$this->propertyMapper->resetMessages();
} else {
$messages = GeneralUtility::makeInstance(ObjectManager::class)
->get(Result::class);
Expand Down

0 comments on commit 2f05c8d

Please sign in to comment.