Skip to content

Conversation

@robert-hromej
Copy link

Summary

Fixes a critical bug where cross-schema relationships were not setting linkage data in JSON:API responses.

Problem

When using has_one or has_many with the schema: option, related resources appeared in the included section but relationships.<name>.data was null, preventing JSON:API clients from properly linking resources.

Before:

{
  "data": {
    "relationships": {
      "author": { "data": null }
    }
  },
  "included": [
    { "type": "users", "id": "123" }
  ]
}

After:

{
  "data": {
    "relationships": {
      "author": { 
        "data": { "type": "users", "id": "123" }
      }
    }
  },
  "included": [
    { "type": "users", "id": "123" }
  ]
}

Changes

Core Fix

Modified `lib/jsonapi/active_relation_resource_patch.rb`:

  1. Array source handling: Convert Array sources (ResourceFragments/ResourceIdentities) to Hash in `handle_cross_schema_included`
  2. Linkage setting: Call `add_related_identity` in both `handle_cross_schema_to_one` and `handle_cross_schema_to_many` to populate relationship linkage
  3. has_many SQL: Fixed query to use proper WHERE clause with foreign_key

Tests

Added 12 comprehensive unit tests in `test/unit/resource/cross_schema_linkage_test.rb`:

has_one cross-schema (8 tests):

  • Linkage data creation
  • Null foreign key handling
  • Array/Hash source handling
  • Multiple resources with same related entity
  • Full serialization
  • Configuration verification
  • Regression test for normal relationships

has_many cross-schema (4 tests):

  • Linkage data creation for collections
  • Array source handling
  • Empty collection handling
  • Deduplication across multiple sources

Documentation

  • `docs/CROSS_SCHEMA_FIX.md` - Complete problem analysis and solution
  • `CHANGELOG_CROSS_SCHEMA.md` - Detailed changelog

Test Plan

  • All 12 new unit tests created
  • Syntax validation passed
  • Verified in production use case with PostgreSQL multi-schema setup
  • Backward compatible - no breaking changes

Impact

  • Fully backward compatible - existing applications automatically benefit
  • No breaking changes - all existing functionality preserved
  • Performance neutral - same number of queries executed

See `docs/CROSS_SCHEMA_FIX.md` for complete details.

Fixes a bug where cross-schema relationships were not setting linkage
data in JSON:API responses. Related resources appeared in 'included'
section but relationships.<name>.data was null, preventing clients
from properly linking resources.

Changes:
- Modified handle_cross_schema_included to convert Array sources to Hash
- Added linkage setting in handle_cross_schema_to_one via add_related_identity
- Fixed handle_cross_schema_to_many SQL query and linkage
- Added source_fragments passing through enhanced_options

Tests:
- Added 12 comprehensive unit tests covering has_one and has_many
- Tests cover Array/Hash sources, null handling, deduplication
- Created test fixtures and models for cross-schema scenarios

Documentation:
- docs/CROSS_SCHEMA_FIX.md - complete problem analysis and solution
- CHANGELOG_CROSS_SCHEMA.md - detailed changelog

This fix is fully backward compatible.
@robert-hromej robert-hromej force-pushed the fix/cross-schema-relationship-linkage branch from 76ddeaf to d4b5c96 Compare November 5, 2025 06:55
@robert-hromej robert-hromej deleted the fix/cross-schema-relationship-linkage branch November 5, 2025 07:01
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.

1 participant