Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #119: Fix constructing more complex object with arrays #126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

meex28
Copy link

@meex28 meex28 commented Dec 23, 2024

Fixing the bug #119

Description

Fixed incorrect array handling when combining complex hx-vals with json-enc. Previously, when processing:
<button hx-ext="json-enc" hx-vals='{"Id":1,"Items":["abc","xyz"]}'> ...</button>

The encodeParameters function gets as input FormData object like:

  • Id: '1'
  • Items: "abc"
  • Items: "xyz"

When retrieving types from hx-vals, for each entry with an Items key, whole array was added to the result object:

{
    Id: 1,
    Items: [ ["abc", "xyz"], ["abc", "xyz"] ]
}

Instead of:

{
    Id: 1,
    Items: ["abc", "xyz"]
}

To change this behaviour, the function now first groups FormData parameters by keys, and then maps raw values to types from hx-vals.

Testing

In the test handles hx-vals properly added an array-type value ('array': ['text', 123, {'key': 'value'}]) and expects it to to be properly included in the body.

Checklist

  • I have read the contribution guidelines
  • I ran the test suite locally (npm run test) and verified that it succeeded

…revent duplicating arrays elements in final object
Copy link

netlify bot commented Dec 23, 2024

Deploy Preview for htmx-extensions canceled.

Name Link
🔨 Latest commit 71e3cab
🔍 Latest deploy log https://app.netlify.com/sites/htmx-extensions/deploys/676995e9030a6b0008b92e92

@meex28 meex28 changed the title CLOSES #119: Fix constructing more complex object with arrays Fix #119: Fix constructing more complex object with arrays Dec 23, 2024
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.

1 participant