Skip to content

Commit

Permalink
Don't omit-nulls for annotated properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
biaol-odata committed Nov 9, 2018
1 parent 2a5b9b1 commit fd97a44
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ internal void WriteProperties(
owningType,
false /* isTopLevel */,
!isComplexValue,
omitNullValues,
// Annotated properties won't be omitted even if it is null.
omitNullValues && property.InstanceAnnotations.Count == 0,
duplicatePropertyNameChecker);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OData.Core/JsonLight/ODataJsonLightWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ protected override void StartResource(ODataResource resource)

this.jsonLightResourceSerializer.InstanceAnnotationWriter.WriteInstanceAnnotations(parentNavLink.GetInstanceAnnotations(), parentNavLink.Name);

if (!this.ShouldOmitNullValues())
// Annotated resource won't be omitted even if it is null.
if (!this.ShouldOmitNullValues() || parentNavLink.GetInstanceAnnotations().Count > 0)
{
// Write the property name of an expanded navigation property to start the value of null per preference setting.
this.jsonWriter.WriteName(parentNavLink.Name);
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.OData.Core/ShippingAssemblyAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.Test.Taupo.OData" + AssemblyRef.TestPublicKey)]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.OData.Core.Tests" + AssemblyRef.TestPublicKey)]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.OData.Client.Tests" + AssemblyRef.TestPublicKey)]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.Test.OData.Tests.Client" + AssemblyRef.TestPublicKey)]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.Test.OData.DependencyInjection.NetCore" + AssemblyRef.TestPublicKey)]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.OData.Core.Tests.NetCore" + AssemblyRef.TestPublicKey)]
#pragma warning restore 436
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ public void ReadWriteNullExpandedNavigationTest_notOmitNulls()
Uri serviceUri = new Uri("http://test/");

string expectedNulls = @"{
""@odata.context"":""http://test/$metadata#employees(Name,Title,Dynamic,Address,Manager(),Friend())/$entity"",
""Name"":""Fred"",
""[email protected]"":""annotationValue"",""Title"":null,
""Dynamic"":null,
""[email protected]"":""annotationValue"",""DynamicAnnotated"":null,
""Address"":null,""Manager"":null,
""Friend"": {""Name"":""FriendOfFred""}
}";
""@odata.context"":""http://test/$metadata#employees(Name,Title,Dynamic,Address,Manager(),Friend())/$entity"",
""Name"":""Fred"",
""[email protected]"":""annotationValue"",""Title"":null,
""Dynamic"":null,
""[email protected]"":""annotationValue"",""DynamicAnnotated"":null,
""[email protected]"":""InsufficientPrivileges"",""Address"":null,
""Manager"":null,
""Friend"": {""Name"":""FriendOfFred""}
}";
expectedNulls = Regex.Replace(expectedNulls, @"\s*", string.Empty, RegexOptions.Multiline);
Assert.IsTrue(expectedNulls.Contains(serviceUri.ToString()));

Expand Down Expand Up @@ -180,12 +181,13 @@ public void ReadWriteNullNavigationTest_omitNulls()

// Requirement: Omit null value for nested resource if omit-values=nulls is specified.
string expectedNoNulls = @"{
""@odata.context"":""http://test/$metadata#employees(Name,Title,Dynamic,Address,Manager,Friend())/$entity"",
""Name"":""Fred"",
""[email protected]"":""annotationValue"",
""[email protected]"":""annotationValue"",
""Friend"": {""Name"":""FriendOfFred""}
}";
""@odata.context"":""http://test/$metadata#employees(Name,Title,Dynamic,Address,Manager,Friend())/$entity"",
""Name"":""Fred"",
""[email protected]"":""annotationValue"",""Title"":null,
""[email protected]"":""annotationValue"",""DynamicAnnotated"":null,
""[email protected]"":""InsufficientPrivileges"",""Address"":null,
""Friend"": {""Name"":""FriendOfFred""}
}";

expectedNoNulls = Regex.Replace(expectedNoNulls, @"\s*", string.Empty, RegexOptions.Multiline);
Assert.IsTrue(expectedNoNulls.Contains(serviceUri.ToString()));
Expand Down Expand Up @@ -263,13 +265,13 @@ public void ReadWriteNullNavigationTest_omitNulls()
}

