Skip to content

Commit

Permalink
Allow serializing when property and method start from "is"
Browse files Browse the repository at this point in the history
  • Loading branch information
maks-rafalko committed Mar 9, 2021
1 parent a32b677 commit ef4731a
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 92 deletions.
30 changes: 30 additions & 0 deletions features/serializer/dummy_boolean.feature
@@ -0,0 +1,30 @@
Feature: Serialization of boolean properties
In order to use an hypermedia API
As a client software developer
I need to be able get boolean properties with is* methods

@createSchema
Scenario: Create a dummy boolean resource
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/dummy_booleans" with body:
"""
{
"isDummyBoolean": true
}
"""
Then the response status code should be 201
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the header "Content-Location" should be equal to "/dummy_booleans/1"
And the header "Location" should be equal to "/dummy_booleans/1"
And the JSON should be equal to:
"""
{
"@context": "/contexts/DummyBoolean",
"@id": "/dummy_booleans/1",
"@type": "DummyBoolean",
"id": 1,
"isDummyBoolean": true,
"dummyBoolean": true
}
"""
5 changes: 4 additions & 1 deletion src/Serializer/AbstractItemNormalizer.php
Expand Up @@ -517,7 +517,10 @@ protected function denormalizeRelation(string $attributeName, PropertyMetadata $
*/
protected function getFactoryOptions(array $context): array
{
$options = [];
$options = [
/* @see https://github.com/symfony/symfony/blob/v5.1.0/src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php */
'enable_getter_setter_extraction' => true,
];

if (isset($context[self::GROUPS])) {
/* @see https://github.com/symfony/symfony/blob/v4.2.6/src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php */
Expand Down
18 changes: 12 additions & 6 deletions tests/GraphQl/Serializer/ItemNormalizerTest.php
Expand Up @@ -76,13 +76,15 @@ public function testNormalize()
$dummy = new Dummy();
$dummy->setName('hello');

$defaultOptions = ['enable_getter_setter_extraction' => true];

$propertyNameCollection = new PropertyNameCollection(['name']);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, $defaultOptions)->willReturn($propertyNameCollection);

$propertyMetadata = new PropertyMetadata(null, null, true);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadata);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', $defaultOptions)->willReturn($propertyMetadata);

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1');
Expand Down Expand Up @@ -131,13 +133,15 @@ public function testNormalizeNoResolverData(): void
$dummy = new Dummy();
$dummy->setName('hello');

$defaultOptions = ['enable_getter_setter_extraction' => true];

$propertyNameCollection = new PropertyNameCollection(['name']);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, $defaultOptions)->willReturn($propertyNameCollection);

$propertyMetadata = new PropertyMetadata(null, null, true);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadata);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', $defaultOptions)->willReturn($propertyMetadata);

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1');
Expand Down Expand Up @@ -181,13 +185,15 @@ public function testDenormalize()
{
$context = ['resource_class' => Dummy::class, 'api_allow_update' => true];

$defaultOptions = ['enable_getter_setter_extraction' => true];

$propertyNameCollection = new PropertyNameCollection(['name']);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection)->shouldBeCalled();
$propertyNameCollectionFactoryProphecy->create(Dummy::class, $defaultOptions)->willReturn($propertyNameCollection)->shouldBeCalled();

$propertyMetadata = new PropertyMetadata(null, null, true, true);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn($propertyMetadata)->shouldBeCalled();
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', $defaultOptions)->willReturn($propertyMetadata)->shouldBeCalled();

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);

Expand Down
26 changes: 16 additions & 10 deletions tests/Hal/Serializer/ItemNormalizerTest.php
Expand Up @@ -112,15 +112,17 @@ public function testNormalize()
$dummy->setName('hello');
$dummy->setRelatedDummy($relatedDummy);

$defaultOptions = ['enable_getter_setter_extraction' => true];

$propertyNameCollection = new PropertyNameCollection(['name', 'relatedDummy']);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, $defaultOptions)->willReturn($propertyNameCollection);

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn(
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', $defaultOptions)->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true)
);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', $defaultOptions)->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class), '', true, false, false)
);

Expand Down Expand Up @@ -179,15 +181,17 @@ public function testNormalizeWithoutCache()
$dummy->setName('hello');
$dummy->setRelatedDummy($relatedDummy);

$defaultOptions = ['enable_getter_setter_extraction' => true];

$propertyNameCollection = new PropertyNameCollection(['name', 'relatedDummy']);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn($propertyNameCollection);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, $defaultOptions)->willReturn($propertyNameCollection);

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', [])->willReturn(
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', $defaultOptions)->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true)
);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', [])->willReturn(
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummy', $defaultOptions)->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class), '', true, false, false)
);

Expand Down Expand Up @@ -261,18 +265,20 @@ public function testMaxDepth()
$level3->name = 'level 3';
$level2->child = $level3;

$defaultOptions = ['enable_getter_setter_extraction' => true];

$propertyNameCollection = new PropertyNameCollection(['id', 'name', 'child']);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(MaxDepthDummy::class, [])->willReturn($propertyNameCollection);
$propertyNameCollectionFactoryProphecy->create(MaxDepthDummy::class, $defaultOptions)->willReturn($propertyNameCollection);

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(MaxDepthDummy::class, 'id', [])->willReturn(
$propertyMetadataFactoryProphecy->create(MaxDepthDummy::class, 'id', $defaultOptions)->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_INT), '', true)
);
$propertyMetadataFactoryProphecy->create(MaxDepthDummy::class, 'name', [])->willReturn(
$propertyMetadataFactoryProphecy->create(MaxDepthDummy::class, 'name', $defaultOptions)->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), '', true)
);
$propertyMetadataFactoryProphecy->create(MaxDepthDummy::class, 'child', [])->willReturn(
$propertyMetadataFactoryProphecy->create(MaxDepthDummy::class, 'child', $defaultOptions)->willReturn(
new PropertyMetadata(new Type(Type::BUILTIN_TYPE_OBJECT, false, MaxDepthDummy::class), '', true, false, true)
);

Expand Down

0 comments on commit ef4731a

Please sign in to comment.