Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.5.0
=====

* (feature) Add normalization stack for better error reporting.


1.4.1
=====

Expand Down
14 changes: 12 additions & 2 deletions src/Normalizer/SimpleNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class SimpleNormalizer
{
private readonly ?ClassMetadataFactory $doctrineMetadata;
private const string STACK_CONTEXT = "simple-normalizer.debug-stack";

/**
* @param ServiceLocator<SimpleObjectNormalizerInterface> $objectNormalizers
Expand Down Expand Up @@ -95,6 +96,13 @@ private function recursiveNormalize (mixed $value, array $context = []) : mixed
return $value;
}

if (!isset($context[self::STACK_CONTEXT]) || !\is_array($context[self::STACK_CONTEXT]))
{
$context[self::STACK_CONTEXT] = [];
}

$context[self::STACK_CONTEXT][] = get_debug_type($value);

if (\is_array($value))
{
return $this->recursiveNormalizeArray($value, $context);
Expand All @@ -121,15 +129,17 @@ private function recursiveNormalize (mixed $value, array $context = []) : mixed
catch (ServiceNotFoundException $exception)
{
throw new ObjectTypeNotSupportedException(\sprintf(
"Can't normalize type %s",
"Can't normalize type '%s' in stack %s",
get_debug_type($value),
implode(" > ", array_reverse($context[self::STACK_CONTEXT])),
), 0, $exception);
}
}

throw new UnsupportedTypeException(\sprintf(
"Can't normalize type %s",
"Can't normalize type %s in stack %s",
get_debug_type($value),
implode(" > ", array_reverse($context[self::STACK_CONTEXT])),
));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Normalizer/ObjectNormalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testNonEmptyStdClassIsInvalid () : void
$object->prop = 5;

$this->expectException(ObjectTypeNotSupportedException::class);
$this->expectExceptionMessage("Can't normalize type stdClass");
$this->expectExceptionMessage("Can't normalize type 'stdClass' in stack stdClass");
$normalizer->normalize($object);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function testContextPassing () : void
[
"test" => 123,
"o" => "hai",
"simple-normalizer.debug-stack" => [
get_debug_type($value),
DummyVO::class,
],
],
);

Expand Down