What's the problem?

When doing a POST by using REST, you usually expect to get message(like entity id and entity type id) to know if they were inserted and know the entity id created as a successful result, but currently instead of returning this, it does return NULL.

Marking this issue as needs review.

Comments

edutrul created an issue. See original summary.

edutrul’s picture

StatusFileSize
new925 bytes

Done! patch to return entity type & entity id when doing a successful POST

berdir’s picture

Status: Active » Needs review
nightwalkr’s picture

I do support this feature! should be included in drupal 8 core

larowlan’s picture

I think the location header had this info?

berdir’s picture

Only if you assume that it contains the entity type/ID and it could be different for some entity types. And you could have aliases, for example...

heilop’s picture

It is very interesting, I think in some cases we will need the entity id.

klausi’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Makes sense, as far as I can see it is allowed to return a body with a 201 response. Should we just return the full newly created entity representation, the same that we would return on GET requests? That way API consumers don't need to learn a new format, they just get the same entity they already know from GET requests.

And this needs a test case, should be added to the POST create tests.

We should also update the comment in the code, which wrongly says that 201 responses are empty.

edutrul’s picture

StatusFileSize
new1.89 KB

Thanks a lot @klausi for checking it

ok Added 546216-return-entity-after-post-rest.patch which contains:

  1. Updated comment,
  2. Return entity object.
  3. Updated test case for this POST

Waiting for review/feedback

edutrul’s picture

Title: Return entity type & entity id after successfull POST in rest » Return entity object after successfull POST in rest
Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 9: return_entity_type-2546216-9.patch, failed testing.

edutrul’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new1.99 KB

Patch from comment #9 failed because I needed to desarialize object in order to compare with $entity_type
Here's patch again with the fix.

Status: Needs review » Needs work

The last submitted patch, 12: return_entity_object-2546216-12.patch, failed testing.

-enzo-’s picture

StatusFileSize
new2.13 KB

Test was updated to validate serialized UUID against response UUID, if is the same means the entity was process successfully

-enzo-’s picture

StatusFileSize
new2.13 KB

Removed extra line

edutrul’s picture

Status: Needs work » Needs review
-enzo-’s picture

clemens.tolboom’s picture

+++ b/core/modules/rest/src/Tests/CreateTest.php
@@ -361,8 +361,13 @@ public function createAccountPerEntity($entity_type) {
+    ¶

Whitespace

klausi’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
    @@ -107,9 +107,9 @@ public function post(EntityInterface $entity = NULL) {
    +      // 201 Created response have the new entity created.
    

    Should be "201 Created responses return the newly created entity in the response body."

  2. +++ b/core/modules/rest/src/Tests/CreateTest.php
    @@ -361,8 +361,13 @@ public function createAccountPerEntity($entity_type) {
    +    // Validate is object UUID send is the same returned
    

    Should be "Make sure that the response includes an entity in the body and check the UUID as an example."

+++ b/core/modules/rest/src/Tests/CreateTest.php
@@ -361,8 +361,13 @@ public function createAccountPerEntity($entity_type) {
+    $request = \Drupal\Component\Serialization\Json::decode($serialized);
+    $response = \Drupal\Component\Serialization\Json::decode($response);

Please move the namespaces to "use" statements.

fortis’s picture

Status: Needs work » Needs review
StatusFileSize
new2.13 KB

we already have "use Drupal\Component\Serialization\Json;"
fixed php comments and used Json::decode

fortis’s picture

StatusFileSize
new2.13 KB

wrong comment number in filename, renamed

Status: Needs review » Needs work

The last submitted patch, 21: return_entity_object-2546216-21.patch, failed testing.

The last submitted patch, 20: return_entity_object-2546216-16.patch, failed testing.

klausi’s picture

Issue tags: -Needs tests
  1. +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
    @@ -107,9 +107,9 @@ public function post(EntityInterface $entity = NULL) {
    +      // 201 Created responses return the newly created entity in the response body.
    

    Comments should wrap at 80 characters, see https://www.drupal.org/coding-standards/docs#drupal

  2. +++ b/core/modules/rest/src/Tests/CreateTest.php
    @@ -361,8 +361,13 @@ public function createAccountPerEntity($entity_type) {
    +    // Make sure that the response includes an entity in the body and check the UUID as an example.
    

    same here.

The last submitted patch, 20: return_entity_object-2546216-16.patch, failed testing.

fortis’s picture

Status: Needs work » Needs review
StatusFileSize
new2.15 KB

oops, fixed

klausi’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
    @@ -107,9 +107,10 @@ public function post(EntityInterface $entity = NULL) {
    +      // 201 Created responses return the newly created entity in the
    +      // response body.
    

    "response" still fits on the line before, let's use as much of the 80 character line length as possible.

  2. +++ b/core/modules/rest/src/Tests/CreateTest.php
    @@ -361,8 +361,14 @@ public function createAccountPerEntity($entity_type) {
    +    // Make sure that the response includes an entity in the body and check
    +    // the UUID as an example.
    

    same here, the "the" still fits on the line before.

The last submitted patch, 27: return_entity_object-2546216-26.patch, failed testing.

fortis’s picture

Status: Needs work » Needs review
StatusFileSize
new2.15 KB

Status: Needs review » Needs work

The last submitted patch, 30: return_entity_object-2546216-30.patch, failed testing.

fortis’s picture

Status: Needs work » Needs review

SimpleTest passed

klausi’s picture

Status: Needs review » Reviewed & tested by the community

Looks good!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 30: return_entity_object-2546216-30.patch, failed testing.

Status: Needs work » Needs review
wim leers’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +API-First Initiative

This would be a huge win for the REST experience.

wim leers’s picture

Version: 8.0.x-dev » 8.1.x-dev

But probably should go in 8.1.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

This needs a change record, so marking needs work for that, but otherwise looks good.

wim leers’s picture

Title: Return entity object after successfull POST in rest » Return entity object in REST response body after successful POST

Better title too.

wim leers’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record

  • catch committed e50aac4 on
    Issue #2546216 by fortis, edutrul, -enzo-: Return entity object in REST...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.1.x, thanks!

wim leers’s picture

Issue tags: +Performance

YAY! This is a huge win in terms of RX and perf. Glad to see this in.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.