Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,18 @@ public NavigationSourceLinkBuilderAnnotation(IEdmNavigationSource navigationSour
}

// Add navigation link builders for all navigation properties in derived types.
bool derivedTypesDefineNavigationProperty = false;
foreach (IEdmEntityType derivedEntityType in derivedTypes)
{
foreach (IEdmNavigationProperty navigationProperty in derivedEntityType.DeclaredNavigationProperties())
{
derivedTypesDefineNavigationProperty = true;
Func<ResourceContext, IEdmNavigationProperty, Uri> navigationLinkFactory =
(resourceContext, navProperty) => resourceContext.GenerateNavigationPropertyLink(navProperty, includeCast: true);
AddNavigationPropertyLinkBuilder(navigationProperty, new NavigationLinkBuilder(navigationLinkFactory, followsConventions: true));
}
}

Func<ResourceContext, Uri> selfLinkFactory =
(resourceContext) => resourceContext.GenerateSelfLink(includeCast: derivedTypesDefineNavigationProperty);
(resourceContext) => resourceContext.GenerateSelfLink(includeCast: false);
IdLinkBuilder = new SelfLinkBuilder<Uri>(selfLinkFactory, followsConventions: true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ static CustomersController()
{
new Order { Id = 4, Amount = 170M }
}
},
new GoldCustomer {
Id = 4,
Name = "Customer 4",
LoyaltyCardNo = "9876543211",
Orders = new List<Order>
{
new Order { Id = 5, Amount = 2230M },
new Order { Id = 6, Amount = 1150M }
},
BulkOrders = new List<Order>
{
new Order { Id = 7, Amount = 66832M }
}
}
};
}
Expand All @@ -59,6 +73,7 @@ public IActionResult Get()

[EnableQuery]
[HttpGet("odata/Customers/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.VipCustomer({key})")] // convention routing doesn't create this template.
[HttpGet("odata/Customers/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer({key})")] // convention routing doesn't create this template.
public IActionResult Get(int key)
{
var customer = Customers.FirstOrDefault(c => c.Id == key);
Expand Down Expand Up @@ -91,5 +106,26 @@ public IActionResult GetVipCustomer([FromODataUri] int key)

return Ok(vipCustomer);
}

// Handles /entityset/cast path template
[EnableQuery]
public IActionResult GetFromGoldCustomer()
{
return Ok(Customers.OfType<GoldCustomer>());
}

// Handles /entityset/key/cast and /entityset/cast/key path templates
[EnableQuery]
public IActionResult GetGoldCustomer([FromODataUri] int key)
{
var vipCustomer = Customers.OfType<GoldCustomer>().SingleOrDefault(d => d.Id.Equals(key));

if (vipCustomer == null)
{
return NotFound();
}

return Ok(vipCustomer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class VipCustomer : Customer
public string LoyaltyCardNo { get; set; }
}

public class GoldCustomer : VipCustomer
{
public List<Order> BulkOrders { get; set; }
}

public class Order
{
public int Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//------------------------------------------------------------------------------

using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OData.TestCommon;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -135,5 +136,37 @@ public async Task RestrictEntityToDerivedTypeInstance_ThenExpandNavProperty(stri
"\"Orders\":[{\"Id\":2,\"Amount\":230},{\"Id\":3,\"Amount\":150}]";
Assert.Contains(expectedContent, await response.Content.ReadAsStringAsync());
}

[Theory]
[InlineData("Customers(4)")]
[InlineData("Customers(4)/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer")]
[InlineData("Customers/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer(4)")]
public async Task DerivedTypeNavPropertyLink_FullMetadata(string pathAndQuery)
{
// Arrange: Key preceeds name of the derived type
string requestUri = $"/odata/{pathAndQuery}";

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=full"));
HttpClient client = CreateClient();

// Act
HttpResponseMessage response = await client.SendAsync(request);

// Assert
Assert.True(response.IsSuccessStatusCode);

string expectedContent = "{\"@odata.context\":\"http://localhost/odata/$metadata#Customers/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer/$entity\"," +
"\"@odata.type\":\"#Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer\"," +
"\"@odata.id\":\"http://localhost/odata/Customers(4)\"," +
"\"@odata.editLink\":\"Customers(4)/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer\"," +
"\"Id\":4,\"Name\":\"Customer 4\",\"LoyaltyCardNo\":\"9876543211\"," +
"\"Orders@odata.associationLink\":\"http://localhost/odata/Customers(4)/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer/Orders/$ref\"," +
"\"Orders@odata.navigationLink\":\"http://localhost/odata/Customers(4)/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer/Orders\"," +
"\"BulkOrders@odata.associationLink\":\"http://localhost/odata/Customers(4)/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer/BulkOrders/$ref\"," +
"\"BulkOrders@odata.navigationLink\":\"http://localhost/odata/Customers(4)/Microsoft.AspNetCore.OData.E2E.Tests.DerivedTypes.GoldCustomer/BulkOrders\"}";

Assert.Equal(expectedContent, await response.Content.ReadAsStringAsync());
}
}
}