-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Fix escaping bug in MutableJsonDocument #35204
Merged
annelo-msft
merged 23 commits into
Azure:main
from
annelo-msft:dynamicjson-escape-tests
Apr 12, 2023
Merged
Fix escaping bug in MutableJsonDocument #35204
annelo-msft
merged 23 commits into
Azure:main
from
annelo-msft:dynamicjson-escape-tests
Apr 12, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
API change check API changes are not detected in this pull request. |
annelo-msft
commented
Mar 31, 2023
sdk/core/Azure.Core.Experimental/src/MutableJsonDocument.WriteTo.cs
Outdated
Show resolved
Hide resolved
…To(Stream) go through same path as WriteTo(Utf8JsonWriter)
jsquire
approved these changes
Apr 12, 2023
sdk/core/Azure.Core.Experimental/src/MutableJsonElement.WriteTo.cs
Outdated
Show resolved
Hide resolved
sdk/core/Azure.Core.Experimental/src/MutableJsonElement.WriteTo.cs
Outdated
Show resolved
Hide resolved
heaths
approved these changes
Apr 12, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Forgot to finish my review yesterday)
annelo-msft
commented
Apr 12, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
In this AppConfig round-trip sample, if the highlighted line is commented out, the AppConfig service will respond to the request using DynamicData with a 400, where the same request using JsonDocument or JsonNode will succeed with a 200. This is because, prior to this PR, DynamicData was not handling escaping the same way JsonDocument does.
In the prior implementation, MutableJsonDocument.WriteTo() created a reader on the original JSON buffer, without doing any escaping, and passed reader.ValueSpan directly to writer.WriteStringValue(). Utf8JsonWriter will always escape string values (unless passed options that direct it otherwise), and JsonDocument deals with this by unescaping a string prior to calling WriteString().
Solution
Since all of the escaping logic in JsonDocument is held in internal types, the best we could do to duplicate the logic would be to copy those helper classes, or hand-write similar logic. This is clearly error prone. Instead, this PR moves the implementation of WriteTo() up a level of abstraction, so it can take advantage of the escaping logic in JsonDocument itself.
Implementation
This PR:
Contributes to #33856