Skip to content

Fix: Don't add null values to query string. #5108

Merged
jasonsaayman merged 11 commits into
axios:v1.xfrom
ChronosMasterOfAllTime:feat/params_serializer_compat
Oct 15, 2022
Merged

Fix: Don't add null values to query string. #5108
jasonsaayman merged 11 commits into
axios:v1.xfrom
ChronosMasterOfAllTime:feat/params_serializer_compat

Conversation

@ChronosMasterOfAllTime

@ChronosMasterOfAllTime ChronosMasterOfAllTime commented Oct 12, 2022

Copy link
Copy Markdown
Contributor

This aims to resolve Feature Request #5107 and #5094

We added a "backwards compatibility" to allow serializing params with our own custom function instead of the current iterative toString approach.

@ghost

ghost commented Oct 12, 2022

Copy link
Copy Markdown

Yes please!

@ChronosMasterOfAllTime

Copy link
Copy Markdown
Contributor Author

@jasonsaayman were there merge conflicts? Seems like some odd things from the merge. (Like the readme for example)

@ChronosMasterOfAllTime ChronosMasterOfAllTime changed the title feat: add boolean flag to mimic pre 1.x behavior for paramsSerializer custom function Fix: Don't add null values to query string. Oct 14, 2022

@jasonsaayman jasonsaayman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 🚀

@jasonsaayman jasonsaayman merged commit b0ebf9f into axios:v1.x Oct 15, 2022
@ChronosMasterOfAllTime ChronosMasterOfAllTime deleted the feat/params_serializer_compat branch October 15, 2022 15:24
@ChronosMasterOfAllTime

Copy link
Copy Markdown
Contributor Author

Thank you!

Tbhesswebber pushed a commit to Tbhesswebber/axios that referenced this pull request Oct 15, 2022
* feat: add  boolean flag to mimic pre 1.x behavior for paramsSerializer custom function

* chore: update ParamsSerializer Readme

* fix: dont slice hash off URL if not appending params

* Omit nulls from formData serialization

* fix: dont add nulls or undefined values to arrays either

* readme update

* fix test

* chore: documentation

* chore: do TS properly

Co-authored-by: Jay <jasonsaayman@gmail.com>
@ghost ghost mentioned this pull request Nov 3, 2022
@havardthom

Copy link
Copy Markdown

This PR contradicts the discusssion here #1139 and solution here #1987

I have a use case for null query param, as many people in #1139 . Will this be added to Axios in the future?

@ChronosMasterOfAllTime

Copy link
Copy Markdown
Contributor Author

This PR contradicts the discusssion here #1139 and solution here #1987

I have a use case for null query param, as many people in #1139 . Will this be added to Axios in the future?

Nulls were being included as empty string some_param=, not some_param=null. I would think you would want the latter. Also if your API is set to deserialze the QS it should automatically handle missing fields as null/nil/None/undefined.

@OzQu

OzQu commented Nov 14, 2022

Copy link
Copy Markdown

As far as I understood, there is multiple use cases to have query string with keys without values

  it('should support params', function () {
    expect(buildURL('/foo', {
      foo: 'bar',
      isUndefined: undefined,
      isNull: null
    })).toEqual('/foo?foo=bar&isNull');
  });

Could that be considered?

@ghost

ghost commented Nov 16, 2022

Copy link
Copy Markdown

@OzQu I get the ask, however if you look at the behavior, an unset query Param returns an empty string when parsed via URLSearchParams

image

If you want null as a value for test, it would have to be handled by your API. I would think omitting the value is the equivalent of ?someVal when deserializing the request.

We can add it back in, but know it's up to the API to correctly handle this behavior when deserializing into your request object.

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.

4 participants