Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 527bc77

Browse filesBrowse files
committed
[Serializer] Add the possibilitiy to filter attributes
1 parent 7c5dcfc commit 527bc77
Copy full SHA for 527bc77

File tree

2 files changed

+36
-1
lines changed
Filter options

2 files changed

+36
-1
lines changed

‎src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,15 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
236236
*/
237237
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
238238
{
239-
return !in_array($attribute, $this->ignoredAttributes);
239+
if (in_array($attribute, $this->ignoredAttributes)) {
240+
return false;
241+
}
242+
243+
if (isset($context['attributes']) && is_array($context['attributes'])) {
244+
return in_array($attribute, $context['attributes'], true);
245+
}
246+
247+
return true;
240248
}
241249

242250
/**

‎src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,33 @@ public function testExtractAttributesRespectsContext()
643643

644644
$this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, null, array('include_foo_and_bar' => true)));
645645
}
646+
647+
public function testAttributesContextNormalize()
648+
{
649+
$normalizer = new ObjectNormalizer();
650+
651+
$objectDummy = new ObjectDummy();
652+
$objectDummy->setFoo('foo');
653+
$objectDummy->setBaz('baz');
654+
655+
$context = array('attributes' => array('foo', 'baz'));
656+
$this->assertEquals(array('foo' => 'foo', 'baz' => 'baz'), $normalizer->normalize($objectDummy, null, $context));
657+
}
658+
659+
public function testAttributesContextDenormalize()
660+
{
661+
$normalizer = new ObjectNormalizer();
662+
663+
$expected = new ObjectDummy();
664+
$expected->setFoo('foo');
665+
$expected->setBaz('baz');
666+
667+
$context = array('attributes' => array('foo', 'baz'));
668+
$this->assertEquals(
669+
$expected,
670+
$normalizer->denormalize(array('foo' => 'foo', 'baz' => 'baz', 'bar' => 'bar'), ObjectDummy::class, null, $context)
671+
);
672+
}
646673
}
647674

648675
class ObjectDummy

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.