Skip to content
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

Context::getCurrentPath() returns indexes of list items #1325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ public function getCurrentPath(): array

$paths = [];
foreach ($this->metadataStack as $metadata) {
if ($metadata instanceof ClassMetadata && $metadata->positionInList !== null) {
array_unshift($paths, $metadata->positionInList);
}
if ($metadata instanceof PropertyMetadata) {
array_unshift($paths, $metadata->name);
}
Expand Down
13 changes: 13 additions & 0 deletions src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ public function initialize(VisitorInterface $visitor, Context $context): void
*/
public function accept($data, ?array $type = null)
{
// Element could be one of the element list
$positionInList = null;
if (isset($type['position_in_list'])) {
$positionInList = $type['position_in_list'];
unset($type['position_in_list']);
$type = $type ?: null;
}

// If the type was not given, we infer the most specific type from the
// input data in serialization mode.
if (null === $type) {
Expand Down Expand Up @@ -219,6 +227,11 @@ public function accept($data, ?array $type = null)
$metadata = $this->metadataFactory->getMetadataForClass($type['name']);
\assert($metadata instanceof ClassMetadata);

if ($positionInList !== null) {
$metadata = clone $metadata;
$metadata->positionInList = $positionInList;
}

if ($metadata->usingExpression && null === $this->expressionExclusionStrategy) {
throw new ExpressionLanguageRequiredException(sprintf('To use conditional exclude/expose in %s you must configure the expression language.', $metadata->name));
}
Expand Down
1 change: 1 addition & 0 deletions src/JsonSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function visitArray(array $data, array $type)

$elType = $this->getElementType($type);
foreach ($data as $k => $v) {
$elType['position_in_list'] = $k;
try {
$v = $this->navigator->accept($v, $elType);
} catch (NotAcceptableException $e) {
Expand Down
7 changes: 7 additions & 0 deletions src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class ClassMetadata extends MergeableClassMetadata
*/
public $isMap = false;

/**
* @internal
*
* @var int|null
*/
public $positionInList = null;

/**
* @var bool
*/
Expand Down
7 changes: 6 additions & 1 deletion tests/Serializer/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,29 @@ public function testSerializationContextPathAndDepth()
$exclusionStrategy->expects($this->any())
->method('shouldSkipProperty')
->with($this->anything(), $this->callback(static function (SerializationContext $context) use ($self, $objects) {
$expectedDepth = $expectedPath = null;
$expectedCurrentPath = $expectedDepth = $expectedPath = null;

if ($context->getObject() === $objects[0]) {
$expectedDepth = 1;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node';
$expectedCurrentPath = [];
} elseif ($context->getObject() === $objects[1]) {
$expectedDepth = 2;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
$expectedCurrentPath = ['children', 0];
} elseif ($context->getObject() === $objects[2]) {
$expectedDepth = 2;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
$expectedCurrentPath = ['children', 1];
} elseif ($context->getObject() === $objects[3]) {
$expectedDepth = 3;
$expectedPath = 'JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node -> JMS\Serializer\Tests\Fixtures\Node';
$expectedCurrentPath = ['children', 1, 'children', 0];
}

$self->assertEquals($expectedDepth, $context->getDepth(), 'shouldSkipProperty depth');
$self->assertEquals($expectedPath, $context->getPath(), 'shouldSkipProperty path');
$self->assertEquals($expectedCurrentPath, $context->getCurrentPath(), 'Unexpected context current path');

return true;
}))
Expand Down