apitte
apitte copied to clipboard
SymfonyValidator errors with typed properties
Ok, i switched to typed properties and found some bugs in SymfonyValidator,, i am not sure, how to fix it, maybe you will know?
#[Assert\NotNull]
#[Assert\Type(type: 'integer')]
public $typedId;
when you send ['typedId' => 'sss'] the validator throws typeId is not integer, but
#[Assert\NotNull]
#[Assert\Type(type: 'integer')]
public int $typedId;
throws typedId cannot be null...
This is real problem in nullable properties
#[Assert\Type(type: 'integer')]
public ?int $typedId = null;
when you send ['typedId' => 'sss'], it will pass without any errors...
Will take a 👀
At this time, implementation looks like this:
https://github.com/contributte/apitte/blob/816aa4fb8670d8ff1bbe9777e4b70ebbb1142788/src/Core/Mapping/Request/BasicEntity.php#L45-L72
It means, if you have typed property int $typedId and try to assign string to it, PHP will throws error, but there is a try/catch.
https://github.com/contributte/apitte/blob/816aa4fb8670d8ff1bbe9777e4b70ebbb1142788/src/Core/Mapping/Request/BasicEntity.php#L64-L68
It's not the best implementation I know. Better implementation would be read the validations (NotNull, TypedInt, etc.) and validate input as array, but it's huge BC at this time.
I've added some more tests for the future https://github.com/contributte/apitte/commit/33ec0d462a7d30c174e2519fac940c8d4382424c