Skip to content

Commit

Permalink
fixes #2661. Write the nested resource within nested collection in co…
Browse files Browse the repository at this point in the history
…llection (#2666)

* fixes #2661. Write the nested resource within nested collection in collection

* fixes the failing test
  • Loading branch information
xuzhg authored May 15, 2023
1 parent 5638479 commit 51b4676
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Microsoft.OData.Core/ODataWriterCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2932,7 +2932,7 @@ private void PromoteNestedResourceInfoScope(ODataItem content)
NestedResourceInfoScope newScope = previousScope.Clone(WriterState.NestedResourceInfoWithContent);

this.scopeStack.Push(newScope);
if (newScope.ItemType == null && content != null && !SkipWriting && !(content is ODataPrimitiveValue))
if ((newScope.ItemType == null || newScope.ItemType.IsUntyped()) && content != null && !SkipWriting && !(content is ODataPrimitiveValue))
{
ODataPrimitiveValue primitiveValue = content as ODataPrimitiveValue;
if (primitiveValue != null)
Expand All @@ -2941,10 +2941,18 @@ private void PromoteNestedResourceInfoScope(ODataItem content)
}
else
{
ODataResourceBase resource = content as ODataResourceBase;
newScope.ResourceType = resource != null
? GetResourceType(resource)
: GetResourceSetType(content as ODataResourceSetBase);
if (content is ODataResourceBase resource)
{
newScope.ResourceType = string.IsNullOrEmpty(resource.TypeName) ?
EdmUntypedStructuredType.Instance :
GetResourceType(resource);
}
else if (content is ODataResourceSetBase resourceSet)
{
newScope.ResourceType = string.IsNullOrEmpty(resourceSet.TypeName) ?
EdmUntypedStructuredType.Instance :
GetResourceSetType(resourceSet);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,45 @@ public void WriteResourceDeclaredCollectionUntypedProperty_WorksForCollectionVal
Assert.Equal("{\"@odata.context\":\"http://www.sampletest.com/$metadata#serverEntitySet/$entity\",\"Infos\":[\"abc\",null,42]}", result);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void WriteResourceCollectionUntypedProperty_WorksResourceInNestedCollectionOfCollection2(bool isOpen)
{
string typeName = isOpen ? "Server.NS.ServerOpenEntityType" : "Server.NS.ServerEntityType";
EdmEntitySet entitySet = isOpen ? this.serverOpenEntitySet : this.serverEntitySet;
EdmEntityType entityType = isOpen ? this.serverOpenEntityType : this.serverEntityType;
string propertyName = isOpen ? "AnyDynamic" : "Infos";

string actual = WriteEntryPayload(entitySet, entityType,
writer =>
{
writer.WriteStart(new ODataResource { TypeName = typeName });
writer.WriteStart(new ODataNestedResourceInfo { Name = propertyName, IsCollection = true });
writer.WriteStart(new ODataResourceSet { TypeName = "Collection(Edm.Untyped)" });
writer.WriteStart(new ODataResourceSet());
writer.WriteStart(new ODataResource
{
TypeName = "Edm.Untyped",
Properties = new ODataProperty[]
{
new ODataProperty { Name = "FirstName", Value = "Kerry"}
}
});
writer.WriteEnd(); // End of "Edm.Untyped"
writer.WriteEnd();
writer.WriteEnd(); // End of "Infos" / AnyDynamic
writer.WriteEnd();
writer.WriteEnd();
});

string result = isOpen ?
"{\"@odata.context\":\"http://www.sampletest.com/$metadata#serverOpenEntitySet/$entity\",\"AnyDynamic\":[[{\"FirstName\":\"Kerry\"}]]}" :
"{\"@odata.context\":\"http://www.sampletest.com/$metadata#serverEntitySet/$entity\",\"Infos\":[[{\"FirstName\":\"Kerry\"}]]}";

Assert.Equal(result, actual);
}

private string WriteDeclaredUntypedProperty(ODataProperty untypedProperty)
{
var entry = new ODataResource
Expand Down

0 comments on commit 51b4676

Please sign in to comment.