apitte icon indicating copy to clipboard operation
apitte copied to clipboard

SymfonyValidator errors with typed properties

Open martenb opened this issue 1 year ago • 3 comments

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...

martenb avatar Dec 11 '23 18:12 martenb

Will take a 👀

f3l1x avatar Dec 15 '23 15:12 f3l1x

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.

f3l1x avatar Feb 23 '24 17:02 f3l1x

I've added some more tests for the future https://github.com/contributte/apitte/commit/33ec0d462a7d30c174e2519fac940c8d4382424c

f3l1x avatar Feb 23 '24 17:02 f3l1x