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

HttpParams : Handling of null & undefined values #18567

Closed
chrish619 opened this issue Aug 7, 2017 · 6 comments
Closed

HttpParams : Handling of null & undefined values #18567

chrish619 opened this issue Aug 7, 2017 · 6 comments

Comments

@chrish619
Copy link

chrish619 commented Aug 7, 2017

I'm submitting a...

[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

new HttpParams().set('arg', null).toString(); // "arg=null"
let a = {};
new HttpParams().set('arg', a.any).toString(); // "arg=undefined"

Expected behavior

new HttpParams().set('arg', null).toString(); // "arg="
let a = {};
new HttpParams().set('arg', a.any).toString(); // "arg="

Minimal reproduction of the problem with instructions

https://github.com/angular/angular/blob/master/packages/common/http/src/params.ts#L57
This is due to the underlying encodeURIComponent return string literals for "null" or "undefined";

(edited) Plnkr replication : https://embed.plnkr.co/k5naIib3sGADM91UZ31a/

What is the motivation / use case for changing the behavior?

This has mostly arisen due to HttpClient, and replacement of URLSearchParams, with HttpParams

Is this an intended behaviour, or does this require a fix? e.g.

  • null and undefined are converted to ""? or;
  • null is "", and undefined is thrown?

I appreciate these are not intended as direct replacements, and you can create your own HttpParameterCodec, but this seems like an unintended operation.

Environment

Angular version: 4.3.3

Browser:
- [x] Chrome (desktop) version 60.0
@vicb vicb added the comp: http label Aug 7, 2017
@robwormald
Copy link
Contributor

robwormald commented Aug 7, 2017

This is the intended behavior, I believe, as it tracks the behavior of the native-standard - https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

let q = new URLSearchParams();
q.append('x', null);
q.append('y', undefined);
q.append('z', '');

q.toString(); // x=null&y=undefined&z=

Since there's no way to encode a null/undefined value to a string, the behavior is sort of undefined :) - best to pass an empty string if that's what you want to send.

@alxhub
Copy link
Member

alxhub commented Aug 7, 2017

Also, note that the signature of .set() for example does not allow null or undefined for the value to set, so your reproduction would not compile under strictNullChecks.

@pavel-blagodov
Copy link

pavel-blagodov commented Aug 10, 2017

Try this example written in ES5. It uses correct application/x-www-form-urlencoded encoding algorithm since encodeURIComponent works in accordance with https://www.w3.org/TR/html5/forms.html#url-encoded-form-data

var MyHttpUrlEncoder =
      ng.core.Class({
        extends: ng.common.http.HttpUrlEncodingCodec,
        constructor: function () {},
        encodeKey: function (k) {
          return encodeURIComponent(k);
        },
        encodeValue: function (v) {
          return encodeURIComponent(this.serializeValue(v));
        },
        serializeValue: function (v) {
          if (_.isObject(v)) {
            return _.isDate(v) ? v.toISOString() : JSON.stringify(v);
          }
          if (v === null || _.isUndefined(v)) {
            return "";
          }
          return v;
        }
      });

then

new ng.common.http.HttpParams({encoder: new MyHttpUrlEncoder()}).set("a", null).toString();
//"a="

@alxhub alxhub closed this as completed Nov 28, 2017
@ChrisDevinePimss
Copy link

Didn't the old one allow to pass nulls/undefined and it would not put them in the query string.

I only noticed because iv recently updated to use HttpClient and found this change in functionality has broken a lot of services.

@Avien
Copy link

Avien commented Feb 5, 2018

I agree, the set method used to ignore null/undefined values

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants