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

The parse method returns incorrect output when both strictNullHandling and allowEmptyArrays are set to true #510

Closed
Ari-ZH opened this issue Jul 8, 2024 · 2 comments · Fixed by techouse/qs#21 or techouse/qs_codec#5

Comments

@Ari-ZH
Copy link

Ari-ZH commented Jul 8, 2024

Here is the test code: when strictNullHandling and allowEmptyArrays are both set to true, the parse function does not correctly interpret the result of stringify.

import { expect, test } from 'vitest';
import qs from 'qs';

const beforeParams = {
  testEmptyArray: [],
};

// stringify result
const searchStr = 'testEmptyArray[]';

// expect parse result
const afterParams = {
  testEmptyArray: [],
};

test('stringify', () => {
  expect(
    qs.stringify(beforeParams, {
      strictNullHandling: true,
      allowEmptyArrays: true,
    }),
  ).toBe(searchStr);
});

test('parseSearchStr', () => {
  expect(
    qs.parse(searchStr, { strictNullHandling: true, allowEmptyArrays: true }),
  ).toStrictEqual(afterParams);
  //  expect { testEmptyArray:[] } but return { testEmptyArray:[null] }
});

Version: 6.12.2

@Ari-ZH
Copy link
Author

Ari-ZH commented Jul 8, 2024

I believe the issue arises here. https://github.com/ljharb/qs/blob/main/lib/parse.js#L129
leaf is null when strictNullHandling is true , so it return [null]

@ljharb
Copy link
Owner

ljharb commented Jul 8, 2024

Thanks, this is very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants