Skip to content

fix(common): properly cast http param values to strings#42643

Closed
cexbrayat wants to merge 1 commit intoangular:mainfrom
cexbrayat:fix/42641-http-params
Closed

fix(common): properly cast http param values to strings#42643
cexbrayat wants to merge 1 commit intoangular:mainfrom
cexbrayat:fix/42641-http-params

Conversation

@cexbrayat
Copy link
Member

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Before this commit, when initializing HttpParams with:

const body = new HttpParams({fromObject: {b: 2}});

then body.get('b') returned 2 instead of '2' as expected.

Fixes #42641

What is the new behavior?

This commit makes sure the values are converted to strings in such cases.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@google-cla google-cla bot added the cla: yes label Jun 24, 2021
@jessicajaniuk jessicajaniuk added the area: common Issues related to APIs in the @angular/common package label Jun 24, 2021
@ngbot ngbot bot added this to the Backlog milestone Jun 24, 2021
@AndrewKushnir
Copy link
Contributor

Quick comment: from API perspective it looks a bit unusual that the values are always converted to strings, i.e. in the following example:

const body = new HttpParams({fromObject: {b: 2}});
const b = body.get('b');

As an API user I may expect the original value to be returned (i.e. 2 as a number, but not as a string). Do we base this behavior on the params representation in a URL (i.e. ?b=2 in my case), which would always be a string?

Should we consider changing the get (and related methods) API signature instead?

Note: I'm not sure what the right approach is, just want to check if changing the get signature was considered and if yes, we can probably document that in the commit message and PR description for future reference + expand docs for get function to mention that the returned value would always be converted to a string (and why we do that).

alxhub
alxhub previously approved these changes Jun 24, 2021
@alxhub
Copy link
Member

alxhub commented Jun 24, 2021

Presubmit

@cexbrayat
Copy link
Member Author

@AndrewKushnir Thanks for your feedback. I have no strong opinions on this. This fix is a good first step (as the current behavior is broken), but if the team decides to change the signatures of get and others, I can probably help with that.

@petebacondarwin
Copy link
Contributor

@AndrewKushnir Thanks for your feedback. I have no strong opinions on this. This fix is a good first step (as the current behavior is broken), but if the team decides to change the signatures of get and others, I can probably help with that.

I agree, we will schedule some time at the next framework meeting to discuss the public API for the HttpParams class.

@petebacondarwin petebacondarwin added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Jun 25, 2021
@ngbot
Copy link

ngbot bot commented Jun 25, 2021

I see that you just added the action: merge label, but the following checks are still failing:
    failure status "ci/angular: size" is failing
    failure status "ci/circleci: build-ivy-npm-packages" is failing
    failure status "ci/circleci: build-npm-packages" is failing
    failure status "ci/circleci: legacy-unit-tests-saucelabs" is failing
    failure status "ci/circleci: test" is failing
    failure status "ci/circleci: test_ivy_aot" is failing
    failure status "ci/circleci: test_zonejs" is failing
    failure status "google3" is failing
    pending status "ci/circleci: setup" is pending

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

@jessicajaniuk jessicajaniuk force-pushed the fix/42641-http-params branch from bd73187 to 037931c Compare June 28, 2021 17:38
@jessicajaniuk
Copy link
Contributor

The presubmit finished, but failed to report back. There are two legit test failures that exactly test against HttpParams, and are failing for the expected reasons. We'd have to fix those two tests forward along with this.

@petebacondarwin
Copy link
Contributor

petebacondarwin commented Jul 1, 2021

Should we consider changing the get (and related methods) API signature instead?

Discussed in our team meeting. We should not change the public API of this class to allow non-strings to be returned.

The aim of the class is to provide a stringified list of params for HTTP requests, not as a general purpose container for values.
Also if anything we should be aligning our API (and eventually, perhaps, our implementation) with the native URLSearchParams type.

@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit state: blocked and removed action: merge The PR is ready for merge by the caretaker labels Jul 7, 2021
@AndrewKushnir
Copy link
Contributor

AndrewKushnir commented Jul 7, 2021

@AndrewKushnir Thanks for your feedback. I have no strong opinions on this. This fix is a good first step (as the current behavior is broken), but if the team decides to change the signatures of get and others, I can probably help with that.

@cexbrayat thanks for the reply. I've looked at the code once again and found out that the append and appendAll methods also accept a union of string|number|boolean, but convert the values to strings later. So converting initial values to strings (the logic added in this PR) makes sense and is consistent with other parts of that class.

