Skip to content

fix(json-enc): restore hx-vals keys dropped by empty-array FormData encoding - #194

Open
alter wants to merge 1 commit into
bigskysoftware:mainfrom
alter:fix/json-enc-empty-array
Open

alter wants to merge 1 commit into
bigskysoftware:mainfrom
alter:fix/json-enc-empty-array

Conversation

@alter

@alter alter commented Sep 24, 2026

Copy link
Copy Markdown

Description

encodeParameters builds object from the request's FormData parameters, then walks Object.keys(object) to restore original types from hx-vals/hx-vars. A key whose value serializes to nothing in FormData (an empty array, since there's nothing to push into the form data for it) never gets an entry in object in the first place, so that restore loop never visits it and the key is silently dropped from the JSON body.

Reported against htmx core in bigskysoftware/htmx#3845, but the actual code is here: the json-enc extension's own restore step only looks at keys FormData already produced, not at the full set of keys in hx-vals.

Fix

Restore over the union of Object.keys(object) and Object.keys(vals) instead of only Object.keys(object), so a key present in hx-vals but absent from the FormData-derived object still gets added.

Testing

Added keeps hx-vals keys whose value serializes to nothing in FormData, mirroring the reporter's repro (hx-vals="js:{'firstLevel': [], 'secondLevel': {'values': []}}"). Confirmed it fails against the unmodified code (firstLevel missing from the parsed request body) and passes after the fix. Full suite: 13/13 passing, no regressions.

Closes bigskysoftware/htmx#3845

@netlify

netlify Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for htmx-extensions canceled.

Name Link
🔨 Latest commit 56ed9ff
🔍 Latest deploy log https://app.netlify.com/projects/htmx-extensions/deploys/6ab4fbfa32e2d30008a81a28

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.

hx-vals + json-enc: first-level empty array is dropped

1 participant