loopback-component-jsonapi icon indicating copy to clipboard operation
loopback-component-jsonapi copied to clipboard

Nested request returns object with incorrect "self" relation

Open pnw opened this issue 8 years ago • 3 comments

If I make a request to http://localhost:3000/api/users/1/posts, I get the following response:

{
  "links": {
    "self": "http://localhost:3000/api/users/1/posts" // correct
  },
  "data": [
    {
      "type": "user", // this should be "post"
      "relationships": {
        "user": {
          "links": {
            "related": "http://localhost:3000/api/posts/99/user"  // correct
          }
        }
      },
      "id": "99",
      "attributes": {
        "some": "attributes"
      },
      "links": {
        "self": "http://localhost:3000/api/users/99"  // this should be "http://localhost:3000/api/posts/99"
      }
    }
  ]
}

Note the error in the URL: "http://localhost:3000/api/users/99"

The id segment is correct (this is the self link for post 99), but the model-name segment refers to the parent resource, "users", instead of the child resource, "posts"

pnw avatar Oct 06 '16 21:10 pnw

I'm confused by this. It's the self link in the "user" entry.

csprocket777 avatar Apr 21 '17 00:04 csprocket777

I think that part of the issue is that the type key in this example is also incorrect @csprocket777. A request to http://localhost:3000/api/users/1/posts should only return data with "type": "post". That is probably the reason why the self link is getting generated incorrectly, it is probably getting built from pluralising the type.

markstuart avatar Apr 21 '17 09:04 markstuart

yeah as @markstuart says its not just the "self" but also the "type" that's wrong. I've updated the issue text to highlight this

digitalsadhu avatar Apr 21 '17 10:04 digitalsadhu