I'm adding the "blocked" label for now due to some failing tests in Google's codebase that would require a cleanup. We'd also need to run a global check in Google's codebase to see if we have more instances.

@AndrewKushnir AndrewKushnir modified the milestones: Backlog, v13-candidates Aug 13, 2021
@HyperLife1119
Copy link
Contributor

HyperLife1119 commented Oct 15, 2021

Is it possible to directly re-encapsulate the native URLSearchParams? Rather than manually maintaining the map.

@petebacondarwin
Copy link
Contributor

@HyperLife1119 - do you mean for the HttpParams implementation to use the URLSearchParams internally?

@HyperLife1119
Copy link
Contributor

do you mean for the HttpParams implementation to use the URLSearchParams internally?

Yes, but Angular currently does not do this, I want to know the reason.

@petebacondarwin
Copy link
Contributor

URLSearchParams is not supported in IE11 - so we could not use it without a polyfill.
But in 13.0.0 support for IE11 is dropped from Angular so this could become an option.

@HyperLife1119
Copy link
Contributor

URLSearchParams is not supported in IE11 - so we could not use it without a polyfill. But in 13.0.0 support for IE11 is dropped from Angular so this could become an option.

Thanks, I will continue to pay attention to this PR.

@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Oct 15, 2021
@AndrewKushnir AndrewKushnir added the action: presubmit The PR is in need of a google3 presubmit label Nov 23, 2021
@AndrewKushnir AndrewKushnir added target: major This PR is targeted for the next major release and removed target: patch This PR is targeted for the next patch release labels Mar 10, 2022
@AndrewKushnir AndrewKushnir force-pushed the fix/42641-http-params branch from 037931c to e5b5a00 Compare March 10, 2022 01:24
@AndrewKushnir AndrewKushnir self-assigned this Mar 10, 2022
@AndrewKushnir AndrewKushnir force-pushed the fix/42641-http-params branch from e5b5a00 to 8da0c36 Compare April 16, 2022 02:00
@AndrewKushnir
Copy link
Contributor

Global Presubmit (internal-only link).

@dylhunn dylhunn dismissed stale reviews from alxhub and petebacondarwin via 8da0c36 May 2, 2022 18:42
@dylhunn
Copy link
Contributor

dylhunn commented May 2, 2022

@AndrewKushnir @cexbrayat Is this something we intend to land in 14?

If so, I kicked off a de-flake run of failures from the above TGP.

@cexbrayat
Copy link
Member Author

@dylhunn That was the idea, yes

@dylhunn
Copy link
Contributor

dylhunn commented May 2, 2022

Looking at the presubmit results, some cleanup in g3 is needed. We're squeaking in a bit late (freeze is Wednesday). Andrew and I will have a look at what's needed, but this may have slipped by us again 🤦

I'll post an update after our rerun finishes.

Before this commit, when initializing `HttpParams` with:

    const body = new HttpParams({fromObject: {b: 2}});

then `body.get('b')` returned `2` instead of `'2'` as expected.

This commit makes sure the values are converted to strings in such cases.

Fixes angular#42641
@AndrewKushnir AndrewKushnir force-pushed the fix/42641-http-params branch from 8da0c36 to 1c311a0 Compare May 2, 2022 23:09
@AndrewKushnir
Copy link
Contributor

Quick update: we are waiting for some cleanup in Google's codebase to land. After that we'll rerun the necessary tests (TGP) and if everything goes well, we should be able to proceed with the merge of this PR.

@dylhunn
Copy link
Contributor

dylhunn commented May 4, 2022

I am running another TGP with all g3 patches applied:

Edit: Looks like all failures are preexisting.

@dylhunn dylhunn added merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note and removed state: blocked action: presubmit The PR is in need of a google3 presubmit labels May 4, 2022
@ngbot ngbot bot added the action: merge The PR is ready for merge by the caretaker label May 4, 2022
@dylhunn
Copy link
Contributor

dylhunn commented May 4, 2022

merge-assistance: we are still waiting for one g3 team to submit the fix we provided.

@dylhunn
Copy link
Contributor

dylhunn commented May 4, 2022

This PR was merged into the repository by commit 10691c6.

@dylhunn dylhunn closed this in 10691c6 May 4, 2022
@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 Jun 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: common Issues related to APIs in the @angular/common package cla: yes merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HttpParams instantiation using fromObject option with number or boolean parameters does not convert them to string

7 participants