diff --git a/src/Microsoft.OData.Core/ODataNestedResourceInfo.cs b/src/Microsoft.OData.Core/ODataNestedResourceInfo.cs
index dd8b1c7e66..bee3eb6e1f 100644
--- a/src/Microsoft.OData.Core/ODataNestedResourceInfo.cs
+++ b/src/Microsoft.OData.Core/ODataNestedResourceInfo.cs
@@ -36,7 +36,7 @@ public sealed class ODataNestedResourceInfo : ODataItem
private bool hasAssociationUrl;
/// The number of items in the resource set.
- private long count;
+ private long? count;
/// Gets or sets a value that indicates whether the nested resource info represents a collection or a resource.
/// true if the nested resource info represents a collection; false if the navigation represents a resource.
@@ -113,7 +113,7 @@ public long? Count
throw new ODataException(Strings.ODataWriterCore_QueryCountInODataNestedResourceInfo);
}
- this.count = (long)value;
+ this.count = value;
}
}
diff --git a/test/FunctionalTests/Microsoft.OData.Core.Tests/IntegrationTests/Writer/InstanceAnnotationWriterIntegrationTests.cs b/test/FunctionalTests/Microsoft.OData.Core.Tests/IntegrationTests/Writer/InstanceAnnotationWriterIntegrationTests.cs
index 43466e370d..968dd6d878 100644
--- a/test/FunctionalTests/Microsoft.OData.Core.Tests/IntegrationTests/Writer/InstanceAnnotationWriterIntegrationTests.cs
+++ b/test/FunctionalTests/Microsoft.OData.Core.Tests/IntegrationTests/Writer/InstanceAnnotationWriterIntegrationTests.cs
@@ -583,6 +583,26 @@ public void WriteAnnotationAndCountAtStartExpandedFeedShouldPassInJsonLight()
this.WriteAnnotationsAndValidatePayload(action, EntitySet, ODataFormat.Json, expectedPayload, request: false, createFeedWriter: false);
}
+
+ [Fact]
+ public void WriteCountOnExpandedEntryShouldFail()
+ {
+ Action action = (odataWriter) =>
+ {
+ var entryToWrite = new ODataResource { Properties = new[] { new ODataProperty { Name = "ID", Value = 1 } } };
+ odataWriter.WriteStart(entryToWrite);
+
+ ODataNestedResourceInfo navLink = new ODataNestedResourceInfo { Name = "ResourceNavigationProperty", IsCollection = false };
+ navLink.Count = 10;
+
+ odataWriter.WriteStart(navLink);
+ odataWriter.WriteEnd();
+ odataWriter.WriteEnd();
+ };
+
+ Action testResponse = () => this.WriteAnnotationsAndValidatePayload(action, EntitySet, ODataFormat.Json, null, request: false, createFeedWriter: false);
+ testResponse.Throws(Strings.ODataWriterCore_QueryCountInODataNestedResourceInfo);
+ }
#endregion Writing instance annotations on expanded feeds
#region Write Delta Feed