-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent results with nullable property #114
Comments
Introducing a second nullable will make it possible to map. final class Foo
{
public string|null $note;
public string|null $note2;
}
$foo = (new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(Foo::class, []);
$foo->note === null; // true |
With an final class Foo
{
public Bar|null $bar;
}
class Bar {
public string|null $title;
public string|null $title2;
}
$foo = (new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(Foo::class, []);
$foo->bar instanceof Bar; // true Introducing a new property yields different result. final class Foo
{
public $title; // New property
public Bar|null $bar;
}
class Bar {
public string|null $title;
public string|null $title2;
}
$foo = (new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(Foo::class, []);
$foo->bar instanceof Bar; // false |
Thanks for the excellent reproduction steps, I will definitely take a look at it. Also, looks like it's bound to #34. |
@romm Thanks! :) Not sure if this fits here, but anyway, some testing: <?php
require "vendor/autoload.php";
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Tree\Node;
final class Foo
{
public string $note = "ok";
public Foo $foo;
}
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(Foo::class, []);
} catch (MappingError $e) {
print_node($e->node());
}
function print_node(Node $node): void
{
foreach ($node->messages() as $message) {
echo $node->path() . ": " .$message . "\n";
}
foreach ($node->children() as $child) {
print_node($child);
}
}
Result
Expected
Nested type might be a bit tricky to render any further.. :) |
<?php
require "vendor/autoload.php";
final class Foo
{
public ?Foo $foo;
}
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(Foo::class, [
'bar' => 'String'
]); Result
Changing type to |
<?php
require "vendor/autoload.php";
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Tree\Node;
final class Foo
{
public ?string $bar = null;
}
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(Foo::class, []);
} catch (MappingError $e) {
print_node($e->node());
}
function print_node(Node $node): void
{
foreach ($node->messages() as $message) {
echo $node->path() . ": " .$message . "\n";
}
foreach ($node->children() as $child) {
print_node($child);
}
} Result
ExpectedNo error. $bar is nullable and has default value of null. There is no error when the default value is a string, so it is partially implemented. |
Seems like you have solved all my points in this issue now 👏🏻 Recursive issue: Take a look at the following example. When sending a nested value, the error is reported as expected (type of Foo is exposed). So it seems to be sort-of implemented. But, I can agree that this is a non-blocking issue 🙂 require "vendor/autoload.php";
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Tree\Node;
final class Foo
{
public string $note = "hello";
public ?Foo $foo;
}
try {
(new \CuyZ\Valinor\MapperBuilder())
->mapper()
->map(Foo::class, [
'foo' => [
'foo' => 'ok'
]
]);
} catch (MappingError $e) {
print_node($e->node());
}
function print_node(Node $node): void
{
foreach ($node->messages() as $message) {
echo $node->path() . ": " .$message . "\n";
}
foreach ($node->children() as $child) {
print_node($child);
}
} Result
|
Fixed with #159. |
When the source is empty, is the mapper not able to set
null
to nullable property.Reproduction
Expected behavior
Foo::$note
should be assignednull
as value to be consistent with exampleBar
, but see my other issue here: #113.The text was updated successfully, but these errors were encountered: