Skip to content

Latest commit

 

History

History
132 lines (99 loc) · 3.98 KB

File metadata and controls

132 lines (99 loc) · 3.98 KB

Changelog: Cross-Schema Relationship Linkage Fix

Summary

Fixed a critical bug where has_one and has_many cross-schema relationships were not setting relationship linkage data (relationships.<name>.data) in JSON:API responses, even though the related resources were correctly included in the included section.

Changes

Modified Files

lib/jsonapi/active_relation_resource_patch.rb

Changes to handle_cross_schema_included:

  • Added support for Array sources (not just Hash)
  • Converts Array of ResourceFragments/ResourceIdentities to Hash
  • Passes source_fragments in enhanced_options to downstream handlers

Changes to handle_cross_schema_to_one:

  • Added linkage setting after creating related resource fragment
  • Calls add_related_identity on source fragment to populate relationships.data
  • Added comprehensive debug logging

Changes to handle_cross_schema_to_many:

  • Fixed SQL query to use WHERE clause with foreign_key
  • Groups related records by source_id for proper linkage
  • Adds linkage for each related resource to source fragment

New Test Files

test/unit/resource/cross_schema_linkage_test.rb

Comprehensive unit test suite covering:

has_one tests:

  • test_has_one_cross_schema_creates_linkage_data - Basic linkage creation
  • test_has_one_cross_schema_with_null_foreign_key - Null foreign key handling
  • test_cross_schema_relationship_with_array_source - Array source support
  • test_cross_schema_relationship_with_hash_source - Hash source support
  • test_multiple_candidates_with_same_recruiter - Deduplication
  • test_cross_schema_included_in_full_serialization - End-to-end test
  • test_cross_schema_relationships_hash_registration - Configuration test
  • test_non_cross_schema_relationships_still_work - Regression test

has_many tests:

  • test_has_many_cross_schema_creates_linkage_data - Basic has_many linkage
  • test_has_many_cross_schema_with_array_source - Array source with has_many
  • test_has_many_cross_schema_empty_collection - Empty collection handling
  • test_has_many_cross_schema_deduplication - Deduplication across multiple sources

test/integration/cross_schema_integration_test.rb

Integration test placeholders (skipped - require full Rails setup)

Modified Test Files

test/fixtures/active_record.rb

Added tables for cross-schema testing:

  • test_candidates - Primary resource with foreign keys
  • test_users - Related resource from "different schema"
  • test_locations - Normal same-schema relationship
  • test_departments - For has_one cross-schema
  • test_companies - For has_many cross-schema

New Fixtures

  • test/fixtures/test_users.yml
  • test/fixtures/test_candidates.yml
  • test/fixtures/test_locations.yml
  • test/fixtures/test_departments.yml

Documentation

docs/CROSS_SCHEMA_FIX.md

Complete documentation covering:

  • Problem description with examples
  • Root cause analysis
  • Solution implementation details
  • Usage examples
  • Testing approach
  • Migration notes

Verification

The fix was tested and verified to work in production use cases where:

  • Primary resources have has_one relationships to resources in different PostgreSQL schemas
  • Relationship linkage data now correctly appears in JSON:API responses
  • Frontend deserializers can properly link related resources

Before (Broken)

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

After (Fixed)

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

Backward Compatibility

This fix is fully backward compatible. Existing code using cross-schema relationships will automatically benefit from the fix without any changes required.

Next Steps

  1. Run full test suite to ensure no regressions
  2. Create PR to main repository
  3. Update version number
  4. Release new gem version