Skip to content

Commit 966a5eb

Browse files
committed
Allow updating nested resource on POST
Fixes #1624, #1363, #427
1 parent a62e089 commit 966a5eb

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

features/hydra/error.feature

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,38 @@ Feature: Error handling
103103
And the JSON node "hydra:description" should be equal to "Update is not allowed for this operation."
104104
And the JSON node "trace" should exist
105105

106-
Scenario: Get an error during update of an existing relation with a non-allowed update operation
106+
@dropSchema
107+
@createSchema
108+
Scenario: Do not get an error during update of an existing relation with a non-allowed update operation
109+
When I add "Content-Type" header equal to "application/ld+json"
110+
Given I send a "POST" request to "/related_dummies" with body:
111+
"""
112+
{
113+
"@type": "https://schema.org/Product",
114+
"symfony": "laravel"
115+
}
116+
"""
117+
Then the response status code should be 201
118+
And the response should be in JSON
119+
And the JSON node "@id" should be equal to "/related_dummies/1"
120+
And the JSON node "symfony" should be equal to "laravel"
121+
107122
When I add "Content-Type" header equal to "application/ld+json"
108123
And I send a "POST" request to "/relation_embedders" with body:
109124
"""
110125
{
111126
"anotherRelated": {
112-
"@id": "/related_dummies/2",
127+
"@id": "/related_dummies/1",
113128
"@type": "https://schema.org/Product",
114129
"symfony": "phalcon"
115130
}
116131
}
117132
"""
118-
Then the response status code should be 400
133+
Then the response status code should be 201
119134
And the response should be in JSON
120135
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
121-
And the JSON node "@context" should be equal to "/contexts/Error"
122-
And the JSON node "@type" should be equal to "hydra:Error"
123-
And the JSON node "hydra:title" should be equal to "An error occurred"
124-
And the JSON node "hydra:description" should be equal to "Update is not allowed for this operation."
125-
And the JSON node "trace" should exist
136+
And the JSON node "@context" should be equal to "/contexts/RelationEmbedder"
137+
And the JSON node "@type" should be equal to "RelationEmbedder"
138+
And the JSON node "@id" should be equal to "/relation_embedders/1"
139+
And the JSON node "anotherRelated.@id" should be equal to "/related_dummies/1"
140+
And the JSON node "anotherRelated.symfony" should be equal to "phalcon"

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public function denormalize($data, $class, $format = null, array $context = [])
9898
{
9999
// Avoid issues with proxies if we populated the object
100100
if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
101-
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']) {
101+
if (isset($context['api_allow_update']) && true !== $context['api_allow_update']
102+
&& (!isset($context['api_resource_is_relation']) || true !== $context['api_resource_is_relation'])
103+
) {
102104
throw new InvalidArgumentException('Update is not allowed for this operation.');
103105
}
104106

src/Serializer/AbstractItemNormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ private function denormalizeRelation(string $attributeName, PropertyMetadata $pr
297297
($propertyMetadata->isWritableLink() && \is_array($value))
298298
) {
299299
$context['resource_class'] = $className;
300+
$context['api_resource_is_relation'] = true;
300301

301302
return $this->serializer->denormalize($value, $className, $format, $context);
302303
}

0 commit comments

Comments
 (0)