Skip to content

Commit 96fd131

Browse files
committed
fix identifier normalization group override
1 parent b7eca2d commit 96fd131

File tree

7 files changed

+19
-9
lines changed

7 files changed

+19
-9
lines changed

features/relation.feature

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ Feature: Relations support
8585
"@context": "/contexts/RelatedToDummyFriend",
8686
"@id": "/related_to_dummy_friends/dummyFriend=1;relatedDummy=1",
8787
"@type": "RelatedToDummyFriend",
88-
"name": "Friends relation"
88+
"name": "Friends relation",
89+
"dummyFriend": {
90+
"@id": "/dummy_friends/1",
91+
"@type": "DummyFriend",
92+
"name": "Zoidberg"
93+
}
8994
}
9095
"""
9196

@@ -100,7 +105,12 @@ Feature: Relations support
100105
"@context": "/contexts/RelatedToDummyFriend",
101106
"@id": "/related_to_dummy_friends/dummyFriend=1;relatedDummy=1",
102107
"@type": "RelatedToDummyFriend",
103-
"name": "Friends relation"
108+
"name": "Friends relation",
109+
"dummyFriend": {
110+
"@id": "/dummy_friends/1",
111+
"@type": "DummyFriend",
112+
"name": "Zoidberg"
113+
}
104114
}
105115
"""
106116

src/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public function create(string $resourceClass, string $property, array $options =
5656
foreach ($identifiers as $identifier) {
5757
if ($identifier === $property) {
5858
$propertyMetadata = $propertyMetadata->withIdentifier(true);
59-
$propertyMetadata = $propertyMetadata->withReadable(false);
6059
$propertyMetadata = $propertyMetadata->withWritable($doctrineClassMetadata->isIdentifierNatural());
6160

6261
break;

src/Bridge/Symfony/Bundle/Resources/config/doctrine_orm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
<!-- Metadata loader -->
6666

67-
<service id="api_platform.doctrine.orm.metadata.property.metadata_factory" class="ApiPlatform\Core\Bridge\Doctrine\Orm\Metadata\Property\DoctrineOrmPropertyMetadataFactory" decorates="api_platform.metadata.property.metadata_factory" decoration-priority="10" public="false">
67+
<service id="api_platform.doctrine.orm.metadata.property.metadata_factory" class="ApiPlatform\Core\Bridge\Doctrine\Orm\Metadata\Property\DoctrineOrmPropertyMetadataFactory" decorates="api_platform.metadata.property.metadata_factory" decoration-priority="40" public="false">
6868
<argument type="service" id="doctrine" />
6969
<argument type="service" id="api_platform.doctrine.orm.metadata.property.metadata_factory.inner" />
7070
</service>

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
193193
$propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $propertyName, $options);
194194

195195
if (
196-
(isset($context['jsonld_normalize']) && !$propertyMetadata->isIdentifier() && $propertyMetadata->isReadable()) ||
196+
(isset($context['jsonld_normalize']) && $propertyMetadata->isReadable()) ||
197197
(isset($context['jsonld_denormalize']) && $propertyMetadata->isWritable())
198198
) {
199199
$allowedAttributes[] = $propertyName;

src/Metadata/Property/Factory/SerializerPropertyMetadataFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ private function transformReadWrite(PropertyMetadata $propertyMetadata, string $
6868
{
6969
$groups = $this->getPropertySerializerGroups($resourceClass, $property);
7070

71-
if (false !== $propertyMetadata->isReadable()) {
71+
if ($propertyMetadata->isIdentifier()) {
72+
$propertyMetadata = $propertyMetadata->withReadable(null !== $normalizationGroups && !empty(array_intersect($normalizationGroups, $groups)));
73+
} elseif (false !== $propertyMetadata->isReadable()) {
7274
$propertyMetadata = $propertyMetadata->withReadable(null === $normalizationGroups || !empty(array_intersect($normalizationGroups, $groups)));
7375
}
7476
if (false !== $propertyMetadata->isWritable()) {

tests/Fixtures/TestBundle/Entity/DummyFriend.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class DummyFriend
3333
* @ORM\Column(type="integer")
3434
* @ORM\Id
3535
* @ORM\GeneratedValue(strategy="AUTO")
36-
* @Groups({"fakemanytomany"})
3736
*/
3837
private $id;
3938

tests/Fixtures/TestBundle/Entity/RelatedToDummyFriend.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Related To Dummy Friend represent an association table for a manytomany relation.
2222
*
23-
* @ApiResource()
23+
* @ApiResource(attributes={"normalization_context"={"groups": {"fakemanytomany"}}})
2424
* @ORM\Entity
2525
*/
2626
class RelatedToDummyFriend
@@ -31,6 +31,7 @@ class RelatedToDummyFriend
3131
* @ORM\Column
3232
* @Assert\NotBlank
3333
* @ApiProperty(iri="http://schema.org/name")
34+
* @Groups({"fakemanytomany"})
3435
*/
3536
private $name;
3637

@@ -46,7 +47,6 @@ class RelatedToDummyFriend
4647
* @ORM\Id
4748
* @ORM\ManyToOne(targetEntity="RelatedDummy", inversedBy="relatedToDummyFriend")
4849
* @ORM\JoinColumn(name="relateddummy_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
49-
* @Groups({"fakemanytomany"})
5050
*/
5151
private $relatedDummy;
5252

0 commit comments

Comments
 (0)