Skip to content

Commit

Permalink
Don't throw if a property is missing from DynamicJson, to allow for o…
Browse files Browse the repository at this point in the history
…ptional property checks (#34026)

* Add tests for optional property scenarios

* Implement optional property check logic.

* Add back CanTestPropertyForNull test

* return null instead of DynamicJson(
ull)

* raw string literals
  • Loading branch information
annelo-msft authored Feb 11, 2023
1 parent 5db3f5b commit 2606ffd
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
11 changes: 8 additions & 3 deletions sdk/core/Azure.Core.Experimental/src/DynamicJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ internal override void WriteTo(Stream stream)
writer.Flush();
}

private object GetProperty(string name)
private object? GetProperty(string name)
{
return new DynamicJson(_element.GetProperty(name));
if (_element.TryGetProperty(name, out MutableJsonElement element))
{
return new DynamicJson(element);
}

return null;
}

private object GetViaIndexer(object index)
private object? GetViaIndexer(object index)
{
switch (index)
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core.Experimental/src/MutableJsonElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public MutableJsonElement GetProperty(string name)
{
if (!TryGetProperty(name, out MutableJsonElement value))
{
throw new InvalidOperationException($"{_path} does not containe property called {name}");
throw new InvalidOperationException($"{_path} does not contain property called {name}");
}

return value;
Expand Down
64 changes: 63 additions & 1 deletion sdk/core/Azure.Core.Experimental/tests/DynamicJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,68 @@ public void CanMakeChangesAndAddNewProperty()
Assert.AreEqual(2, (int)jsonData.Bar);
}

[Test]
public void CanCheckOptionalProperty()
{
dynamic json = GetDynamicJson("""
{
"Foo" : "foo"
}
""");

// Property is present
Assert.IsFalse(json.Foo == null);

// Property is absent
Assert.IsTrue(json.Bar == null);
}

[Test]
public void CanCheckOptionalPropertyWithChanges()
{
dynamic json = GetDynamicJson("""
{
"Foo" : "foo",
"Bar" : {
"A" : "a"
}
}
""");

// Add property Baz
json.Baz = "baz";

// Remove property A
json.Bar = new { B = "b" };

// Properties are present
Assert.IsFalse(json.Foo == null);
Assert.IsFalse(json.Bar.B == null);
Assert.IsFalse(json.Baz == null);

// Properties are absent
Assert.IsTrue(json.Bar.A == null);
}

[Test]
public void CanSetOptionalProperty()
{
dynamic json = GetDynamicJson("""
{
"Foo" : "foo"
}
""");

// Property is absent
Assert.IsTrue(json.OptionalValue == null);

json.OptionalValue = 5;

// Property is present
Assert.IsFalse(json.OptionalValue == null);
Assert.AreEqual(5, (int)json.OptionalValue);
}

#region Helpers
internal static dynamic GetDynamicJson(string json)
{
Expand All @@ -300,6 +362,6 @@ internal static dynamic GetDynamicJson(string json)
internal class CustomType
{
}
#endregion
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void CanGetArrayPropertyIndex()
public void CanGetObjectMemberViaArrayIndex()
{
dynamic data = JsonDataTestHelpers.CreateFromJson(
@"{ ""value"": [ { ""tag"": ""tagValue"" }, 2, 3] }"
"""{ "value": [ { "tag": "tagValue" }, 2, 3] }"""
);

Assert.AreEqual("tagValue", (string)data.value[0].tag);
Expand Down Expand Up @@ -225,7 +225,7 @@ public void CanSetArrayPropertyIndex()
public void CanSetObjectMemberViaArrayIndex()
{
dynamic data = JsonDataTestHelpers.CreateFromJson(
@"{ ""value"": [ { ""tag"": ""tagValue"" }, 2, 3] }"
"""{ "value": [ { "tag": "tagValue" }, 2, 3] }"""
);

data.value[0].tag = "newValue";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public void CanAccessProperties()
}

[Test]
[Ignore("To be implemented.")]
public void CanTestPropertyForNull()
{
dynamic jsonData = new BinaryData("{ \"primitive\":\"Hello\", \"nested\": { \"nestedPrimitive\":true } }").ToDynamic();
Expand Down

0 comments on commit 2606ffd

Please sign in to comment.