You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
Properties defined as type string and format date or datetime have unsufficient error handling when denormalizing invalid formats. When attempting to denormalize data that does not follow the required format, the following type of error occurs:
TypeError : Model\Foo::setBar(): Argument #1 ($bar) must be of type ?DateTime, bool given, called in ...\Normalizer\FooNormalizer.php on line ..
How to reproduce
Use any OpenAPI v3 spec with a date or datetime field.
Generate models and normalizers.
Attempt to denormalize an invalid datetime value, such as "01/01/2020" or "foobar".
Cause
The generated Normalizer code looks like this:
class FooNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
{
(..)
public function denormalize($data, $class, $format = null, array $context = array())
{
(..)
if (\array_key_exists('bar', $data) && $data['bar'] !== null) {
$object->setBar(\DateTime::createFromFormat('Y-m-d\\TH:i:sP', $data['bar']));
unset($data['bar']);
}
And the generated Model looks like this:
class Bar extends \ArrayObject
{
(..)
public function setBar(?\DateTime $bar) : self
{
$this->initialized['bar'] = true;
$this->bar= $bar;
return $this;
}
}
When DateTime::createFromFormat fails, it returns false, which is not accepted by setBar which requires ?DateTime
Possible Solution
I think the optimal solution would be to make format validation part of the generated validators. That way invalid formats never reach this part of the denormalization where it's too late to do anything with it.
The text was updated successfully, but these errors were encountered:
Jane version(s) affected:
7
Description
Properties defined as type
string
and formatdate
ordatetime
have unsufficient error handling when denormalizing invalid formats. When attempting to denormalize data that does not follow the required format, the following type of error occurs:How to reproduce
Cause
The generated Normalizer code looks like this:
And the generated Model looks like this:
When
DateTime::createFromFormat
fails, it returnsfalse
, which is not accepted by setBar which requires?DateTime
Possible Solution
I think the optimal solution would be to make format validation part of the generated validators. That way invalid formats never reach this part of the denormalization where it's too late to do anything with it.
The text was updated successfully, but these errors were encountered: