Description
Symfony version(s) affected: 5.1.6, 5.1.7
Description
Hello!
In one of our projects we have a class similar to this:
final class ExampleDTO
{
private ?string $example;
public function __construct(?string $example)
{
$this->example = $example;
}
public function getExample(): string
{
return $this->example ?? 'fallback';
}
}
It's used in the context of api platform, where incoming data is being deserialized to this object.
Before with Symfony 5.1.5 the example
property was detected as type ?string
, after upgrading to 5.1.6/5.1.7 it's only detected as type string
. Now when the incoming data contains a null value, it will fail with The type of the "example" attribute must be "string", "NULL" given.
I did some tests and was able to track this new behaviour down to the property-info component. I suspect this change to be the reason:
#38041
It looks like now the return type of getExample()
is used instead of the property type hint (?string
).
So I would like to ask if this is the expected behaviour and if yes, how can I make the old way work again (somehow influence the priorities?). Thanks in advance!
How to reproduce
I created a reproducer here:
https://github.com/rogamoore/property-info-bug
Steps:
- Checkout https://github.com/rogamoore/property-info-bug/releases/tag/old-behaviour (property-info 5.1.5)
- Run
composer install
- Run
symfony server:start
- Go to
http://127.0.0.1:8000/
- Result:
^ array:1 [▼
0 => Symfony\Component\PropertyInfo\Type {#116 ▼
-builtinType: "string"
-nullable: true
-class: null
-collection: false
-collectionKeyType: null
-collectionValueType: null
}
]
- Checkout https://github.com/rogamoore/property-info-bug/releases/tag/new-behaviour (property-info 5.1.6)
- Run
composer install
- Run
symfony server:start
- Go to
http://127.0.0.1:8000/
- Result:
^ array:1 [▼
0 => Symfony\Component\PropertyInfo\Type {#116 ▼
-builtinType: "string"
-nullable: false
-class: null
-collection: false
-collectionKeyType: null
-collectionValueType: null
}
]
Additional context
PHP 7.4.10