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

Fix escaping bug in MutableJsonDocument #35204

Merged
merged 23 commits into from
Apr 12, 2023

Conversation

annelo-msft
Copy link
Member

@annelo-msft annelo-msft commented Mar 28, 2023

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

@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

@annelo-msft annelo-msft marked this pull request as ready for review April 11, 2023 21:36
Copy link
Member

@heaths heaths left a 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 annelo-msft merged commit cca28d5 into Azure:main Apr 12, 2023
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.

5 participants