Skip to content

Conversation

@ibustosatlegalzoom
Copy link
Contributor

Screenshot

N/A

What Changed & Why

Learn more at #263

Introduced a flag attachRelationshipDataOnUnresolvedIncludes which by default is set to true for backwards-compatibility purposes.

When set to true, Devour will continue to attach relationship metadata in the client's response on unresolved includes as it normally does.

When set to false however, Devour will abstain from attaching such metatada. This applies both for relationships of type hasMany and hasOne-

Testing

Just run the tests.

Documentation

Updated the README file with instructions.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new configuration flag attachRelationshipDataOnUnresolvedIncludes to control whether relationship metadata (id, type) should be attached to responses when the related resources are not included in the API response. The flag defaults to true for backward compatibility, but when set to false, only fully resolved (included) relationships will have their data populated.

Key changes:

  • Added attachRelationshipDataOnUnresolvedIncludes flag to the JsonApi constructor with a default value of true
  • Modified deserialize logic to conditionally attach relationship data based on the flag for both hasOne and hasMany relationships
  • Added comprehensive test coverage for both relationship types with the new flag disabled
  • Updated README with documentation explaining the new feature

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/index.js Adds the new attachRelationshipDataOnUnresolvedIncludes configuration option to the defaults and assigns it to the instance
src/middleware/json-api/_deserialize.js Wraps the unresolved relationship data attachment logic in conditional checks for both attachHasOneFor and attachHasManyFor functions
test/api/deserialize-test.js Adds two new test cases verifying the flag prevents relationship metadata attachment for unresolved hasMany and hasOne relationships
README.md Documents the new flag and its purpose in the Relationships section

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

expect(product.type).to.eql('products')
expect(product.title).to.eql('hello')
expect(product.tags).to.be.an('array')
expect(product.tags).to.be.empty()
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion to.be.empty() is not a valid expect.js method. Based on the testing patterns used elsewhere in this codebase (e.g., line 180 in serialize-test.js), use .length to check array emptiness instead: expect(product.tags.length).to.eql(0) or expect(product.tags.length).to.be(0).

Suggested change
expect(product.tags).to.be.empty()
expect(product.tags.length).to.be(0)

Copilot uses AI. Check for mistakes.
README.md Outdated
// => data.comment will be populated with any comments included by your API
```

By default, Devour will include relationship metadata (id, type, and other metadata) in each response. It may be handy to exclude this metadata from the response when the relationship is not resolved by the backend (not listed in the "includes" Json:api response). For this, make sure the `attachRelationshipDataOnUnresolvedIncludes` client flag is set to `false`, (default value is `true`).
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The documentation sentence is awkwardly worded. Consider revising for clarity: "By default, Devour includes relationship metadata (id, type, and other metadata) for all relationships in each response. To exclude this metadata when a relationship is not resolved by the backend (i.e., not listed in the "included" section of the JSON:API response), set the attachRelationshipDataOnUnresolvedIncludes client flag to false (default value is true)."

Suggested change
By default, Devour will include relationship metadata (id, type, and other metadata) in each response. It may be handy to exclude this metadata from the response when the relationship is not resolved by the backend (not listed in the "includes" Json:api response). For this, make sure the `attachRelationshipDataOnUnresolvedIncludes` client flag is set to `false`, (default value is `true`).
By default, Devour includes relationship metadata (such as `id`, `type`, and other fields) for all relationships in each response. To exclude this metadata when a relationship is not resolved by the backend (i.e., not listed in the `"included"` section of the JSON:API response), set the `attachRelationshipDataOnUnresolvedIncludes` client flag to `false`. The default value is `true`.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants