[Serializer] Allow to pass a single value for the groups opt

This commit is contained in:
Kévin Dunglas 2018-06-05 11:16:28 +02:00
parent 0e9ded3188
commit 5b392030f9
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
2 changed files with 5 additions and 2 deletions

View File

@ -227,8 +227,8 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
}
$groups = false;
if (isset($context[static::GROUPS]) && \is_array($context[static::GROUPS])) {
$groups = $context[static::GROUPS];
if (isset($context[static::GROUPS]) && (\is_array($context[static::GROUPS]) || is_scalar($context[static::GROUPS]))) {
$groups = (array) $context[static::GROUPS];
} elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) {
return false;
}

View File

@ -92,6 +92,9 @@ class AbstractNormalizerTest extends TestCase
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('test')), false);
$this->assertEquals(array($a2, $a4), $result);
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => 'test'), false);
$this->assertEquals(array($a2, $a4), $result);
$result = $this->normalizer->getAllowedAttributes('c', array(AbstractNormalizer::GROUPS => array('other')), false);
$this->assertEquals(array($a3, $a4), $result);
}