Problem/Motivation

Our current serializer option appears to drop anything other than the data property. This means that we lose access to things like pagination metadata.

Steps to reproduce

* Create a JsonApiClient instance that uses Jsona as a serializer.
* Make a request to a data set that would typically be paginated (or force it with something like `?page[limit]=5`
* Notice that links.pagination is not returned.

Proposed resolution

It should be possible today to use the `rawResponse` option to retrieve this data when using a serializer, but it seems like it would be much friendlier to also provide this metadata when deserializing.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork api_client-3426518

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

brianperry created an issue. See original summary.

brianperry’s picture

Parent issue: » #3440572: [Meta] 1.x Roadmap
brianperry’s picture

brianperry’s picture

Priority: Normal » Major
wotnak’s picture

The problem is with a specific serializer, in this case, Jsona.
For someone with a similar problem, the Jsona maintainer suggested extending the Jsona class to override the deserialize function. https://github.com/olosegres/jsona/issues/22#issuecomment-531164105
This would, however, require providing that overridden class in the json-api-client package.

Something like this could work:

import Jsona from "jsona";
export default class JsonaWithLinks extends Jsona {
  deserialize(body, options) {
    const data = super.deserialize(body, options);
    // Include top level links object only for collections to not override single resource links.
    if (Array.isArray(body.data) && body.links) {
      data.links = body.links;
    }
    return data;
  }
}

The same thing could be done for the meta top level object to, for example, make it easier to use total count info provided by the JSON:API Extras.

balintbrews’s picture

@brianperry — We have this solution ready for Canvas Code Component users: #3570305: Make links and meta available by default in data returned by the preconfigured JsonApiClient. Do you think we should move this to the API Client? We could add jsona as a peerDependency and mark it as optional in peerDependenciesMeta.

brianperry’s picture

Yes, I think this would be a nice addition to the library.

We kind of have a pattern for this for the default cache in the utils package: https://git.drupalcode.org/project/api_client/-/blob/canary/packages/uti...

We then re-export this in the jsonapi client, which allows an instance of the client with a cache to be created like this:

const client = new JsonApiClient(
  "https://drupal-api-demo.party", {
    cache: createCache(),
  }
);

If we were to do something similar for a deserializer I think it would be porting `JsonaWithMetaAndLinks` from https://www.drupal.org/project/canvas/issues/3570305 to the utils package. We should also maybe rename it to something like `defaultDeserializer` in case we continue to enhance it in the future.

As far as treating it as a peer dep, open to opinions here. This is useful enough and jsona is small enough that I wonder if we should just make it a dependency.

wotnak’s picture

Assigned: Unassigned » wotnak

brianperry’s picture

Status: Active » Needs review

Opened an MR with a first pass at these changes. Would this address the Canvas use case?

Not sure where the community landed on AI contributions, but will disclose that this work was AI assisted.

brianperry’s picture

Unsure about the test failures here. They are passing for me locally. Node version mismatch is probably my first uneducated guess.

wotnak’s picture

Assigned: wotnak » Unassigned
Status: Needs review » Reviewed & tested by the community

Looks good to me. It would address the Canvas use case.

I can reproduce test failures locally on Node v24, but updating Vitest to the current v4 version fixes it (with Vitest v4 tests also pass after reverting https://git.drupalcode.org/project/api_client/-/commit/3c88835c4a6921a97...).

Currently tests in CI are running on unsupported EOL Node.js v18. Vitest is also used in the old, unsupported v0 version. PNPM v8 declared in package.json is also no longer supported. It might be a good idea to generally update all of these (and potentially other dependencies, e.g., eslint v8 is also no longer supported), but this could probably be done in a separate issue.

  • hooroomoo committed 238a49af on canary authored by brianperry
    feat: #3426518 Using Serializer Obscures Links and Pagination Data
    
    By:...
hooroomoo’s picture

@wotnak Could you open an issue to update the dependencies and link it to this issue?

  • hooroomoo committed 238a49af on main authored by brianperry
    feat: #3426518 Using Serializer Obscures Links and Pagination Data
    
    By:...

  • hooroomoo committed 238a49af on changeset-release/main authored by brianperry
    feat: #3426518 Using Serializer Obscures Links and Pagination Data
    
    By:...
balintbrews’s picture

Status: Reviewed & tested by the community » Fixed

Thank you, all! This is now included in @drupal-api-client/json-api-client@1.4.0 — also picked up in drupal-canvas@0.2.0 (via #3570305: Make links and meta available by default in data returned by the preconfigured JsonApiClient) and by the Canvas UI (not yet in a tagged release).

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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