misc: transform missing source value to null in flexible mode

This commit is contained in:
Romain Canon 2022-11-06 21:38:14 +01:00
parent 034f1c51e1
commit 92a41a1564
8 changed files with 10 additions and 19 deletions

View File

@ -28,7 +28,7 @@ final class ArrayNodeBuilder implements NodeBuilder
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
{
$type = $shell->type();
$value = $shell->hasValue() ? $shell->value() : null;
$value = $shell->value();
assert($type instanceof ArrayType || $type instanceof NonEmptyArrayType || $type instanceof IterableType);

View File

@ -22,12 +22,10 @@ final class IterableNodeBuilder implements NodeBuilder
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
{
if ($shell->hasValue()) {
$value = $shell->value();
$value = $shell->value();
if (is_iterable($value) && ! is_array($value)) {
$shell = $shell->withValue(iterator_to_array($value));
}
if (is_iterable($value) && ! is_array($value)) {
$shell = $shell->withValue(iterator_to_array($value));
}
return $this->delegate->build($shell, $rootBuilder);

View File

@ -27,7 +27,7 @@ final class ListNodeBuilder implements NodeBuilder
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
{
$type = $shell->type();
$value = $shell->hasValue() ? $shell->value() : null;
$value = $shell->value();
assert($type instanceof ListType || $type instanceof NonEmptyListType);

View File

@ -23,7 +23,7 @@ final class ScalarNodeBuilder implements NodeBuilder
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
{
$type = $shell->type();
$value = $shell->hasValue() ? $shell->value() : null;
$value = $shell->value();
assert($type instanceof ScalarType);

View File

@ -28,7 +28,7 @@ final class ShapedArrayNodeBuilder implements NodeBuilder
public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
{
$type = $shell->type();
$value = $shell->hasValue() ? $shell->value() : null;
$value = $shell->value();
assert($type instanceof ShapedArrayType);

View File

@ -31,13 +31,7 @@ final class StrictNodeBuilder implements NodeBuilder
if (! $shell->hasValue()) {
if ($this->flexible) {
if ($type->accepts(null)) {
return TreeNode::leaf($shell, null);
}
if ($type->accepts([])) {
return TreeNode::leaf($shell, []);
}
return $this->delegate->build($shell->withValue(null), $rootBuilder);
}
throw new MissingNodeValue($type);

View File

@ -25,7 +25,7 @@ final class UnionNodeBuilder implements NodeBuilder
{
$type = $shell->type();
if (! $type instanceof UnionType || ! $shell->hasValue()) {
if (! $type instanceof UnionType) {
return $this->delegate->build($shell, $rootBuilder);
}

View File

@ -436,8 +436,7 @@ final class FlexibleMappingTest extends IntegrationTest
} catch (MappingError $exception) {
$error = $exception->node()->children()['bar']->messages()[0];
self::assertSame('1655449641', $error->code());
self::assertSame('Cannot be empty and must be filled with a value matching type `string`.', (string)$error);
self::assertSame('Value *missing* is not a valid string.', (string)$error);
}
}
}