/// <summary> /// Start writing a delta feed. /// </summary> /// <param name="deltaFeed">Delta feed/collection to write.</param> public abstract void WriteStart(ODataDeltaFeed deltaFeed);
/// <summary> /// Asynchronously start writing a delta feed. /// </summary> /// <param name="deltaFeed">Delta feed/collection to write.</param> /// <returns>A task instance that represents the asynchronous write operation.</returns> public abstract Task WriteStartAsync(ODataDeltaFeed deltaFeed);
private void WriteAnnotationAtEndInDeltaFeed(string expectedPayload, long? count, Uri nextLink, Uri deltaLink) { Action<ODataDeltaWriter> action = (odataWriter) => { var feedToWrite = new ODataDeltaFeed { Id = new Uri("urn:feedId") }; odataWriter.WriteStart(feedToWrite); feedToWrite.InstanceAnnotations.Add(new ODataInstanceAnnotation("Custom.EndAnnotation", PrimitiveValue1)); feedToWrite.Count = count; feedToWrite.NextPageLink = nextLink; feedToWrite.DeltaLink = deltaLink; odataWriter.WriteEnd(); }; WriteDeltaFeedAnnotationsAndValidatePayload(action, EntitySet, expectedPayload); }
public static void WriteDeltaFeed(ODataDeltaWriter deltaWriter, List<ODataItem> items, bool? countOption, Uri newDeltaLink) { var deltaFeed = new ODataDeltaFeed { DeltaLink = newDeltaLink }; if (countOption == true) { deltaFeed.Count = items.Cast<object>().Count(); } deltaWriter.WriteStart(deltaFeed); foreach (ODataItem item in items) { var entry = item as ODataEntry; var deletedEntry = item as ODataDeltaDeletedEntry; var deltaLink = item as ODataDeltaLink; var deltaDeletedLink = item as ODataDeltaDeletedLink; if (entry != null) { deltaWriter.WriteStart(entry); deltaWriter.WriteEnd(); } else if (deletedEntry != null) { deltaWriter.WriteDeltaDeletedEntry(deletedEntry); } else if (deltaLink != null) { deltaWriter.WriteDeltaLink(deltaLink); } else if (deltaDeletedLink != null) { deltaWriter.WriteDeltaDeletedLink(deltaDeletedLink); } } deltaWriter.WriteEnd(); }
/// <summary> /// Provide additional serialization information to the <see cref="ODataDeltaWriter"/> for <paramref name="deltaFeed"/>. /// </summary> /// <param name="deltaFeed">The instance to set the serialization info.</param> /// <param name="serializationInfo">The serialization info to set.</param> public static void SetSerializationInfo(this ODataDeltaFeed deltaFeed, ODataDeltaFeedSerializationInfo serializationInfo) { ExceptionUtils.CheckArgumentNotNull(deltaFeed, "deltaFeed"); deltaFeed.SerializationInfo = serializationInfo; }
public override void Process(IODataRequestMessage requestMessage, IODataResponseMessage responseMessage) { var token = HttpUtility.ParseQueryString(RequestUri.Query).Get("$token"); if (string.IsNullOrEmpty(token)) { deltaToken = "common"; } else { deltaToken = token; } if (deltaToken == "common") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "Customers?$expand=Orders"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var entitySet = this.DataSource.Model.FindDeclaredEntitySet("Customers"); var entityType = entitySet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(entitySet, entityType); var deltaFeed = new ODataDeltaFeed(); var deltaEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), Properties = new[] {new ODataProperty {Name = "FirstName", Value = "GGGG"}} }; var deletedLink = new ODataDeltaDeletedLink( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(8)"), "Orders"); var addedLink = new ODataDeltaLink( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(7)"), "Orders"); var navigationEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Orders(100)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", Properties = new[] { new ODataProperty {Name = "OrderID", Value = 100}, new ODataProperty {Name = "OrderDate", Value = new DateTimeOffset(DateTime.Now)} } }; navigationEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", NavigationSourceKind = EdmNavigationSourceKind.EntitySet, NavigationSourceName = "Orders" }); var deletedEntry = new ODataDeltaDeletedEntry( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(2)").AbsoluteUri, DeltaDeletedEntryReason.Deleted); deltaFeed.DeltaLink = new Uri(ServiceConstants.ServiceBaseUri, "$delta?$token=common"); deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedLink(deletedLink); deltaWriter.WriteDeltaLink(addedLink); deltaWriter.WriteStart(navigationEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedEntry(deletedEntry); deltaWriter.WriteEnd(); } } else if (deltaToken == "containment") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments?$expand=BillingStatements"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var accountsSet = this.DataSource.Model.FindDeclaredEntitySet("Accounts"); var accountType = accountsSet.EntityType(); var myPisNav = accountType.FindProperty("MyPaymentInstruments") as IEdmNavigationProperty; var piSet = accountsSet.FindNavigationTarget(myPisNav); var piType = piSet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(piSet as IEdmContainedEntitySet, piType); var deltaFeed = new ODataDeltaFeed(); var deltaEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)"), Properties = new[] { new ODataProperty { Name = "FriendlyName", Value = "GGGG" } } }; var deletedEntry = new ODataDeltaDeletedEntry( new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements(103901001)").AbsoluteUri, DeltaDeletedEntryReason.Deleted); deletedEntry.SetSerializationInfo(new ODataDeltaSerializationInfo() { NavigationSourceName = "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements" }); var deletedLink = new ODataDeltaDeletedLink( new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)"), new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements(103901001)"), "BillingStatements"); var navigationEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements(103901005)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Statement", Properties = new[] { new ODataProperty { Name = "TransactionType", Value = "OnlinePurchase" }, new ODataProperty { Name = "TransactionDescription", Value = "unknown purchase" }, new ODataProperty { Name = "Amount", Value = 32.1 } } }; navigationEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Microsoft.Test.OData.Services.ODataWCFService.Statement", NavigationSourceKind = EdmNavigationSourceKind.ContainedEntitySet, NavigationSourceName = "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements" }); var addedLink = new ODataDeltaLink( new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)"), new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements(103901005)"), "BillingStatements"); deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedEntry(deletedEntry); deltaWriter.WriteDeltaDeletedLink(deletedLink); deltaWriter.WriteStart(navigationEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaLink(addedLink); deltaWriter.WriteEnd(); } } else if (deltaToken == "derived") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "People?$expand=Microsoft.Test.OData.Services.ODataWCFService.Customer/Orders"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var peopleSet = this.DataSource.Model.FindDeclaredEntitySet("People"); var personType = peopleSet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(peopleSet, personType); var deltaFeed = new ODataDeltaFeed(); var deltaEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "People(1)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Customer", Properties = new[] { new ODataProperty { Name = "City", Value = "GGGG" } } }; var addedLink = new ODataDeltaLink( new Uri(ServiceConstants.ServiceBaseUri, "People(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(7)"), "Orders"); var deletedEntry = new ODataDeltaDeletedEntry( new Uri(ServiceConstants.ServiceBaseUri, "People(2)").AbsoluteUri, DeltaDeletedEntryReason.Changed); var deletedLink = new ODataDeltaDeletedLink( new Uri(ServiceConstants.ServiceBaseUri, "People(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(8)"), "Orders"); var navigationEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Orders(100)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", Properties = new[] { new ODataProperty {Name = "OrderID", Value = 100}, new ODataProperty {Name = "OrderDate", Value = new DateTimeOffset(DateTime.Now)} } }; navigationEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", NavigationSourceKind = EdmNavigationSourceKind.EntitySet, NavigationSourceName = "Orders" }); deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedLink(deletedLink); deltaWriter.WriteDeltaLink(addedLink); deltaWriter.WriteStart(navigationEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedEntry(deletedEntry); deltaWriter.WriteEnd(); } } else if (deltaToken == "expanded") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "Customers?$expand=Orders"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var customerSet = this.DataSource.Model.FindDeclaredEntitySet("Customers"); var orderSet = this.DataSource.Model.FindDeclaredEntitySet("Orders"); var peopleSet = this.DataSource.Model.FindDeclaredEntitySet("People"); var customerType = customerSet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(customerSet, customerType); // Delta feed and entry var deltaFeed = new ODataDeltaFeed { DeltaLink = new Uri(ServiceConstants.ServiceBaseUri, "$delta?$token=expanded") }; var deltaEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, customerSet.Name + "(1)"), Properties = new[] { new ODataProperty { Name = "FirstName", Value = "GGGG" } } }; // Expanded feed var navigationLink = new ODataNavigationLink() { Name = "Orders", IsCollection = true, }; var expandedFeed = new ODataFeed(); var expandedEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, orderSet.Name + "(8)"), Properties = new[] { new ODataProperty { Name = "OrderDate", Value = new DateTimeOffset(2011, 3, 4, 16, 03, 57, TimeSpan.FromHours(-8)) }, new ODataProperty { Name = "OrderID", Value = 8 }, new ODataProperty { Name = "OrderShelfLifes", Value = new ODataCollectionValue { Items = new[] { new TimeSpan(1) } } }, new ODataProperty { Name = "ShelfLife", Value = new TimeSpan(1) }, new ODataProperty { Name = "ShipDate", Value = new Date(2014, 8, 12) }, new ODataProperty { Name = "ShipTime", Value = new TimeOfDay(6, 5, 30, 0) }, } }; // Expanded entry var navigationLinkSingle = new ODataNavigationLink() { Name = "Parent", IsCollection = false, }; var expandedEntrySingle = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, peopleSet.Name + "(2)"), Properties = new[] { new ODataProperty { Name = "FirstName", Value = "Jill" }, new ODataProperty { Name = "LastName", Value = "Jones" }, new ODataProperty { Name = "Numbers", Value = new ODataCollectionValue() }, new ODataProperty { Name = "Emails", Value = new ODataCollectionValue() }, new ODataProperty { Name = "PersonID", Value = 2 }, new ODataProperty { Name = "Home", Value = GeographyPoint.Create(15.0, 161.8) }, } }; // Delta feed and entry deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry); // Expanded feed deltaWriter.WriteStart(navigationLink); deltaWriter.WriteStart(expandedFeed); deltaWriter.WriteStart(expandedEntry); deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); // Expanded entry deltaWriter.WriteStart(navigationLinkSingle); deltaWriter.WriteStart(expandedEntrySingle); deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); // Delta feed and entry deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); } } else if (deltaToken == "projection") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "Customers?$select=PersonID,FirstName,LastName&$expand=Orders($select=OrderID,OrderDate)"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var entitySet = this.DataSource.Model.FindDeclaredEntitySet("Customers"); var entityType = entitySet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(entitySet, entityType); var deltaFeed = new ODataDeltaFeed(); var deltaEntry1 = new ODataEntry { Properties = new[] { new ODataProperty { Name = "PersonID", Value = 1 }, new ODataProperty { Name = "FirstName", Value = "FFFF" }, new ODataProperty { Name = "LastName", Value = "LLLL" }, new ODataProperty { Name = "City", Value = "Beijing" } } }; var deletedLink = new ODataDeltaDeletedLink( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(8)"), "Orders"); var addedLink = new ODataDeltaLink( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(7)"), "Orders"); var navigationEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Orders(100)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", Properties = new[] { new ODataProperty {Name = "OrderID", Value = 100}, new ODataProperty {Name = "OrderDate", Value = new DateTimeOffset(DateTime.Now)} } }; navigationEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", NavigationSourceKind = EdmNavigationSourceKind.EntitySet, NavigationSourceName = "Orders" }); var deletedOrderEntry = new ODataDeltaDeletedEntry( new Uri(ServiceConstants.ServiceBaseUri, "Orders(20)").AbsoluteUri, DeltaDeletedEntryReason.Deleted); deletedOrderEntry.SetSerializationInfo(new ODataDeltaSerializationInfo() { NavigationSourceName = "Orders" }); var deltaEntry2 = new ODataEntry { Properties = new[] { new ODataProperty { Name = "PersonID", Value = 2 }, new ODataProperty { Name = "FirstName", Value = "AAAA" }, } }; deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry1); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedLink(deletedLink); deltaWriter.WriteDeltaLink(addedLink); deltaWriter.WriteStart(navigationEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedEntry(deletedOrderEntry); deltaWriter.WriteStart(deltaEntry2); deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); } } }
public void WriteDeltaFeedInline_Sets_NextPageLink_OnWriteEnd() { // Arrange IEnumerable instance = new object[0]; ODataDeltaFeed deltafeed = new ODataDeltaFeed { NextPageLink = new Uri("http://nextlink.com/") }; Mock<ODataDeltaFeedSerializer> serializer = new Mock<ODataDeltaFeedSerializer>(new DefaultODataSerializerProvider()); serializer.CallBase = true; serializer.Setup(s => s.CreateODataDeltaFeed(instance, _customersType, _writeContext)).Returns(deltafeed); var mockWriter = new Mock<ODataDeltaWriter>(); mockWriter.Setup(m => m.WriteStart(It.Is<ODataDeltaFeed>(f => f.NextPageLink == null))).Verifiable(); mockWriter .Setup(m => m.WriteEnd()) .Callback(() => { Assert.Equal("http://nextlink.com/", deltafeed.NextPageLink.AbsoluteUri); }) .Verifiable(); // Act serializer.Object.WriteDeltaFeedInline(instance, _customersType, mockWriter.Object, _writeContext); // Assert mockWriter.Verify(); }
/// <summary> /// Create the <see cref="ODataDeltaFeed"/> to be written for the given feed instance. /// </summary> /// <param name="feedInstance">The instance representing the feed being written.</param> /// <param name="feedType">The EDM type of the feed being written.</param> /// <param name="writeContext">The serializer context.</param> /// <returns>The created <see cref="ODataDeltaFeed"/> object.</returns> public virtual ODataDeltaFeed CreateODataDeltaFeed(IEnumerable feedInstance, IEdmCollectionTypeReference feedType, ODataSerializerContext writeContext) { ODataDeltaFeed feed = new ODataDeltaFeed(); if (writeContext.ExpandedEntity == null) { // If we have more OData format specific information apply it now, only if we are the root feed. PageResult odataFeedAnnotations = feedInstance as PageResult; if (odataFeedAnnotations != null) { feed.Count = odataFeedAnnotations.Count; feed.NextPageLink = odataFeedAnnotations.NextPageLink; } else if (writeContext.Request != null) { feed.NextPageLink = writeContext.Request.ODataProperties().NextLink; long? countValue = writeContext.Request.ODataProperties().TotalCount; if (countValue.HasValue) { feed.Count = countValue.Value; } } } else { // nested feed ITruncatedCollection truncatedCollection = feedInstance as ITruncatedCollection; if (truncatedCollection != null && truncatedCollection.IsTruncated) { feed.NextPageLink = GetNestedNextPageLink(writeContext, truncatedCollection.PageSize); } } return feed; }
public void WriteDeltaFeedInline_Writes_CreateODataFeedOutput() { // Arrange IEnumerable instance = new object[0]; ODataDeltaFeed deltafeed = new ODataDeltaFeed(); Mock<ODataDeltaFeedSerializer> serializer = new Mock<ODataDeltaFeedSerializer>(new DefaultODataSerializerProvider()); serializer.CallBase = true; serializer.Setup(s => s.CreateODataDeltaFeed(instance, _customersType, _writeContext)).Returns(deltafeed); Mock<ODataDeltaWriter> writer = new Mock<ODataDeltaWriter>(); writer.Setup(s => s.WriteStart(deltafeed)).Verifiable(); // Act serializer.Object.WriteDeltaFeedInline(instance, _customersType, writer.Object, _writeContext); // Assert writer.Verify(); }
public void WriteDerivedEntityWithSerilizationInfo() { this.TestInit(this.GetModel()); ODataDeltaFeed feed = new ODataDeltaFeed(); ODataEntry derivedEntity = new ODataEntry() { TypeName = "MyNS.PhysicalProduct", Properties = new[] { new ODataProperty {Name = "Id", Value = new ODataPrimitiveValue(1)}, new ODataProperty {Name = "Name", Value = new ODataPrimitiveValue("car")}, new ODataProperty {Name = "Material", Value = new ODataPrimitiveValue("gold")}, }, SerializationInfo = new ODataFeedAndEntrySerializationInfo() { ExpectedTypeName = "MyNS.Product", NavigationSourceEntityTypeName = "MyNS.Product", NavigationSourceName = "Products", } }; ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetCustomers(), this.GetCustomerType()); writer.WriteStart(feed); writer.WriteStart(derivedEntity); writer.WriteEnd(); writer.WriteEnd(); writer.Flush(); string payload = this.TestFinish(); payload.Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Customers/$delta\",\"value\":[{\"@odata.context\":\"http://host/service/$metadata#Products/$entity\",\"@odata.type\":\"#MyNS.PhysicalProduct\",\"Id\":1,\"Name\":\"car\",\"Material\":\"gold\"}]}"); }
public void WriteEntityInDeltaFeedWithSelectExpand() { this.TestInit(this.GetModel()); ODataDeltaFeed feed = new ODataDeltaFeed(); ODataEntry orderEntry= new ODataEntry() { Properties = new List<ODataProperty> { new ODataProperty { Name = "ShippingAddress", Value = new ODataComplexValue { Properties = new List<ODataProperty> { new ODataProperty { Name = "City", Value = "Shanghai" }, } } } }, SerializationInfo = new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Order", NavigationSourceKind = EdmNavigationSourceKind.EntitySet, NavigationSourceName = "Orders" }, }; var result = new ODataQueryOptionParser(this.GetModel(), this.GetCustomerType(), this.GetCustomers(), new Dictionary<string, string> { { "$expand", "Orders($select=ShippingAddress)" }, { "$select", "ContactName" } }).ParseSelectAndExpand(); ODataUri odataUri = new ODataUri() { ServiceRoot = new Uri("http://host/service"), SelectAndExpand = result }; var outputContext = CreateJsonLightOutputContext(this.stream, this.GetModel(), odataUri); ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetProducts(), this.GetProductType()); writer.WriteStart(feed); writer.WriteStart(orderEntry); writer.WriteEnd(); writer.WriteEnd(); writer.Flush(); string payload = this.TestFinish(); payload.Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Products(ContactName,Orders,Orders(ShippingAddress))/$delta\",\"value\":[{\"@odata.context\":\"http://host/service/$metadata#Orders/$entity\",\"ShippingAddress\":{\"City\":\"Shanghai\"}}]}"); }
public void WriteContainedEntityInDeltaFeedWithSelectExpand() { this.TestInit(this.GetModel()); ODataDeltaFeed feed = new ODataDeltaFeed(); ODataEntry entry = new ODataEntry() { TypeName = "MyNS.Product", Properties = new[] { new ODataProperty {Name = "Id", Value = new ODataPrimitiveValue(1)}, new ODataProperty {Name = "Name", Value = new ODataPrimitiveValue("Car")}, }, }; ODataEntry containedEntry = new ODataEntry() { TypeName = "MyNS.ProductDetail", Properties = new[] { new ODataProperty {Name = "Id", Value = new ODataPrimitiveValue(1)}, new ODataProperty {Name = "Detail", Value = new ODataPrimitiveValue("made in china")}, }, }; containedEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceEntityTypeName = "MyNS.ProductDetail", NavigationSourceName = "Products(1)/Details", NavigationSourceKind = EdmNavigationSourceKind.ContainedEntitySet }); var result = new ODataQueryOptionParser(this.GetModel(), this.GetProductType(), this.GetProducts(), new Dictionary<string, string> { { "$expand", "Details($select=Detail)" }, { "$select", "Name" } }).ParseSelectAndExpand(); ODataUri odataUri = new ODataUri() { ServiceRoot = new Uri("http://host/service"), SelectAndExpand = result }; var outputContext = CreateJsonLightOutputContext(this.stream, this.GetModel(), odataUri); ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetProducts(), this.GetProductType()); writer.WriteStart(feed); writer.WriteStart(containedEntry); writer.WriteEnd(); writer.WriteStart(entry); writer.WriteEnd(); writer.WriteEnd(); writer.Flush(); string payload = this.TestFinish(); payload.Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Products(Name,Details,Details(Detail))/$delta\",\"value\":[{\"@odata.context\":\"http://host/service/$metadata#Products(1)/Details/$entity\",\"Id\":1,\"Detail\":\"made in china\"},{\"Id\":1,\"Name\":\"Car\"}]}"); }
public void WriteContainedEntityInDeltaFeed() { this.TestInit(this.GetModel()); ODataDeltaFeed feed = new ODataDeltaFeed(); ODataEntry containedEntry = new ODataEntry() { TypeName = "MyNS.ProductDetail", Properties = new[] { new ODataProperty {Name = "Id", Value = new ODataPrimitiveValue(1)}, new ODataProperty {Name = "Detail", Value = new ODataPrimitiveValue("made in china")}, }, }; containedEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceEntityTypeName = "MyNS.ProductDetail", NavigationSourceName = "Products(1)/Details", NavigationSourceKind = EdmNavigationSourceKind.ContainedEntitySet }); ODataEntry containedInContainedEntity = new ODataEntry() { TypeName = "MyNS.ProductDetailItem", Properties = new[] { new ODataProperty {Name = "ItemId", Value = new ODataPrimitiveValue(1)}, new ODataProperty {Name = "Description", Value = new ODataPrimitiveValue("made by HCC")}, }, }; containedInContainedEntity.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo() { NavigationSourceEntityTypeName = "MyNS.ProductDetailItem", NavigationSourceName = "Products(1)/Details(1)/Items", NavigationSourceKind = EdmNavigationSourceKind.ContainedEntitySet }); ODataJsonLightDeltaWriter writer = new ODataJsonLightDeltaWriter(outputContext, this.GetProducts(), this.GetProductType()); writer.WriteStart(feed); writer.WriteStart(containedEntry); writer.WriteEnd(); writer.WriteStart(containedInContainedEntity); writer.WriteEnd(); writer.WriteEnd(); writer.Flush(); string payload = this.TestFinish(); payload.Should().Be("{\"@odata.context\":\"http://host/service/$metadata#Products/$delta\",\"value\":[{\"@odata.context\":\"http://host/service/$metadata#Products(1)/Details/$entity\",\"Id\":1,\"Detail\":\"made in china\"},{\"@odata.context\":\"http://host/service/$metadata#Products(1)/Details(1)/Items/$entity\",\"ItemId\":1,\"Description\":\"made by HCC\"}]}"); }