Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jimsynz
Copy link
Contributor

@jimsynz jimsynz commented Aug 7, 2025

Summary

Fixes a regression in Ash.Scope.ToOpts implementation for Map where top-level :shared keys were not being properly converted to context options.

Problem

The Ash.Scope.ToOpts implementation for Map was only checking for :shared nested under a :context key:

%{context: %{shared: %{application: "test"}}}  # ✅ Worked
%{shared: %{application: "test"}}              # ❌ Didn't work

However, the deprecated Ash.Context.to_opts/1 documentation indicates it should handle both patterns, and client code was expecting the top-level pattern to work.

Solution

Updated the get_context/1 function in Ash.Scope.ToOpts for Map to check both patterns:

  1. First checks for nested :context key (existing behavior)
  2. Falls back to top-level :shared key (new behavior)
  3. Maintains backward compatibility and proper priority

Changes

  • lib/ash/scope.ex: Updated Ash.Scope.ToOpts implementation for Map
  • test/scope_test.exs: Added comprehensive regression tests (7 test cases)

Testing

  • ✅ All existing tests pass (1952 tests, 0 failures)
  • ✅ New regression tests cover both patterns and edge cases
  • ✅ Verified fix with client reproduction case
  • ✅ Backward compatibility maintained

Contributor Checklist

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

@jimsynz jimsynz requested a review from zachdaniel August 7, 2025 01:51
@zachdaniel
Copy link
Contributor

zachdaniel commented Aug 7, 2025

I think this gets pretty confusing. It isn't obvious what should happen if you have %{context: %{shared: %{a: 1}}, shared: %{a: 2}}. I'd suggest that we should merge them all up in that scenario, treating the global shared key as having priority. Not perfect but avoids "losing" a key.

The `Ash.Scope.ToOpts` implementation for `Map` was only checking for
`:shared` nested under a `:context` key, but the deprecated
`Ash.Context.to_opts/1` documentation indicates it should also handle
top-level `:shared` keys.

This change maintains backward compatibility while fixing the regression
where `%{shared: %{...}}` maps were not properly converted to context options.

Changes:
- Updated `Ash.Scope.ToOpts` for `Map` to check both patterns
- Merges top-level `:shared` with nested `:context -> :shared` when both exist

Fixes issue where client code using top-level `:shared` keys would
silently fail to pass context to actions.
@jimsynz jimsynz force-pushed the fix/scope-top-level-shared-context branch from 339fa0f to aa91bbf Compare September 22, 2025 03:12
@jimsynz
Copy link
Contributor Author

jimsynz commented Sep 22, 2025

I think this gets pretty confusing. It isn't obvious what should happen if you have %{context: %{shared: %{a: 1}}, shared: %{a: 2}}. I'd suggest that we should merge them all up in that scenario, treating the global shared key as having priority. Not perfect but avoids "losing" a key.

Agreed. Updated.

@zachdaniel zachdaniel merged commit 1c9167f into main Sep 22, 2025
43 checks passed
@zachdaniel
Copy link
Contributor

🚀 Thank you for your contribution! 🚀

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.

3 participants