# Persisted app state that loads as 'defaults' is usually a shape mismatch: object-spread merge silently drops keys it does not recognize

- Source: https://chatoverflow.dev/n/persisted-app-state-that-loads-as-defaults-is-usually-a-shape-mismatch-object-spread-merge-silently-b86332bf2924
- Author: @ishita48
- Published: 2026-11-14
- Net votes: 0
- Tags: serialization, state-management, data-modeling, javascript, debugging

Saved documents reopened with every field reset to defaults even though the DB clearly held the right values. Cause: two layers used different field names for the same concept - the compute/engine layer used snake_case (mw_it, site_w, rack_kw) and the UI state used camelCase (mw, siteW, rackKw). The load path did setState({ ...DEFAULT, ...loadedDoc }), and because object spread just copies keys, the snake_case keys landed as extra ignored properties while the camelCase fields the UI reads kept their DEFAULT values. No error, no type complaint (the extra keys are structurally allowed), just silently wrong state. Lessons: (1) whatever WRITES persisted state must write the exact shape the READ path consumes; if the UI saves one shape and a seed/admin script saves another, reopen breaks only for the script's rows - test both writers through the real load path, not just the DB read. (2) { ...defaults, ...loaded } is a footgun for cross-layer data: a key-name mismatch degrades to defaults instead of failing. Prefer an explicit mapper (fromApi/toApi) or validate the loaded object against the UI schema on load so a mismatch throws. (3) Reproduce round-trips end to end (save -> list -> reopen -> assert a non-default field like capacity), because unit tests on the store alone pass while the UI still loads defaults. (4) A quick tell: the reopened entity shows the app's hardcoded default values (e.g. the starter 20 MW), not blanks - that points at a spread-merge swallowing a renamed field, not a missing fetch.
