Skip to content

Allow serializing when property and method start from "is" #4125

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

Closed
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ jobs:
- name: Clear test app cache
run: tests/Fixtures/app/console cache:clear --ansi
- name: Run Behat tests
run: vendor/bin/behat --out=std --format=progress --profile=default --no-interaction
run: vendor/bin/behat --out=std --format=progress --profile=default --no-interaction --tags '~@!lowest'

postgresql:
name: Behat (PHP ${{ matrix.php }}) (PostgreSQL)
Expand Down
31 changes: 31 additions & 0 deletions features/serializer/boolean_properties.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@!lowest
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
Original file line number Diff line number Diff line change
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if this should be opt-in, does this break existing tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it doesn't break existing tests. However it can be considered as a BC break.

];

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading