Skip to content

Commit

Permalink
Verify Cast after a collection of complex type property can work
Browse files Browse the repository at this point in the history
Verify the ODL has fixed issue "Type cast after a collection of complex
type property doesn't work" See:
OData/odata.net#457
  • Loading branch information
xuzhg committed Aug 9, 2016
1 parent 9b96615 commit 83bc211
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
Expand Up @@ -175,7 +175,8 @@ public override void WriteObject(object graph, Type type, ODataMessageWriter mes
TypeName = resourceSetType.FullName()
};

if (writeContext.NavigationSource != null)
IEdmStructuredTypeReference structuredType = GetResourceType(resourceSetType).AsStructured();
if (writeContext.NavigationSource != null && structuredType.IsEntity())
{
ResourceSetContext resourceSetContext = new ResourceSetContext
{
Expand All @@ -186,7 +187,7 @@ public override void WriteObject(object graph, Type type, ODataMessageWriter mes
ResourceSetInstance = resourceSetInstance
};

IEdmEntityType entityType = GetResourceType(resourceSetType).Definition as IEdmEntityType;
IEdmEntityType entityType = structuredType.AsEntity().EntityDefinition();
var operations = writeContext.Model.GetAvailableOperationsBoundToCollection(entityType);
var odataOperations = CreateODataOperations(operations, resourceSetContext, writeContext);
foreach (var odataOperation in odataOperations)
Expand Down
Expand Up @@ -6,6 +6,7 @@
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.OData.Formatter;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;

Expand Down Expand Up @@ -50,10 +51,20 @@ public override string SelectAction(ODataPath odataPath, HttpControllerContext c
}
else
{
IEdmComplexType typeCast;
if (cast.EdmType.TypeKind == EdmTypeKind.Collection)
{
typeCast = ((IEdmCollectionType)cast.EdmType).ElementType.AsComplex().ComplexDefinition();
}
else
{
typeCast = (IEdmComplexType)cast.EdmType;
}

// for example: GetCityOfSubAddressFromVipCustomer or GetCityOfSubAddress
actionName = actionMap.FindMatchingAction(
prefix + property.Name + "Of" + ((IEdmComplexType)cast.EdmType).Name + "From" + declaringType.Name,
prefix + property.Name + "Of" + ((IEdmComplexType)cast.EdmType).Name);
prefix + property.Name + "Of" + typeCast.Name + "From" + declaringType.Name,
prefix + property.Name + "Of" + typeCast.Name);
}

if (actionName != null)
Expand Down
Expand Up @@ -187,8 +187,7 @@ public IHttpActionResult GetOptionalShapes(int key)
return Ok(window.OptionalShapes);
}

// https://github.com/OData/odata.net/issues/457: [UriParser] Cast segment following a collection complex type property reports exception.
// [ODataRoute("Windows({key})/OptionalShapes/WebStack.QA.Test.OData.ComplexTypeInheritance.Circle")]
[ODataRoute("Windows({key})/OptionalShapes/WebStack.QA.Test.OData.ComplexTypeInheritance.Circle")]
public IHttpActionResult GetOptionalShapesOfCircle(int key)
{
Window window = _windows.FirstOrDefault(e => e.Id == key);
Expand Down
@@ -1,4 +1,7 @@
using System;
// 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.Linq;
using System.Net;
Expand Down Expand Up @@ -340,10 +343,10 @@ public async Task QueryComplexTypeProperty(string mode, string mime)
String.Format("\nExpected that Radius: 2, but actually: {0},\n request uri: {1},\n response payload: {2}", radius, requestUri, contentOfString));
}

[Theory(Skip = "https://github.com/OData/odata.net/issues/457 [UriParser] Cast segment following a collection complex type property reports exception.")]
[Theory]
[InlineData("convention")]
[InlineData("explicit")]
// GET ~/Windows(1)/CurrentShape/WebStack.QA.Test.OData.ComplexTypeInheritance.Circle
// GET ~/Windows(1)/OptionalShapes/WebStack.QA.Test.OData.ComplexTypeInheritance.Circle
public async Task GetOptionalShapesPlusCast(string modelMode)
{
string serviceRootUri = string.Format("{0}/{1}", BaseAddress, modelMode).ToLower();
Expand Down

0 comments on commit 83bc211

Please sign in to comment.