Assert.IsTrue(employeeEntityCount == 2, $"employeeEntityCount: Expected: 2, Actual: {employeeEntityCount}");
Assert.IsFalse(addressReturned, "Address is returned but it should not be.");
Assert.IsTrue(addressReturned, "Address is returned but it should not be.");
Assert.IsFalse(managerReturned, "Manager is returned but it should not be, because it is navigation link and its value is omitted in payload.");
Assert.IsNotNull(friendResource, "Friend resource is null but it should not be.");
VerifyAdditionalProperties(employeeResource);
Assert.IsTrue(employeeResource.Properties != null);
Assert.AreEqual(employeeResource.Properties.Count(), 5);
Assert.IsTrue(employeeResource.Properties.Any(p => p.Name.Equals("Address") && (p.Value == null)));
Assert.AreEqual(4, employeeResource.Properties.Count());
Assert.IsFalse(employeeResource.Properties.Any(p => p.Name.Equals("Address")));
Assert.IsFalse(employeeResource.Properties.Any(p => p.Name.Equals("Manager")));
}

Expand All @@ -287,18 +289,20 @@ private void ReadWriteNullExpandedNavigationTest_omitNulls(bool nestedSelect)
// Requirement: Omit null value for nested resource if omit-values=nulls is specified.
string expectedNoNulls = nestedSelect
? @"{
""@odata.context"":""http://test/$metadata#employees(Name,Title,Dynamic,Address,Manager(),Friend(Name,Title))/$entity"",
""Name"":""Fred"",
""[email protected]"":""annotationValue"",
""[email protected]"":""annotationValue"",
""Friend"": {""Name"":""FriendOfFred""}
""@odata.context"":""http://test/$metadata#employees(Name,Title,Dynamic,Address,Manager(),Friend(Name,Title))/$entity"",
""Name"":""Fred"",
""[email protected]"":""annotationValue"",""Title"":null,
""[email protected]"":""annotationValue"",""DynamicAnnotated"":null,
""[email protected]"":""InsufficientPrivileges"",""Address"":null,
""Friend"": {""Name"":""FriendOfFred""}
}"
: @"{
""@odata.context"":""http://test/$metadata#employees(Name,Title,Dynamic,Address,Manager(),Friend())/$entity"",
""Name"":""Fred"",
""[email protected]"":""annotationValue"",
""[email protected]"":""annotationValue"",
""Friend"": {""Name"":""FriendOfFred""}
""@odata.context"":""http://test/$metadata#employees(Name,Title,Dynamic,Address,Manager(),Friend())/$entity"",
""Name"":""Fred"",
""[email protected]"":""annotationValue"",""Title"":null,
""[email protected]"":""annotationValue"",""DynamicAnnotated"":null,
""[email protected]"":""InsufficientPrivileges"",""Address"":null,
""Friend"": {""Name"":""FriendOfFred""}
}";

expectedNoNulls = Regex.Replace(expectedNoNulls, @"\s*", string.Empty, RegexOptions.Multiline);
Expand Down Expand Up @@ -382,7 +386,7 @@ private void ReadWriteNullExpandedNavigationTest_omitNulls(bool nestedSelect)
}

Assert.IsTrue(employeeEntityCount == 2, $"employeeEntityCount: Expected: 2, Actual: {employeeEntityCount}");
Assert.IsFalse(addressReturned, "Address is returned but it should not be.");
Assert.IsTrue(addressReturned, "Address is annotated, should be returned but isn't.");
Assert.IsFalse(managerReturned, "Manager is returned but it should not be, because value is omitted in payload.");

Assert.IsNotNull(friendResource, "Friend resource is null but it should not be.");
Expand Down Expand Up @@ -457,11 +461,16 @@ private void WriteResponse(ODataWriter writer)
});

// write address
writer.WriteStart(new ODataNestedResourceInfo
ODataNestedResourceInfo addrNestedResourceInfo = new ODataNestedResourceInfo
{
Name = "Address",
IsCollection = false
});
};
addrNestedResourceInfo.SetInstanceAnnotations(new ODataInstanceAnnotation[]
{ new ODataInstanceAnnotation("test.annotation", new ODataPrimitiveValue("InsufficientPrivileges")) }
);

writer.WriteStart(addrNestedResourceInfo);
writer.WriteStart((ODataResource)null);
writer.WriteEnd(); //address
writer.WriteEnd(); //address nestedInfo
Expand Down

0 comments on commit fd97a44

Please sign in to comment.