When requesting data via a related link, the type parameter and links are generated from the parent entity, not the entity type of the related item.

E.g. A request to /api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7/field_item_instructions?_format=api_json should return items of type item_instruction--item_instruction, however the type is like so:

{
  "data": [
    {
      "type": "node--testlet_item",
      "id": "332b3d4f-dc64-4b27-ae0f-c410aea6a902",
      "attributes": {
        "id": "1",
        "uuid": "332b3d4f-dc64-4b27-ae0f-c410aea6a902",
        "langcode": "en",
        "title": "This is just an example.",
        "created": "1462476185",
        "changed": "1462476185",
        "default_langcode": "1",
        "content_translation_source": null,
        "content_translation_outdated": null,
        "content_translation_status": null,
        "content_translation_changed": null
      },
      "relationships": {
        "type": {
          "data": {
            "type": "item_instruction_type--item_instruction_type",
            "id": "2db5b75f-69e4-4fcb-865e-ba7dc66099f7"
          },
          "links": {
            "self": "http://rema.dev/api/node/testlet_item/332b3d4f-dc64-4b27-ae0f-c410aea6a902/relationships/type?_format=api_json",
            "related": "http://rema.dev/api/node/testlet_item/332b3d4f-dc64-4b27-ae0f-c410aea6a902/type?_format=api_json"
          }
        },
        "uid": {
          "data": {
            "type": "user--user",
            "id": "..."
          },
          "links": {
            "self": "http://rema.dev/api/node/testlet_item/.../relationships/uid?_format=api_json",
            "related": "http://rema.dev/api/node/testlet_item/.../uid?_format=api_json"
          }
        }
      },
      "links": {
        "self": "http://rema.dev/api/node/testlet_item/...?_format=api_json"
      }
    }
  ],
  "links": {
    "self": "http://rema.dev/api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7/field_item_instructions?q=es/api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7/field_item_instructions&_format=api_json"
  }
}

The issue is that the type is incorrectly listed as the type of the parent entity (node--testlet_item) and the links appear to be generated from that type as well.

Comments

gabesullice created an issue. See original summary.

e0ipso’s picture

Are you missing ... /related in the request url?

gabesullice’s picture

Perhaps... I've never fully understood that URL pattern, honestly. Regardless, I didn't hand write the request URL, instead I followed it from a link on the parent entity's document.

GET rema.dev/es/api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7?_format=api_json

{
  "data": {
      // skipped
    },
    "relationships": {
      "field_item_instructions": {
        "data": [
          {
            "type": "item_instruction--item_instruction",
            "id": "332b3d4f-dc64-4b27-ae0f-c410aea6a902"
          },
          {
            "type": "item_instruction--item_instruction",
            "id": "5d8117c6-aa92-467f-8ae9-0c56a7384700"
          }
        ],
        "links": {
          "self": "http://rema.dev/es/api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7/relationships/field_item_instructions?_format=api_json",
          "related": "http://rema.dev/es/api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7/field_item_instructions?_format=api_json" // <------- Followed this link
        }
      }
    },
    "links": {
      "self": "http://rema.dev/es/api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7?_format=api_json"
    }
  },
  "links": {
    "self": "http://rema.dev/es/api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7?q=es/api/node/testlet_item/03615d8c-cba4-42ea-97c8-2b0309f9f3f7&_format=api_json"
  }
}
gabesullice’s picture

To make the bug report a little more clear:

When visiting related resource link from a primary document, the related resource response should be a collection of the related resource objects.

For example, an article’s comments relationship could specify a link that returns a collection of comment resource objects when retrieved through a GET request.

The issue is that if one were to view http://example.com/articles/1/author (example from the specification) the response would return the resource object for the author, however the type property would incorrectly be "article", not "author".

Additionally, it appears that this is spilling into links on the related objects as well.

My intuition says that the CurrentContext service is probably blindly serving up the entity type of the parent, in turn causing the document normalizer to use the wrong entity type when serializing the type and links.

e0ipso’s picture

Please, disregard the comment in #2, I wasn't thinking clearly at the time.

I see what you mean now. Are the id and the rest of the attributes in the payload correct?

e0ipso’s picture

Title: [BUGFIX] Incorrect type property and links under relation paths » [BUGFIX] Use the correct type property and the correct relationship links

Off topic, sorry about all the issue title changes. I'm following the best practise that commit messages should be read as: Once this is merged this will… {Insert your commit message here}.

gabesullice’s picture

@e0ipso yes, the rest of the payload is correct. Also, that's a nice standard, I'll use that going forward.

e0ipso’s picture

Priority: Normal » Critical
e0ipso’s picture

Status: Active » Needs review
StatusFileSize
new3.91 KB

Patch and test. Merging if green.

Status: Needs review » Needs work

The last submitted patch, 9: 2793195--type-in-related-resources--9.patch, failed testing.

The last submitted patch, 9: 2793195--type-in-related-resources--9.patch, failed testing.

e0ipso’s picture

Status: Needs work » Needs review
StatusFileSize
new6.18 KB
new2.28 KB

Fixes a tangential test.

e0ipso’s picture

This issue uncovered a design flaw. We are setting the base resource config based on the route. One base route --> one base resource config.

This is fine with most implementations, but then Drupal. The issue is that a relationship can point to several resource types and therefore several resource configs. Since the relationships can become the base resource (via the related endpoint) we can have multiple resource config.

  • e0ipso committed 3e708a0 on 8.x-1.x
    Issue #2793195 by e0ipso: [BUGFIX] Use the correct type property and the...
e0ipso’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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