nette / schema

📐 Validating data structures against a given Schema.
https://doc.nette.org/schema
Other
905 stars 26 forks source link

Type error with Helpers::getCastStrategy and DTO with constructor sice v1.2.5 #59

Closed martinstainer closed 1 year ago

martinstainer commented 1 year ago

Version: v1.2.5

Bug Description

Since the v1.2.5 my code throws type error, because Helpers::getCastStrategy expects array or single value, but gets stdClass when using data object with constructor.

testArrayOfUsers
   TypeError: RBCB\Schema\User::__construct(): Argument #1 ($xxxx) must be of type ?string, stdClass given, called in 
   vendor/nette/schema/src/Schema/Helpers.php on line 186

Steps To Reproduce

$json = Json::decode($data);
foreach ($json->data as $user) {
       $schema = Expect::from(new User());
       $user = $processor->process($schema, $user);
       yield $user;
}

And the DTO

class User
{
    public function __construct(
        public ?string $yyyy = null,
        public ?string $xxxx = null,
        public ?string $zzz= null,
     .... 
}

Expected Behavior

Im unsure if I need to cast it manually, so let me know if I use the schema correctly or not.

Possible Solution

Editing the method_exists($type, '__construct') for supporting stdClass.

dg commented 1 year ago

Expect::from() didn't support promoted properties, it used to work actually just because all parameters had a default value. I tried adding direct support for the constructor.