Skip to content

fix(handles): read a field the entity data does not hold as null - #178

Merged
matej21 merged 2 commits into
mainfrom
fix/t1116-added-entity-null
Nov 24, 2026
Merged

matej21 merged 2 commits into
mainfrom
fix/t1116-added-entity-null

Conversation

@matej21

@matej21 matej21 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

FieldHandle.value is typed T | null, but a field that hasMany.add() did not set read undefined, both before and after persist. A created entity's snapshot holds only the keys set on it, and FieldHandle passed the missing key through.

Fixes #175

Fix

FieldHandle.value and serverValue now map a key the entity data does not hold to null. isDirty keeps comparing the raw values, the same values the persister diffs. After a create, the server data has no key for an unset field, so clearing that field with setValue(null) is written on the next persist, and the field must report itself dirty. The fallback from the schema path to the read alias still applies only to undefined, so an explicit null written by setValue(null) is not hidden by a stale aliased value.

Why the fix is in the handle and not the store, and why the type stays as it is: the store keeps a created entity's record sparse on purpose. MutationCollector.buildCreateData sends only the keys that are present and not null, so an unset column is left to its database default. Filling the snapshot with null would put null into entity data that the persister, the undo journal and the update diff all read. The read-side mapping changes only what a consumer reads. Changing the type to T | null | undefined would make every consumer handle a third state that only means "no value", which is the same thing null already means here.

Tests

tests/react/relations/hasMany/addUnsetFieldValue.test.tsx (the repro from the issue, extended):

  • before persist: value and serverValue read null (on main both read undefined; isDirty was already false)
  • after persist: the same
  • cleared after the create: isDirty is true, and the next persist sends { color: null } (guards review finding 1)
  • the create mutation is unchanged: toStrictEqual([{ tags: [{ alias, create: { name: 'New tag' } }] }]), so color is sent neither as null nor as an undefined key (passes on main and here)

Local: bun run typecheck clean, eslint on the changed files clean, bun run test 2612 pass / 0 fail on b4f4c5c.

Out of scope

$data of the same added entity (assembled by EntityDataAssembler) also lacks the unset key: Object.keys(tag.$data) is ['name', 'id'] although color is selected. This PR does not change that; it needs its own decision about alias keys of created entities.

After a create persist, serverValue of an unset field reads null, even where the database applied a non-null default. The client never reads the created row back. This is no worse than undefined on main; the full fix is to read the created row's selected fields back after the create.

Decisions

Decision (from the brief) Where Status
"Reproduce the issue with a failing test first." tests/react/relations/hasMany/addUnsetFieldValue.test.tsx; 2 of 4 tests fail on main done
"Fix it at the root, in the module that owns the store's view of a newly added entity … an added entity's unset fields read null, as the type promises. If the right answer is to change the type instead, say why." packages/bindx/src/handles/FieldHandle.ts (readValue) done: read-side mapping in FieldHandle, store record left sparse; reason above. $data not covered (see Out of scope)
"Check that persisting the added entity is unchanged: an unset field must not become an explicit null in the mutation … Test it." test is left out of the create mutation done
"The repo is public: open the PR ready, not a draft." / "Use Fixes #175" this PR done
"Run one independent review round and post it on the PR. Merge after approval and green CI." round 1 (changes, minor and nits only) and its response done
"No release" — done (no release cut)

Status

  • Review rounds: round 1 on 48cb0d9 found changes (1 minor code fix, 1 minor description fix, 3 nits). All are fixed in b4f4c5c and listed in the review response. Only minor and nit items, so no further round.
  • CI: green on b4f4c5c (run 45442551794), which is based on the current main 02bea00
  • Left before merge: nothing

🤖 Generated with Claude Code

FieldHandle.value is typed `T | null`, but a field that hasMany.add() did
not set read `undefined`, before and after persist: a created entity's
snapshot holds only the keys set on it. The snapshot stays sparse, because
an absent key is what keeps the column out of the create mutation (so the
database default applies); the handle maps the absent key to null. value,
serverValue and isDirty share the one reader.

Fixes #175

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matej21

matej21 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Review round 1

  • Reviewer: independent Claude agent (Opus 5.5, general-purpose agent, fresh context, read-only)
  • Commit: 48cb0d9
  • Verdict: changes

Verified: 2 of the 3 new tests fail on main (value and serverValue read undefined); the mutation test passes on both. The alias fallback keeps an explicit null at the schema path. inputProps caching still works. nested() reads stay consistent. No caller in react, form, dataview or ui depends on undefined from a field read. buildCreateData drops both undefined and null. Every brief decision has a row in the decision table, and each row matches the code.

Findings

  1. minor: FieldHandle.ts isDirty disagrees with the persister after a create. Steps: add({ name }), persist, then color.setValue(null). The field reports isDirty === false (it was true on main), but the next persist sends { color: null }, because the server data has no color key and the persister diffs raw values. Fix: compare the raw values in isDirty and normalize only in value and serverValue. Add a test for it.
  2. minor (description): "would gain nothing over a read-side mapping" is not accurate. After a create persist, serverValue reads null even where the database applied a non-null default. This is no worse than undefined on main, but it should be listed in Out of scope.
  3. nit: the code comment cites "issue FieldHandle.value reads undefined for a field hasMany.add() did not set, although it is typed T | null #175". History belongs in the commit or PR.
  4. nit: the test uses the non-null assertion probe!. Replace it with a guard that throws.
  5. nit (description): the Tests section says isDirty failed on main. It did not; only value and serverValue failed.

Normalizing both sides to null made a field cleared after a create read
clean, while the next persist still writes it: the server data of a
created entity has no key for an unset field, and the persister diffs raw
values. Only value and serverValue map an absent key to null now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matej21

matej21 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Review response, round 1

  1. minor, isDirty vs persister after a create: fixed in b4f4c5c. isDirty compares raw values again; only value and serverValue map an absent key to null. New test is dirty when cleared after the create, as the next persist writes it: it fails with the 48cb0d9 FieldHandle (isColorDirty: false) and passes on b4f4c5c. The second persist sends { color: null }.
  2. minor, description claim: fixed in the PR description. The "gain nothing" claim is gone, and Out of scope now notes that serverValue reads null after a create even where the database applied a default.
  3. nit, issue number in the code comment: fixed in b4f4c5c. The comment now states only the why.
  4. nit, probe!: fixed in b4f4c5c. The probe getter now throws when nothing was published.
  5. nit, Tests section: fixed in the PR description. Only value and serverValue failed on main.

Local on b4f4c5c: typecheck clean, eslint on the changed files clean, bun run test 2612 pass / 0 fail. All findings were minor or nit, so there is no further round.

@matej21
matej21 merged commit d1cce81 into main Nov 24, 2026
5 checks passed
@matej21
matej21 deleted the fix/t1116-added-entity-null branch November 24, 2026 16:58
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.

FieldHandle.value reads undefined for a field hasMany.add() did not set, although it is typed T | null

1 participant