question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`meta` property in relationship is dropped when deserializing

See original GitHub issue

According to the spec, a relationship’s data can contain meta, i.e. jsonApiObject.data[*].relationships.<field>.data[*].meta can exists. (Also applies to singular data/response object)

Or more formally, according to the specification:

  1. Top Level contains data, which must be either single or array of “resource objects”
  2. Resource Objects may contain relationships, which is a “relationships object”
  3. (Each) Relationship Object contains data, which is a “resource linkage”
  4. Resource Linkage can be either single or array of “resource identifier object”
  5. Resource Identifier Objects may include meta

In my case which uses Jsona with Drupal, problem arises because Drupal uses individual relationship object’s meta to transmit image alt texts. As a result the alt text is lost.

As an example, this is a stripped JSON:API response from Drupal:

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
        "self": {
          "href": "http://jsonapi.org/format/1.0/"
        }
      }
    }
  },
  "data": [{
    "type": "node--site_configuration",
    "id": "f8895943-7f51-451b-bb8f-a479853f1b4b",
    "links": {
      "self": {
        "href": "http://acmecorp-cms.lndo.site/en/jsonapi/node/site_configuration/f8895943-7f51-451b-bb8f-a479853f1b4b?resourceVersion=id%3A230"
      }
    },
    "attributes": {
      "langcode": "en",
      "title": "Site Configuration"
    },
    "relationships": {
      "field_logo": {
        "data": {
          "type": "file--file",
          "id": "551ec1b9-b0c6-4649-bb7c-b6ebb09354ff",
          "meta": {
            "alt": "ACME Corp Logo",
            "title": "",
            "width": 206,
            "height": 278
          }
        },
        "links": {
          "related": {
            "href": "http://acmecorp-cms.lndo.site/en/jsonapi/node/site_configuration/f8895943-7f51-451b-bb8f-a479853f1b4b/field_logo?resourceVersion=id%3A230"
          },
          "self": {
            "href": "http://acmecorp-cms.lndo.site/en/jsonapi/node/site_configuration/f8895943-7f51-451b-bb8f-a479853f1b4b/relationships/field_logo?resourceVersion=id%3A230"
          }
        }
      }
    }
  }],
  "included": [{
    "type": "file--file",
    "id": "551ec1b9-b0c6-4649-bb7c-b6ebb09354ff",
    "links": {
      "self": {
        "href": "http://acmecorp-cms.lndo.site/en/jsonapi/file/file/551ec1b9-b0c6-4649-bb7c-b6ebb09354ff"
      }
    },
    "attributes": {
      "langcode": "en",
      "uri": {
        "value": "public://2020-07/acmecorp-logo-colour-2x.png",
        "url": "http://acmecorp.oss-cn-hongkong.aliyuncs.com/s3fs-public/2020-07/acmecorp-logo-colour-2x.png"
      },
      "filemime": "image/png",
      "filesize": 54952
    }
  }]
}

Notice there is alt: "ACME Corp Logo" in meta object in data[0].relationships.field_logo.data.

If we try to parse it with a simple Jsona code, i.e.

const Jsona = require('jsona').default;

const dataFormatter = new Jsona();

const data = require('./drupal-jsonapi-response.json');

console.log(JSON.stringify(dataFormatter.deserialize(data), null, 2));

The output is

[
  {
    "type": "node--site_configuration",
    "id": "f8895943-7f51-451b-bb8f-a479853f1b4b",
    "langcode": "en",
    "title": "Site Configuration",
    "links": {
      "self": {
        "href": "http://acmecorp-cms.lndo.site/en/jsonapi/node/site_configuration/f8895943-7f51-451b-bb8f-a479853f1b4b?resourceVersion=id%3A230"
      }
    },
    "field_logo": {
      "type": "file--file",
      "id": "551ec1b9-b0c6-4649-bb7c-b6ebb09354ff",
      "langcode": "en",
      "uri": {
        "value": "public://2020-07/acmecorp-logo-colour-2x.png",
        "url": "http://acmecorp.oss-cn-hongkong.aliyuncs.com/s3fs-public/2020-07/acmecorp-logo-colour-2x.png"
      },
      "filemime": "image/png",
      "filesize": 54952,
      "links": {
        "self": {
          "href": "http://acmecorp-cms.lndo.site/en/jsonapi/file/file/551ec1b9-b0c6-4649-bb7c-b6ebb09354ff"
        }
      }
    },
    "relationshipNames": [
      "field_logo"
    ]
  }
]

As seen, the meta object along with alt: "ACME Corp Logo" is gone.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
olosegrescommented, Jul 22, 2020

@ericwong3 Sorry, I got confused and added meta processing at the wrong level. Meta that you want will store in resourceIdObjMeta field. Upgrade to 1.8.0 please

0reactions
coatezycommented, Oct 28, 2020

Hey @olosegres! Thanks for checking! 😄 As per my latest comment, I’ve found the issue and I can get the resourceIdObjMeta to work. My issue is probably due to the way that I’m trying to use the JSON API to create reusable fragments to mock scenarios in a mock API to carry out E2E tests in a native application.

I’ve attached a better example so you can see my data object. You will notice that user2 relation contains the resourceIdObjMeta attribute, but user1 does not as it is being included in another relationship one level higher without the meta attribute being set.

https://runkit.com/5f99eba543333e001aae7d26/5f99ebb0508e820013d737a8

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON:API - meta inside data is lost · Issue #3419 - GitHub
With the move towards JSON API as Ember Data's serialization format ... People that are using the meta property for data unrelated to...
Read more >
Django Rest Framework Relationships Serialization
ModelSerializer): class Meta: model = Skill fields = ('user', 'skill', 'level') class ProfileReadSerializer(serializers.
Read more >
JSON:API — Latest Specification (v1.1)
data : resource linkage; meta : a meta object that contains non-standard meta-information about the relationship. a member defined by an ...
Read more >
Serializer relations - Django REST framework
When using the ModelSerializer class, serializer fields and relationships will be ... StringRelatedField(many=True) class Meta: model = Album fields ...
Read more >
Serializers - Django REST framework - Tom Christie
Specifying nested serialization ... The depth option should be set to an integer value that indicates the depth of relationships that should be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found