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

feat(common): serialize to JSON if explicitly set in Content-Type and… #31991

Closed

Conversation

dasois
Copy link

@dasois dasois commented Aug 3, 2019

… not already serialized

RFC 8259 and ECMA-404 define a simple string, boolean and null as valid JSON.
If Content-Type has been explicitly set to 'application/json' the HttpClient should
determine if the body has already been serialized and if not, serialize it.

Not breaking.
Relates to #31973, #31974

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • [ x ] 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?

Currently you are able to e.g. provide a string, that is not a serialized JSON and want to send it to a Server that expects 'application/json' Content-Type. The HttpClient would not serialize that string and thus send an invalid JSON string and parsing will fail on the server side.
Even though HttpClient knows it needs to send JSON, it doesn't make sure that the data already has been serialized. It just assumes that this is the case for a string.

Relating Issue Number: #31973

What is the new behavior?

If Content-Type is explicitly set to 'application/json', the HttpClient will check the body, if it is a string, if it is already parsed JSON and otherwise parse it.

Does this PR introduce a breaking change?

  • Yes
  • [ x ] No, because if people have already provided parsed JSON, then it won't get parsed again.

Other Notes

We already tried to solve this issue in #31974, but the discussions lead to a non-breaking solution being a better choice here, which was the motivation for this PR.

… not already serialized

RFC 8259 and ECMA-404 define a simple string, boolean and null as valid JSON.
If Content-Type has been explicitly set to 'application/json' the HttpClient should
determine, if the body has already been serialized and if not, serialize it.

Not breaking.
Relates to angular#31973, angular#31974
@dasois dasois requested a review from a team as a code owner August 3, 2019 21:45
dasois added a commit to dasois/angular that referenced this pull request Aug 3, 2019
…h whitespace

A serialized JSON string can start with whitespaces by definition.
Allows this case to pass the checks and not be serialized again.

Combined with the other commits of PR  angular#31991 this is not a breaking change.
@dasois dasois force-pushed the feat_request-json-smart-stringify branch from 27a5d8c to b7b83f4 Compare August 3, 2019 22:26
dasois added a commit to dasois/angular that referenced this pull request Aug 3, 2019
…y starting with whitespace

A serialized JSON string can start with whitespaces by definition.
Allows this case to pass the checks and not be serialized again.

Combined with the other commits of PR  angular#31991 this is not a breaking change.
@dasois dasois force-pushed the feat_request-json-smart-stringify branch from b7b83f4 to 7e2570a Compare August 4, 2019 08:13
dasois added a commit to dasois/angular that referenced this pull request Aug 4, 2019
…y starting with whitespace

A serialized JSON string can start with whitespaces by definition.
Allows this case to pass the checks and not be serialized again.

Combined with the other commits of PR  angular#31991 this is not a breaking change.
@dasois dasois force-pushed the feat_request-json-smart-stringify branch from 7e2570a to 7b41d67 Compare August 4, 2019 08:23
dasois added a commit to dasois/angular that referenced this pull request Aug 4, 2019
…y starting with whitespace

A serialized JSON string can start with whitespaces by definition.
Allows this case to pass the checks and not be serialized again.

Combined with the other commits of PR  angular#31991 this is not a breaking change.
…y starting with whitespace

A serialized JSON string can start with whitespaces by definition.
Allows this case to pass the checks and not be serialized again.

Combined with the other commits of PR  angular#31991 this is not a breaking change.
@dasois dasois force-pushed the feat_request-json-smart-stringify branch from 7b41d67 to 7649d4b Compare August 4, 2019 08:45
@dasois
Copy link
Author

dasois commented Aug 4, 2019

codefresh-bazel build seems to be broken, I don't think my change introduced this.

@ngbot ngbot bot added this to the needsTriage milestone Aug 8, 2019
@fpaaske
Copy link

fpaaske commented Feb 14, 2020

What’s the status on this issue? Will this be merged anytime soon?

@tcarlyle
Copy link

Any news when this will be merged?

@dasois
Copy link
Author

dasois commented Apr 20, 2020

@IgorMinar @AndrewKushnir Can I do anything to get this merged?

@petebacondarwin petebacondarwin removed the request for review from a team January 28, 2021 09:51
@petebacondarwin petebacondarwin added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Jan 28, 2021
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Jan 28, 2021
@jessicajaniuk
Copy link
Contributor

This PR has the potential to be a breaking change, but it's definitely a valid concern. We're going to close this PR for now, but we're discussing internally a good solution to this problem. There is no ETA at this moment.

@fpaaske
Copy link

fpaaske commented Oct 28, 2021

@jessicajaniuk could you share some insight into why this is a potentially breaking change? Maybe it could be addressed, instead of just closing the issue?

@jessicajaniuk
Copy link
Contributor

@fpaaske Sorry for the delay on this response. The reason it's breaking is because HttpClient is easily one of the most used services with a lot of people already connecting to endpoints that have the content-type set. All of these applications have code that is currently expecting strings and then convert it to a useable type afterwards. If we change this behavior, it literally breaks everyone in that situation. The possibility of a migration to address the change wouldn't be possible either due to the vastness of the usage.

HttpClient, itself, has been neglected for quite a while. It lacks strong typing, has hard dependencies on RxJS, and has this issue of serialization. Rather than updating the current implementation and risk breaking so many, we're having early conversations about potentially building a new and updated HttpClient that addresses all of these issues. It's still very early in that conversation, but there will likely be a proposal for it in the coming months.

@fpaaske
Copy link

fpaaske commented Oct 29, 2021

Thanks for your explanation, hopefully there will be a good solution in the future!

@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 Nov 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: review The PR is still awaiting reviews from at least one requested reviewer area: common/http cla: yes target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants