laravel-json-api/laravel

Location header missing on created resources without self link

lewisjlusted opened this issue · 2 comments

Issue:

The Location header is missing from the response when a resource is created with selfLink set to false on the schema.

Context:

I came across this when using the assertCreatedWithServerId function for testing. It turns out, the Location header is not set if selfLink is set to false on a resources schema.

It looks like the Location header is only appended in the ResourceResponse if both of the following conditions are met:

  • resourceWasCreated evaluates to true
  • the resource selfUrl exists

..but inside JsonApiResource, the selfUrl function will return null if the schema has selfLink set to false.

Seeing as the JSON:API Spec (9.1.2.1 201 Created) suggests;

  • The Location header SHOULD be included
  • IF the response object contains a self link, it should match the Location header

It seems like the Location header should be included, even if the self link is not included in the response object.

Feel free to disregard this if this is by design, or I've misunderstood the spec!

What's the use case for not including the self link on the resource, but wanting a Location header for that link? If the resource can be fetched from that URL, then it should have the self link on it?

The Laravel JSON:API docs say:

If you have a resource that is not retrievable by its resource identifier, then you must disable the self link.

I.e. it's only meant to be disabled if you cannot fetch it using that link, which would also mean the Location header shouldn't exist because the value of that header would not work.

Closing this due to lack of response.

If anyone wants this issue re-opened, I need an example use case for why the link is being disabled but the Location header is needed, as the link shouldn't be disabled if the resource is retrievable by its id.