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
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3df8de9
Fix property test in public tests; add test that repros escaping issue.
annelo-msft Mar 24, 2023
35c63c4
Merge remote-tracking branch 'upstream/main' into dynamicjson-escape-…
annelo-msft Mar 28, 2023
ed8a1a1
Don't escape special characters in string values.
annelo-msft Mar 28, 2023
6b8c278
Save this file for another commit
annelo-msft Mar 28, 2023
095c82d
Remove unneeded test code
annelo-msft Mar 28, 2023
ff9b2a9
Merge remote-tracking branch 'upstream/main' into dynamicjson-escape-…
annelo-msft Mar 31, 2023
3595b52
Some new thinking and work in progress
annelo-msft Apr 8, 2023
5aa2e09
a few more tests
annelo-msft Apr 8, 2023
2e3dbfc
Add test for writing property name with special character; make Write…
annelo-msft Apr 10, 2023
b8149ed
Merge remote-tracking branch 'upstream/main' into dynamicjson-escape-…
annelo-msft Apr 10, 2023
182ecaa
Use JsonDocument.WriteTo() to get consistent behavior wrt escaping.
annelo-msft Apr 10, 2023
112e69b
Update tests to compare JsonDocument.WriteTo() output to MJD.WriteTo()
annelo-msft Apr 11, 2023
6c70d51
Reorganize test helpers; add complex test cases
annelo-msft Apr 11, 2023
88123b1
Restore no-change stream optimization
annelo-msft Apr 11, 2023
64affd9
nits
annelo-msft Apr 11, 2023
aefbdce
Merge remote-tracking branch 'upstream/main' into dynamicjson-escape-…
annelo-msft Apr 11, 2023
b356986
Add test for object with changes to two primitive properties
annelo-msft Apr 12, 2023
de37204
Add tests for objects with added properties
annelo-msft Apr 12, 2023
828749d
Add trickier tests; fix bugs they found
annelo-msft Apr 12, 2023
38af0f7
dispose writer
annelo-msft Apr 12, 2023
ac2b8cc
Add test for interleaved changes to array values
annelo-msft Apr 12, 2023
e39d014
Let caller own Flush() calls
annelo-msft Apr 12, 2023
3834c7e
nits
annelo-msft Apr 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 0 additions & 277 deletions sdk/core/Azure.Core.Experimental/src/MutableJsonDocument.WriteTo.cs

This file was deleted.

6 changes: 3 additions & 3 deletions sdk/core/Azure.Core.Experimental/src/MutableJsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public void WriteTo(Stream stream, StandardFormat format = default)
throw new ArgumentOutOfRangeException(nameof(format));
}

Utf8JsonWriter writer = new Utf8JsonWriter(stream);
if (!Changes.HasChanges)
{
Write(stream, _original.Span);
stream.Flush();
return;
}

WriteRootElementTo(writer);
Utf8JsonWriter writer = new(stream);
RootElement.WriteTo(writer);
}

internal void WriteTo(Utf8JsonWriter writer)
Expand All @@ -75,7 +75,7 @@ internal void WriteTo(Utf8JsonWriter writer)
return;
}

WriteRootElementTo(writer);
RootElement.WriteTo(writer);
}

private static void Write(Stream stream, ReadOnlySpan<byte> buffer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Text.Json;

namespace Azure.Core.Json
{
public partial struct MutableJsonElement
{
internal void WriteTo(Utf8JsonWriter writer)
{
WriteElement(_path, _highWaterMark, _element, writer);
writer.Flush();
}

private void WriteElement(string path, int highWaterMark, JsonElement originalElement, Utf8JsonWriter writer)
{
if (Changes.TryGetChange(path, highWaterMark, out MutableJsonChange change))
{
change.AsJsonElement().WriteTo(writer);
return;
}

if (Changes.DescendantChanged(path, highWaterMark))
{
switch (originalElement.ValueKind)
{
case JsonValueKind.Object:
WriteObject(path, highWaterMark, originalElement, writer);
break;
case JsonValueKind.Array:
WriteArray(path, highWaterMark, originalElement, writer);
break;
default:
throw new InvalidOperationException("Element doesn't have descendants.");
}

return;
}

originalElement.WriteTo(writer);
}

private void WriteObject(string path, int highWaterMark, JsonElement originalElement, Utf8JsonWriter writer)
{
writer.WriteStartObject();

foreach (JsonProperty property in originalElement.EnumerateObject())
{
string propertyPath = MutableJsonDocument.ChangeTracker.PushProperty(path, property.Name);

if (Changes.TryGetChange(propertyPath, highWaterMark, out MutableJsonChange change))
{
writer.WritePropertyName(property.Name);
change.AsJsonElement().WriteTo(writer);
continue;
}

if (Changes.DescendantChanged(propertyPath, highWaterMark))
{
writer.WritePropertyName(property.Name);
WriteElement(propertyPath, highWaterMark, property.Value, writer);
continue;
}

property.WriteTo(writer);
}

writer.WriteEndObject();
}

private void WriteArray(string path, int highWaterMark, JsonElement originalElement, Utf8JsonWriter writer)
{
if (Changes.TryGetChange(path, highWaterMark, out MutableJsonChange change))
{
JsonElement changedElement = change.AsJsonElement();
changedElement.WriteTo(writer);
return;
}

writer.WriteStartArray();

int arrayIndex = 0;
foreach (JsonElement arrayElement in originalElement.EnumerateArray())
{
string arrayElementPath = MutableJsonDocument.ChangeTracker.PushIndex(path, arrayIndex++);
WriteElement(arrayElementPath, highWaterMark, arrayElement, writer);
}

writer.WriteEndArray();
}
}
}
Loading