Support from Acquia helps fund testing for Drupal Acquia logo

Comments

e0ipso created an issue. See original summary.

milopca’s picture

Assigned: Unassigned » milopca
milopca’s picture

Assigned: milopca » Unassigned
Status: Active » Needs review
Issue tags: +Dublin2016
FileSize
1.76 KB

Patch for adding Location in response headers.

Status: Needs review » Needs work

The last submitted patch, 3: feature_add_a-2806097-3.patch, failed testing.

The last submitted patch, 3: feature_add_a-2806097-3.patch, failed testing.

e0ipso’s picture

Thanks for the patch @milopca!

A very small nit pick:

+++ b/src/RequestHandler.php
@@ -197,7 +200,8 @@ class RequestHandler extends RestRequestHandler {
+          array('Content-Type' => $request->getMimeType($format), 'Location' => $request->getUri());

Can we have the short version? [] instead of array()

Also:

  1. Tests seem to be failing.
  2. Can we have test coverage to check that the Location header matches the self link in the JSON response?
milopca’s picture

Fix the broken test and change array to the small version []
Test for this path is still pending

e0ipso’s picture

Status: Needs work » Needs review

Kicking the testbot.

milopca’s picture

Assigned: milopca » Unassigned
e0ipso’s picture

Status: Needs review » Needs work
Issue tags: -Novice, -Dublin2016

It seems that this patch is not adding the header after creating the entity. This is not a novice patch, cleaning up tags.

Spleshka’s picture

I believe that JSON API specification compliance issue should be resolved. Attached the patch with test.

e0ipso’s picture

Thanks @Spleshka for rejoining this effort.

Spleshka’s picture

My pleasure. FIY: tests failed because of the issue not related to the patch.

Status: Needs review » Needs work
Spleshka’s picture

The further progress is blocked by #2877589: Fix failing test in the dev branch, because tests fail.

Spleshka’s picture

Status: Needs work » Needs review

The blocker is cleared now, submitting the patch for re-testing.

Spleshka’s picture

Now all tests pass, awaiting for manual review.

e0ipso’s picture

Status: Needs review » Fixed

This looks good! Merging.

  • e0ipso committed c8c248f on 8.x-1.x authored by Spleshka
    feat(Spec): Add a location header after creating an entity (#2806097 by...
Wim Leers’s picture

This patch is incorrect, because it's not taking into account the fact that not every entity type has a canonical URL.

See #2853211: EntityResource::post() incorrectly assumes that every entity type has a canonical URL for Drupal core.

Spleshka’s picture

Assigned: Unassigned » Spleshka

Very good point, thanks @Wim. I'll provide a new patch.

clemens.tolboom’s picture

(my 2 cents)

Weird as this committed patch give a location to http://drupal.d8/node/16 but the redirection resolves to the expected application/vnd.api+json response.

curl
  --include
  --user api:api
  --header 'Accept: application/vnd.api+json'
  --header 'Content-type: application/vnd.api+json'
  --request POST http://drupal.d8/jsonapi/node/article --data-binary @jsonapi-node-article-user.json
HTTP/1.1 201 Created
Date: Sat, 13 May 2017 10:37:09 GMT
Server: Apache/2.4.18 (Unix) PHP/5.6.29
X-Content-Type-Options: nosniff
X-Powered-By: PHP/5.6.29
Cache-Control: must-revalidate, no-cache, private
Location: http://drupal.d8/node/16 <=================================

Fetch that location manually breaks of course
curl --header 'Accept: application/vnd.api+json' --request GET http://drupal.d8/node/16

Not sure what this means.

Spleshka’s picture

Yet another good point, thanks @clemens.tolboom. Trying to bring all points together:

1. I assume that when an entity doesn't have a canonical URL we should not send Location header. However, specification explicitly says:

The response SHOULD include a Location header identifying the location of the newly created resource.

Probably empty Location header doesn't make sense here, as it might bring some confusion into DX. Any other suggestions?

2. Very good question in #22. Specification says that Location should identify the location of the newly created resource. The correct Drupal location is entity's canonical URL, which doesn't give any value for frontend apps using JSON API. So I suppose we should provide JSONAPI-friendly url to fetch an entity. It sort of kills issue #1 above (because there is no need to check for canonical url), but potentially brings in additional permission check to view the entity (or no need?).

3. From the resource POSTing specification:

If the resource object returned by the response contains a self key in its links member and a Location header is provided, the value of the self member MUST match the value of the Location header.

This confirms my assumption in #3 - Location URL should match ['links']['self'], which currently provides json api-friendly endpoint to fetch the resource.

Any comments / notes before I move on with the implementation?

e0ipso’s picture

Ah yes! Sorry, I should not have committed that patch. Late commits are never advisable :P

What I should have said there (and did in Dublin initially and forgot later) is that we want the resource entity link. Not the canonical entity link.

We have a link manager service that should help with the task.

Thanks Wim for reopening, and everyone for your contributions so far. In any case, I do not regret giving @Spleshka a commit after the fantastic work he's doing with the module :-D even if I jumped the gun.

Spleshka’s picture

Yeah, thanks everyone for the feedback! Attaching the patch for the most thorough review ever :P

Status: Needs review » Needs work
Spleshka’s picture

Updated dependent tests as well.

e0ipso’s picture

I'm wondering what happens if we drop the entity repository…

  • e0ipso committed bf25f78 on 8.x-1.x authored by Spleshka
    fix(Spec): Fix the location header after creating an entity (#2806097 by...
Spleshka’s picture

Actually nothing bad will happen :) I was going to create a follow up to get rid of it after this issue is done, to avoid patch conflicts. But we can get rid of it right now as well if you want.

Spleshka’s picture

Oh, it's already done :) thanks a lot Mateu and other guys for getting this issue resolved.

e0ipso’s picture

Status: Needs review » Fixed

Finally fixed! Thanks everyone.

Wim Leers’s picture

Note that this is exactly why I argued a long time ago that the JSON API module should offer new link relation types. Then this could would have been far more elegant, and this mistake would likely have been prevented.
That can still happen at a later time of course.

Finally, test coverage is far too weak here. This should have a hardcoded relative URL, then the problem would have been obvious. Furthermore, there is no test coverage for entity types without a canonical URL: in core REST there would not be a Location response header, but for JSON API there would be.

All things that could (and should!) be addressed later.

e0ipso’s picture

Note that this is exactly why I argued a long time ago that the JSON API module should offer new link relation types. Then this could would have been far more elegant, and this mistake would likely have been prevented.
That can still happen at a later time of course.

1. Can we have a follow up ticket to capture that? I agree it can be interesting the have Drupal-entities aware of the link to their JSON-API-resource-entity equivalent.

Finally, test coverage is far too weak here. This should have a hardcoded relative URL, then the problem would have been obvious.

2. I'm not sure I agree 100%. We want the Location to be the same as the self link by design. The self link is tested agains a hardcoded string elsewhere (some of them anyways).

Furthermore, there is no test coverage for entity types without a canonical URL: in core REST there would not be a Location response header, but for JSON API there would be.

3. Not sure I understand.

Wim Leers’s picture

1. I brought this up in #2829746: Clean up \Drupal\jsonapi\LinkManager\LinkManager(Interface) before, but that's been closed since.

2. Yep, we do want it to match the JSON API self link. Maybe that's tested against a hardcoded URL somewhere. But in the patches committed here, that was definitely not the case. Otherwise this bug would never have been introduced, because the problem would've been obvious. That's the value of seeing hardcoded strings in your test expectations.

3. Imagine that entity type foo does not have a canonical URL. That means https://d8/foo/5 does not exist. But https://d8/jsonapi/foo/5 will exist, because JSON API generates a "individual JSON API resource" URL for every entity, even if it's an entity of an entity type with no canonical link relation type.

e0ipso’s picture

WRT 1. We have now #2878463: [PP-1] Define a JSON API link relation for entities.

WRT 2. That's a good point. It would have been easier to spot. However testing absolute links is not super easy (b/c base_url differences, url prefix variations, …). In any case we could do regular expression matching. Feel free to create a new issue if you feel preg_match is a good way to test this. How are you testing them in REST core?

WRT 3. I understand the link situation. I don't understand the reference to lack of test coverage. Is there any action item we should capture here?

Status: Fixed » Closed (fixed)

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