From a6c67637dd1970bdac9e398d7ea9857d1086f860 Mon Sep 17 00:00:00 2001 From: Elizabeth Okerio Date: Fri, 11 Jun 2021 15:09:25 +0300 Subject: [PATCH] Feature/new add support for odata bind (#2506) * support for odata.bind * support for odata.bind --- .../ODataEntityReferenceLinkBase.cs | 10 +- .../ODataNestedResourceInfoWrapper.cs | 1 + .../ODataResourceDeserializer.cs | 185 +++++++++++- .../ODataEntityReferenceLinkE2ETests.cs | 276 ++++++++++++++++++ .../ODataEntityReferenceLinkTests.cs | 181 ++++++++++++ ...crosoft.AspNet.OData.Test.Shared.projitems | 1 + 6 files changed, 634 insertions(+), 20 deletions(-) create mode 100644 test/E2ETest/Microsoft.Test.E2E.AspNet.OData/Build.AspNet/Formatter/ODataEntityReferenceLinkE2ETests.cs create mode 100644 test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Formatter/ODataEntityReferenceLinkTests.cs diff --git a/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataEntityReferenceLinkBase.cs b/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataEntityReferenceLinkBase.cs index eff07d138a..dc0e9a8649 100644 --- a/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataEntityReferenceLinkBase.cs +++ b/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataEntityReferenceLinkBase.cs @@ -17,17 +17,13 @@ public class ODataEntityReferenceLinkBase : ODataItemBase public ODataEntityReferenceLinkBase(ODataEntityReferenceLink item) : base(item) { + EntityReferenceLink = item; } /// /// Gets the wrapped . /// - public ODataEntityReferenceLink EntityReferenceLink - { - get - { - return Item as ODataEntityReferenceLink; - } - } + public ODataEntityReferenceLink EntityReferenceLink { get; } + } } diff --git a/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataNestedResourceInfoWrapper.cs b/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataNestedResourceInfoWrapper.cs index 5dc286af5a..86808653b9 100644 --- a/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataNestedResourceInfoWrapper.cs +++ b/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataNestedResourceInfoWrapper.cs @@ -40,5 +40,6 @@ public ODataNestedResourceInfo NestedResourceInfo /// Gets the nested items that are part of this nested resource info. /// public IList NestedItems { get; private set; } + } } diff --git a/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataResourceDeserializer.cs b/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataResourceDeserializer.cs index 370991c9af..893f61db7f 100644 --- a/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataResourceDeserializer.cs +++ b/src/Microsoft.AspNet.OData.Shared/Formatter/Deserialization/ODataResourceDeserializer.cs @@ -11,9 +11,12 @@ using System.Runtime.Serialization; using System.Threading.Tasks; using Microsoft.AspNet.OData.Common; -using Microsoft.AspNet.OData.Formatter.Serialization; +using Microsoft.AspNet.OData.Interfaces; +using Microsoft.AspNet.OData.Routing; using Microsoft.OData; using Microsoft.OData.Edm; +using Microsoft.OData.UriParser; +using ODataPath = Microsoft.AspNet.OData.Routing.ODataPath; namespace Microsoft.AspNet.OData.Formatter.Deserialization { @@ -97,6 +100,7 @@ public sealed override object ReadInline(object item, IEdmTypeReference edmType, // Recursion guard to avoid stack overflows RuntimeHelpers.EnsureSufficientExecutionStack(); + resourceWrapper = UpdateResourceWrapper(resourceWrapper, readContext); return ReadResource(resourceWrapper, edmType.AsStructured(), readContext); } @@ -331,7 +335,39 @@ public virtual void ApplyNestedProperty(object resource, ODataNestedResourceInfo } } - foreach (ODataItemBase childItem in resourceInfoWrapper.NestedItems) + IList nestedItems; + ODataEntityReferenceLinkBase[] referenceLinks = resourceInfoWrapper.NestedItems.OfType().ToArray(); + if (referenceLinks.Length > 0) + { + // Be noted: + // 1) OData v4.0, it's "Orders@odata.bind", and we get "ODataEntityReferenceLinkWrapper"(s) for that. + // 2) OData v4.01, it's {"odata.id" ...}, and we get "ODataResource"(s) for that. + // So, in OData v4, if it's a single, NestedItems contains one ODataEntityReferenceLinkWrapper, + // if it's a collection, NestedItems contains multiple ODataEntityReferenceLinkWrapper(s) + // We can use the following code to adjust the `ODataEntityReferenceLinkWrapper` to `ODataResourceWrapper`. + // In OData v4.01, we will not be here. + // Only supports declared property + Contract.Assert(edmProperty != null); + + nestedItems = new List(); + if (edmProperty.Type.IsCollection()) + { + IEdmCollectionTypeReference edmCollectionTypeReference = edmProperty.Type.AsCollection(); + ODataResourceSetWrapper resourceSetWrapper = CreateResourceSetWrapper(edmCollectionTypeReference, referenceLinks, readContext); + nestedItems.Add(resourceSetWrapper); + } + else + { + ODataResourceWrapper resourceWrapper = CreateResourceWrapper(edmProperty.Type, referenceLinks[0], readContext); + nestedItems.Add(resourceWrapper); + } + } + else + { + nestedItems = resourceInfoWrapper.NestedItems; + } + + foreach (ODataItemBase childItem in nestedItems) { // it maybe null. if (childItem == null) @@ -347,14 +383,7 @@ public virtual void ApplyNestedProperty(object resource, ODataNestedResourceInfo ApplyResourceInNestedProperty(edmProperty, resource, null, readContext); } } - - ODataEntityReferenceLinkBase entityReferenceLink = childItem as ODataEntityReferenceLinkBase; - if (entityReferenceLink != null) - { - // ignore entity reference links. - continue; - } - + ODataResourceSetWrapperBase resourceSetWrapper = childItem as ODataResourceSetWrapperBase; if (resourceSetWrapper != null) { @@ -388,6 +417,137 @@ public virtual void ApplyNestedProperty(object resource, ODataNestedResourceInfo } } + + private ODataResourceSetWrapper CreateResourceSetWrapper(IEdmCollectionTypeReference edmPropertyType, + IList refLinks, ODataDeserializerContext readContext) + { + ODataResourceSet resourceSet = new ODataResourceSet + { + TypeName = edmPropertyType.FullName(), + }; + + IEdmTypeReference elementType = edmPropertyType.ElementType(); + ODataResourceSetWrapper resourceSetWrapper = new ODataResourceSetWrapper(resourceSet); + foreach (ODataEntityReferenceLinkBase refLinkWrapper in refLinks) + { + ODataResourceWrapper resourceWrapper = CreateResourceWrapper(elementType, refLinkWrapper, readContext); + resourceSetWrapper.Resources.Add(resourceWrapper); + } + + return resourceSetWrapper; + } + + private ODataResourceWrapper CreateResourceWrapper(IEdmTypeReference edmPropertyType, ODataEntityReferenceLinkBase refLink, ODataDeserializerContext readContext) + { + Contract.Assert(readContext != null); + + ODataResource resource = new ODataResource + { + TypeName = edmPropertyType.FullName(), + }; + + resource.Properties = CreateKeyProperties(refLink.EntityReferenceLink.Url, readContext); + + if (refLink.EntityReferenceLink.InstanceAnnotations != null) + { + foreach (ODataInstanceAnnotation instanceAnnotation in refLink.EntityReferenceLink.InstanceAnnotations) + { + resource.InstanceAnnotations.Add(instanceAnnotation); + }; + } + + return new ODataResourceWrapper(resource); + } + + /// + /// Update the resource wrapper if it has the "Id" value. + /// + /// The resource wrapper. + /// The read context. + /// The resource wrapper. + private ODataResourceWrapper UpdateResourceWrapper(ODataResourceWrapper resourceWrapper, ODataDeserializerContext readContext) + { + Contract.Assert(readContext != null); + + if (resourceWrapper?.Resource?.Id == null) + { + return resourceWrapper; + } + + IEnumerable keys = CreateKeyProperties(resourceWrapper.Resource.Id, readContext); + if (keys == null) + { + return resourceWrapper; + } + + if (resourceWrapper.Resource.Properties == null) + { + resourceWrapper.Resource.Properties = keys; + } + else + { + IDictionary newPropertiesDic = resourceWrapper.Resource.Properties.ToDictionary(p => p.Name, p => p); + foreach (ODataProperty key in keys) + { + if (!newPropertiesDic.ContainsKey(key.Name)) + { + newPropertiesDic[key.Name] = key; + } + } + + resourceWrapper.Resource.Properties = newPropertiesDic.Values; + } + + return resourceWrapper; + } + + /// + /// Do uri parsing to get the key values. + /// + /// The key Id. + /// The reader context. + /// The key properties. + private static IList CreateKeyProperties(Uri id, ODataDeserializerContext readContext) + { + Contract.Assert(id != null); + Contract.Assert(readContext != null); + IList properties = new List(); + if (readContext.Request == null) + { + return properties; + } + + IODataPathHandler pathHandler = readContext.InternalRequest.PathHandler; + IWebApiRequestMessage internalRequest = readContext.InternalRequest; + IWebApiUrlHelper urlHelper = readContext.InternalUrlHelper; + try + { + string serviceRoot = urlHelper.CreateODataLink( + internalRequest.Context.RouteName, + internalRequest.PathHandler, + new List()); + ODataPath odataPath = pathHandler.Parse(serviceRoot, id.OriginalString, internalRequest.RequestContainer); + KeySegment keySegment = odataPath.Segments.OfType().LastOrDefault(); + if (keySegment != null) + { + foreach (KeyValuePair key in keySegment.Keys) + { + properties.Add(new ODataProperty + { + Name = key.Key, + Value = key.Value + }); + } + } + + return properties; + } + catch (Exception) + { + return properties; + } + } + /// /// Deserializes the structural properties from into . /// @@ -516,7 +676,7 @@ private object ReadNestedResourceInline(ODataResourceWrapper resourceWrapper, IE { Path = readContext.Path, Model = readContext.Model, - Request = readContext.Request, + Request = readContext.Request }; Type clrType = null; @@ -542,7 +702,6 @@ private object ReadNestedResourceInline(ODataResourceWrapper resourceWrapper, IE : clrType; return deserializer.ReadInline(resourceWrapper, edmType, nestedReadContext); } - private void ApplyResourceSetInNestedProperty(IEdmProperty nestedProperty, object resource, ODataResourceSetWrapperBase resourceSetWrapper, ODataDeserializerContext readContext) { @@ -614,7 +773,7 @@ private object ReadNestedResourceSetInline(ODataResourceSetWrapperBase resourceS { Path = readContext.Path, Model = readContext.Model, - Request = readContext.Request, + Request = readContext.Request }; if (readContext.IsUntyped) diff --git a/test/E2ETest/Microsoft.Test.E2E.AspNet.OData/Build.AspNet/Formatter/ODataEntityReferenceLinkE2ETests.cs b/test/E2ETest/Microsoft.Test.E2E.AspNet.OData/Build.AspNet/Formatter/ODataEntityReferenceLinkE2ETests.cs new file mode 100644 index 0000000000..40ff2933b3 --- /dev/null +++ b/test/E2ETest/Microsoft.Test.E2E.AspNet.OData/Build.AspNet/Formatter/ODataEntityReferenceLinkE2ETests.cs @@ -0,0 +1,276 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Data.Entity; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Microsoft.AspNet.OData; +using Microsoft.AspNet.OData.Extensions; +using Microsoft.AspNet.OData.Routing; +using Microsoft.OData.Edm; +using Microsoft.Test.E2E.AspNet.OData.Common.Controllers; +using Microsoft.Test.E2E.AspNet.OData.Common.Execution; +using Newtonsoft.Json.Linq; +using Xunit; + +namespace Microsoft.Test.E2E.AspNet.OData.Formatter +{ + public class ODataEntityReferenceLinkE2ETests : WebHostTestBase + { + public ODataEntityReferenceLinkE2ETests(WebHostTestFixture fixture) + : base(fixture) + { + } + protected override void UpdateConfiguration(WebRouteConfiguration configuration) + { + var controllers = new[] { typeof(BooksController)}; + configuration.AddControllers(controllers); + configuration.Count().Filter().OrderBy().Expand().MaxTop(null).Select(); + configuration.MapODataServiceRoute("odata", "odata", BuildEdmModel(configuration)); + } + private static IEdmModel BuildEdmModel(WebRouteConfiguration configuration) + { + var builder = configuration.CreateConventionModelBuilder(); + builder.EntitySet("Books"); + builder.EntitySet("Authors"); + builder.Action("ResetDataSource"); + builder.Action("RelateToExistingEntityAndUpdate").ReturnsFromEntitySet("Books").EntityParameter("book"); + + return builder.GetEdmModel(); + } + + [Fact] + public async Task CanCreate_ANewEntityAndRelateToAnExistingEntity_UsingODataBind() + { + await ResetDataSource(); + // Arrange + const string Payload = "{" + + "\"Id\":\"1\"," + + "\"Name\":\"BookA\"," + + "\"Author@odata.bind\":\"Authors(1)\"}"; + + string Uri = BaseAddress + "/odata/Books"; + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Uri); + + request.Content = new StringContent(Payload); + request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json"); + + // Act + HttpResponseMessage response = await Client.SendAsync(request); + + // Assert + Assert.True(response.IsSuccessStatusCode); + Assert.Equal(HttpStatusCode.Created, response.StatusCode); + Assert.NotNull(response.Content); + + //Get the above saved entity from the database + //and expand the navigation property to see if + //it was correctly created with the existing entity + //attached to it. + string query = string.Format("{0}/odata/Books?$expand=Author", BaseAddress); + HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, query); + request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + + // Act + HttpResponseMessage res = await Client.SendAsync(requestMessage); + + // Assert + Assert.True(res.IsSuccessStatusCode); + var responseObject = JObject.Parse(await res.Content.ReadAsStringAsync()); + var result = responseObject["value"] as JArray; + var expandProp = result[0]["Author"] as JObject; + Assert.Equal(1, expandProp["Id"]); + + } + + [Fact] + public async Task CanUpdate_TheRelatedEntitiesProperties() + { + await ResetDataSource(); + // Arrange + const string Payload = "{" + + "\"book\":{" + + "\"Id\":\"1\"," + + "\"Name\":\"BookA\"," + + "\"Author\":{" + + "\"@odata.id\":\"Authors(1)\"," + + "\"Name\":\"UpdatedAuthor\"}}}"; + + string Uri = BaseAddress + "/odata/RelateToExistingEntityAndUpdate"; + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Uri); + + request.Content = new StringContent(Payload); + request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json"); + + // Act + HttpResponseMessage response = await Client.SendAsync(request); + + // Assert + Assert.True(response.IsSuccessStatusCode); + Assert.Equal(HttpStatusCode.Created, response.StatusCode); + Assert.NotNull(response.Content); + + //Get the above saved entity from the database + //and expand its navigation property to see if it was created with + //the existing entity correctly. + //Also note that we were able to update the name property + //of the existing entity + string query = string.Format("{0}/odata/Books?$expand=Author", BaseAddress); + HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, query); + request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + + // Act + HttpResponseMessage res = await Client.SendAsync(requestMessage); + + // Assert + Assert.True(res.IsSuccessStatusCode); + var responseObject = JObject.Parse(await res.Content.ReadAsStringAsync()); + var result = responseObject["value"] as JArray; + var expandProp = result[0]["Author"] as JObject; + Assert.Equal(1, expandProp["Id"]); + Assert.Equal("UpdatedAuthor", expandProp["Name"]); + } + + private async Task ResetDataSource() + { + string requestUri = BaseAddress + "/odata/ResetDataSource"; + HttpClient client = new HttpClient(); + HttpResponseMessage response = await client.GetAsync(requestUri); + response.EnsureSuccessStatusCode(); + } + } + + public class BooksController : TestODataController +#if NETCORE + , IDisposable +#endif + { + private ODataEntityReferenceLinkContext db = new ODataEntityReferenceLinkContext(); + + [EnableQuery] + public ITestActionResult Get() + { + return Ok(db.Books); + } + public ITestActionResult Post([FromBody] Book book) + { + if (!ModelState.IsValid) + { + return BadRequest(); + } + + db.Authors.Attach(book.Author); + db.Books.Add(book); + db.SaveChanges(); + + return Created(book); + } + + [HttpPost] + [ODataRoute("RelateToExistingEntityAndUpdate")] + public ITestActionResult RelateToExistingEntityAndUpdate(ODataActionParameters odataActionParameters) + { + if (!ModelState.IsValid) + { + return BadRequest(); + } + + Book book = (Book)odataActionParameters["book"]; + string authorName = book.Author.Name; + Author author = new Author() + { + Id = book.Author.Id + }; + db.Authors.Attach(author); + book.Author = author; + book.Author.Name = authorName; + db.Books.Add(book); + + db.SaveChanges(); + return Created(book); + } + + [HttpGet] + [ODataRoute("ResetDataSource")] + public ITestActionResult ResetDataSource() + { + db.Database.Delete(); // Start from scratch so that tests aren't broken by schema changes. + CreateDatabase(); + return Ok(); + } + + private static void CreateDatabase() + { + using (ODataEntityReferenceLinkContext db = new ODataEntityReferenceLinkContext()) + { + if (!db.Authors.Any()) + { + IList authors = new List() + { + new Author() + { + Id = 1, + Name = "AuthorA" + }, + new Author() + { + Id = 2, + Name = "AuthorB" + }, + new Author() + { + Id = 3, + Name = "AuthorC" + } + }; + + foreach (var author in authors) + { + db.Authors.Add(author); + } + db.SaveChanges(); + } + } + } + +#if NETCORE + public void Dispose() + { + //_db.Dispose(); + } +#endif + + } + + public class Book + { + [Key] + public int Id { get; set; } + public string Name { get; set; } + public Author Author { get; set; } + public IList AuthorList { get; set; } + } + + public class Author + { + [Key] + public int Id { get; set; } + public string Name { get; set; } + } + + public class ODataEntityReferenceLinkContext : DbContext + { + public static string ConnectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;Integrated Security=True;Initial Catalog=ODataEntityReferenceLinkContext"; + public ODataEntityReferenceLinkContext() + : base(ConnectionString) + { + } + public DbSet Books { get; set; } + public DbSet Authors { get; set; } + } +} diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Formatter/ODataEntityReferenceLinkTests.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Formatter/ODataEntityReferenceLinkTests.cs new file mode 100644 index 0000000000..1d6ec7cf06 --- /dev/null +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Formatter/ODataEntityReferenceLinkTests.cs @@ -0,0 +1,181 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using Microsoft.AspNet.OData.Builder; +using Microsoft.AspNet.OData.Formatter; +using Microsoft.AspNet.OData.Formatter.Deserialization; +using Microsoft.AspNet.OData.Test.Abstraction; +using Microsoft.OData; +using Microsoft.OData.Edm; +using Microsoft.OData.UriParser; +using Xunit; +using ODataPath = Microsoft.AspNet.OData.Routing.ODataPath; + +namespace Microsoft.AspNet.OData.Test.Formatter +{ + public class ODataEntityReferenceLinkTests + { + private readonly ODataDeserializerProvider _deserializerProvider; + public ODataEntityReferenceLinkTests() + { + _deserializerProvider = ODataDeserializerProviderFactory.Create(); + } + + /// + /// In OData v4.0 an ODataEntityReferenceLink will be converted + /// to a resource then deserialized as a resource. + /// + [Fact] + public void ReadResource_CanRead_AnEntityRefenceLink() + { + // Arrange + ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create(); + var books = builder.EntitySet("Books"); + builder.EntityType(); + builder.EntitySet("Authors"); + var author = + books.EntityType.HasOptional((e) => e.Author); + books.HasNavigationPropertyLink(author, (a, b) => new Uri("aa:b"), false); + books.HasOptionalBinding((e) => e.Author, "authorr"); + + + IEdmModel model = builder.GetEdmModel(); + IEdmEntityTypeReference bookTypeReference = model.GetEdmTypeReference(typeof(Book)).AsEntity(); + var deserializer = new ODataResourceDeserializer(_deserializerProvider); + ODataResource odataResource = new ODataResource + { + Properties = new[] + { + new ODataProperty { Name = "Id", Value = 1}, + new ODataProperty { Name = "Name", Value = "BookA"}, + }, + TypeName = "Microsoft.AspNet.OData.Test.Formatter.Book" + }; + + IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Books"); + ODataPath path = new ODataPath(new EntitySetSegment(entitySet)); + var request = RequestFactory.CreateFromModel(model, path: path); + + ODataDeserializerContext readContext = new ODataDeserializerContext() + { + Model = model, + Request = request, + Path = path + }; + + ODataResourceWrapper topLevelResourceWrapper = new ODataResourceWrapper(odataResource); + ODataNestedResourceInfo resourceInfo = new ODataNestedResourceInfo + { + IsCollection = false, + Name = "Author" + }; + + ODataEntityReferenceLink refLink = new ODataEntityReferenceLink { Url = new Uri("http://localhost/Authors(2)") }; + ODataEntityReferenceLinkBase refLinkWrapper = new ODataEntityReferenceLinkBase(refLink); + + ODataNestedResourceInfoWrapper resourceInfoWrapper = new ODataNestedResourceInfoWrapper(resourceInfo); + resourceInfoWrapper.NestedItems.Add(refLinkWrapper); + topLevelResourceWrapper.NestedResourceInfos.Add(resourceInfoWrapper); + + // Act + Book book = deserializer.ReadResource(topLevelResourceWrapper, bookTypeReference, readContext) + as Book; + + // Assert + Assert.NotNull(book); + Assert.Equal(2, book.Author.Id); + Assert.NotNull(book.Author); + + } + + [Fact] + public void ReadResource_CanRead_ACollectionOfEntityRefenceLinks() + { + // Arrange + ODataConventionModelBuilder builder = ODataConventionModelBuilderFactory.Create(); + var books = builder.EntitySet("Books"); + builder.EntityType(); + builder.EntitySet("Authors"); + var author = + books.EntityType.HasOptional((e) => e.Author); + books.HasNavigationPropertyLink(author, (a, b) => new Uri("aa:b"), false); + books.HasOptionalBinding((e) => e.Author, "authorr"); + + + IEdmModel model = builder.GetEdmModel(); + IEdmEntityTypeReference bookTypeReference = model.GetEdmTypeReference(typeof(Book)).AsEntity(); + var deserializer = new ODataResourceDeserializer(_deserializerProvider); + ODataResource odataResource = new ODataResource + { + Properties = new[] + { + new ODataProperty { Name = "Id", Value = 1}, + new ODataProperty { Name = "Name", Value = "BookA"}, + }, + TypeName = "Microsoft.AspNet.OData.Test.Formatter.Book" + }; + + IEdmEntitySet entitySet = model.EntityContainer.FindEntitySet("Books"); + ODataPath path = new ODataPath(new EntitySetSegment(entitySet)); + var request = RequestFactory.CreateFromModel(model, path: path); + + ODataDeserializerContext readContext = new ODataDeserializerContext() + { + Model = model, + Request = request, + Path = path + }; + + ODataResourceWrapper topLevelResourceWrapper = new ODataResourceWrapper(odataResource); + ODataNestedResourceInfo resourceInfo = new ODataNestedResourceInfo + { + IsCollection = true, + Name = "AuthorList" + }; + + IList refLinks = new List() + { + new ODataEntityReferenceLinkBase(new ODataEntityReferenceLink{ Url = new Uri("http://localhost/Authors(2)") }), + new ODataEntityReferenceLinkBase(new ODataEntityReferenceLink{ Url = new Uri("http://localhost/Authors(3)")}) + }; + + + ODataNestedResourceInfoWrapper resourceInfoWrapper = new ODataNestedResourceInfoWrapper(resourceInfo); + + foreach (ODataEntityReferenceLinkBase refLinkWrapper in refLinks) + { + resourceInfoWrapper.NestedItems.Add(refLinkWrapper); + } + topLevelResourceWrapper.NestedResourceInfos.Add(resourceInfoWrapper); + + // Act + Book book = deserializer.ReadResource(topLevelResourceWrapper, bookTypeReference, readContext) + as Book; + + // Assert + Assert.NotNull(book); + Assert.NotNull(book.AuthorList); + Assert.Equal(2, book.AuthorList.Count()); + } + + public class Book + { + [Key] + public int Id { get; set; } + public string Name { get; set; } + public Author Author { get; set; } + public IList AuthorList { get; set; } + } + + public class Author + { + [Key] + public int Id { get; set; } + public string Name { get; set; } + } + } +} diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Microsoft.AspNet.OData.Test.Shared.projitems b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Microsoft.AspNet.OData.Test.Shared.projitems index 38ba2ee985..07a6db0ad3 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Microsoft.AspNet.OData.Test.Shared.projitems +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Microsoft.AspNet.OData.Test.Shared.projitems @@ -168,6 +168,7 @@ +