Skip to content

Commit b14ee45

Browse files
committed
Allow updating nested resource on POST
Fixes api-platform#1624, api-platform#1363, api-platform#427
1 parent d7f36eb commit b14ee45

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

features/hydra/error.feature

+25-8
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Feature: Error handling
8585
And the JSON node "hydra:description" should exist
8686
And the JSON node "trace" should exist
8787

88+
@dropSchema
8889
Scenario: Get an error during update of an existing resource with a non-allowed update operation
8990
When I add "Content-Type" header equal to "application/ld+json"
9091
And I send a "POST" request to "/dummies" with body:
@@ -103,23 +104,39 @@ Feature: Error handling
103104
And the JSON node "hydra:description" should be equal to "Update is not allowed for this operation."
104105
And the JSON node "trace" should exist
105106

106-
Scenario: Get an error during update of an existing relation with a non-allowed update operation
107+
@createSchema
108+
Scenario: Populate database with related dummies. Check that id will be "/related_dummies/1"
109+
Given I add "Content-Type" header equal to "application/ld+json"
110+
And 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+
122+
@dropSchema
123+
Scenario: Do not get an error during update of an existing relation with a non-allowed update operation
107124
When I add "Content-Type" header equal to "application/ld+json"
108125
And I send a "POST" request to "/relation_embedders" with body:
109126
"""
110127
{
111128
"anotherRelated": {
112-
"@id": "/related_dummies/2",
129+
"@id": "/related_dummies/1",
113130
"@type": "https://schema.org/Product",
114131
"symfony": "phalcon"
115132
}
116133
}
117134
"""
118-
Then the response status code should be 400
135+
Then the response status code should be 201
119136
And the response should be in JSON
120137
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
138+
And the JSON node "@context" should be equal to "/contexts/RelationEmbedder"
139+
And the JSON node "@type" should be equal to "RelationEmbedder"
140+
And the JSON node "@id" should be equal to "/relation_embedders/1"
141+
And the JSON node "anotherRelated.@id" should be equal to "/related_dummies/1"
142+
And the JSON node "anotherRelated.symfony" should be equal to "phalcon"

src/Serializer/AbstractItemNormalizer.php

+1
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_allow_update'] = true;
300301

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

0 commit comments

Comments
 (0)