Bug #70872
closedPropertyMapper shouldn't be a Singleton
100%
Description
Hello.
I have a problem with PropertyMapper being a Singleton, becasue on each ->convert()
call it kills own property messages
.
I have an input from a user, which I need to convert with custom TypeConverter (I convert a csv-file to ObjectStorage with specific objects). And whenever some error occurs on this phase (e.g. wrong file content) I return an instance of \TYPO3\CMS\Extbase\Error\Error
from convertFrom()
of my TypeConverter. This works nice and errorAction()
of my Controller is called, so I'm able to show an error to a user.
But at same time and same request I'm asking external service for additional objects, I need to construct and show to a user. External service returns a JSON response, which is converted to array and I'm using PropertyMapper to map array to needed objects. And this phase is crucial: as soon as I make first call to $propertyMapper->convert()
all the errors from the previous phase are lost and no errorAction()
is called, so user sees a success screen, while an error happened in reality.
Sure, there are workarounds:
1. Don't use PropertyMapper, but create objects via 'new', 'objectManager->get' or whatever and then set their properties by set-ers. The problem with this workaround is, that in case I need some sophisticated TypeConverter I can't use it.
2. Merge error messages every time, when you use PropertyMapper:
$propertyMapper = $this->_objectManager->get(PropertyMapper::class); $messages = $propertyMapper->getMessages(); $object = $propertyMapper->convert($dataArray, Object::class); $propertyMapper->getMessages()->merge($messages);
But this really a hack.
So, it makes sense to avoid Singleton for PropertyMapper.