Skip to content

Commit

Permalink
Fixes #1171: ODataResourceSetDeserializer type cast wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Feb 29, 2024
1 parent df8ca08 commit d4aa9a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override async Task<object> ReadAsync(ODataMessageReader messageReader, T
throw Error.Argument("edmType", SRResources.ArgumentMustBeOfType, "Collection of complex, entity or untyped");
}

IEdmStructuredType structuredType = edmType.AsCollection().ElementType() as IEdmStructuredType;
IEdmStructuredType structuredType = edmType.AsCollection().ElementType().Definition as IEdmStructuredType;

ODataReader resourceSetReader = await messageReader.CreateODataResourceSetReaderAsync(structuredType).ConfigureAwait(false);
object resourceSet = await resourceSetReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,39 @@ public void ReadResourceItem_Throws_TypeCannotBeDeserialized()
"'Microsoft.AspNetCore.OData.Tests.Models.Customer' cannot be deserialized using the OData input formatter.");
}

[Fact]
public async Task ReadAsync_ReturnTypedCollection_WithTypeMode()
{
// Arrange
HttpContent content = new StringContent("{ 'value': [ {'City' : 'Redmond' }, {'City' : 'Issaquah' } ] }");
HeaderDictionary headerDict = new HeaderDictionary
{
{ "Content-Type", "application/json" }
};

IODataRequestMessage request = ODataMessageWrapperHelper.Create(await content.ReadAsStreamAsync(), headerDict);
ODataMessageReader reader = new ODataMessageReader(request, new ODataMessageReaderSettings(), _model);
IEdmComplexType addressType = _model.SchemaElements.OfType<IEdmComplexType>().First(c => c.Name == "Address");
var deserializer = new ODataResourceSetDeserializer(_deserializerProvider);
ODataDeserializerContext readContext = new ODataDeserializerContext
{
Model = _model,
ResourceType = typeof(IList<Address>),
ResourceEdmType = new EdmCollectionTypeReference(new EdmCollectionType(addressType.ToEdmTypeReference(true))),
};

// Act
var result = await deserializer.ReadAsync(reader, typeof(IList<Address>), readContext);

// Assert
IEnumerable<Address> addresses = (result as IEnumerable).Cast<Address>();

Assert.Equal(2, addresses.Count());
Assert.Collection(addresses,
e => Assert.Equal("Redmond", e.City),
e => Assert.Equal("Issaquah", e.City));
}

[Fact]
public async Task ReadAsync_ReturnsEdmUntypedCollection_WithTypeMode()
{
Expand Down

0 comments on commit d4aa9a5

Please sign in to comment.