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
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
Comment #2
brianperryComment #3
brianperryComment #4
brianperryComment #5
wotnakThe 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:
The same thing could be done for the
metatop level object to, for example, make it easier to use total count info provided by the JSON:API Extras.Comment #6
balintbrews@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
jsonaas apeerDependencyand mark it as optional inpeerDependenciesMeta.Comment #7
brianperryYes, 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:
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.
Comment #8
wotnakComment #10
brianperryOpened 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.
Comment #11
brianperryUnsure about the test failures here. They are passing for me locally. Node version mismatch is probably my first uneducated guess.
Comment #12
wotnakLooks 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.
Comment #14
hooroomoo@wotnak Could you open an issue to update the dependencies and link it to this issue?
Comment #17
balintbrewsThank you, all! This is now included in
@drupal-api-client/json-api-client@1.4.0— also picked up indrupal-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).