fix: handle ForbiddenField in full_diff when using field_policies (#215)#219
fix: handle ForbiddenField in full_diff when using field_policies (#215)#219M-Gonzalo wants to merge 4 commits intoash-project:mainfrom
Conversation
…h-project#215) - Treat %Ash.ForbiddenField{} values as nil when dumping values for full_diff - Cover scalar and array cases in FullDiff.Helpers.dump_value/2 - Add regression tests in AshPaperTrail.FullDiffTest - Document ForbiddenField->nil behavior for :full_diff in the Resource DSL docs - Note the fix under Unreleased in the changelog
CHANGELOG.md
Outdated
|
|
||
| ## Unreleased | ||
|
|
||
| ### Bug Fixes: |
There was a problem hiding this comment.
the changelog is generated, no need to edit yourself.
There was a problem hiding this comment.
0nce you remove this I'll merge.
There was a problem hiding this comment.
Done. I also removed a manual insert to the DSL docs. Didn't realize until later that they're also autogenerated
|
@M-Gonzalo this change looks good, but consider telling your agent that produces these descriptions to be way less verbose. This is just a few LoC, and that description is tons of prose 😓 |
|
Its still reasonable to document this behavior in some way, but yeah the DSL docs are autogenerated from the DSL istelf. |
|
Looks like in trimming down the PR we removed the description change that documented this behavior. That would still be good to add 😄 |
Handle
Ash.ForbiddenFieldsafely in:full_diffand document behaviorSummary
This PR fixes GitHub issue 215 by making full-diff change tracking resilient to
%Ash.ForbiddenField{}values introduced by Ash field policies. Previously, when a resource hadfield_policiesand an attribute inchangeset.datawas replaced with%Ash.ForbiddenField{},:full_diffwould callAsh.Type.dump_to_embedded/3with that value, receive:error, and crash with aMatchError. The new behavior treats anyAsh.ForbiddenFieldencountered during full-diff dumping asnil, and documents this explicitly.Changes
Full-diff value dumping
dump_value/2inhelpers.exto special-caseAsh.ForbiddenField:dump_value(%Ash.ForbiddenField{}, _attribute), which now returnsnil.{:array, attr_type}case so that:%Ash.ForbiddenField{}, the dumped element becomesnil.Ash.Type.dump_to_embedded/3with the existing constraints.Ash.ForbiddenFieldand returnnilin that case, instead of callingAsh.Type.dump_to_embedded/3.{:ok, dumped_value} = :errorcrash path that issue 215 reported, while keeping the shape of the full-diff maps (from/to/unchanged) unchanged.Tests
full_diff_test.exswith a new describe block that exercisesAsh.ForbiddenFieldhandling at the helper layer:dump_value treats ForbiddenField in data as nil for scalar attributes:Helpers.dump_value/2with%Ash.ForbiddenField{field: :note, type: :attribute}.nil.dump_value treats ForbiddenField inside arrays as nil elements:{:array, :string}attribute metadata.Helpers.dump_value/2with["visible", %Ash.ForbiddenField{}, "also visible"].["visible", nil, "also visible"].MatchErroras in the issue; after the helper changes they now pass.Documentation
:full_diffin the generated DSL docs:change_tracking_modeoption docs inDSL-AshPaperTrail.Resource.md, added that when using:full_diff, any values hidden by field policies (i.e.%Ash.ForbiddenField{}in loaded data) are recorded asnilin the diff.Changelog
CHANGELOG.mdunder a new “Unreleased” heading:%Ash.ForbiddenField{}from field policies; such values are now recorded asnilin diffs. (Issuefull_diffcrashes on%Ash.ForbiddenField{}#215)”Behavioral impact
change_tracking_mode: :full_diff:field_policiescause an attribute to be represented as%Ash.ForbiddenField{}inchangeset.data, the full diff will now:nilwhen computing the diff.%{unchanged: nil}, and a change from forbidden to a visible value will appear as%{from: nil, to: "new value"}.%Ash.ForbiddenField{}will be represented asnilin the dumped array before list-diffing.Testing
mix test test/ash_paper_trail/full_diff_test.exsThis PR thus:
full_diffcrashes on%Ash.ForbiddenField{}#215.nil) for:full_diff.Contributor checklist
Leave anything that you believe does not apply unchecked.