-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support type cast in group by #1182
base: main
Are you sure you want to change the base?
Conversation
@microsoft-github-policy-service agree |
IEdmStructuredTypeReference structured = node.StructuredTypeReference; | ||
Contract.Assert(structured != null, "NS casts can contain only structured types"); | ||
|
||
Type clrType = Model.GetClrType(structured); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we stick with using var for consistency ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is based on the following method :
AspNetCoreOData/src/Microsoft.AspNetCore.OData/Query/Expressions/QueryBinder.cs
Lines 692 to 703 in 7e2e707
public virtual Expression BindSingleResourceCastNode(SingleResourceCastNode node, QueryBinderContext context) | |
{ | |
CheckArgumentNull(node, context); | |
IEdmStructuredTypeReference structured = node.StructuredTypeReference; | |
Contract.Assert(structured != null, "NS casts can contain only structured types"); | |
Type clrType = context.Model.GetClrType(structured); | |
Expression source = BindCastSourceNode(node.Source, context); | |
return Expression.TypeAs(source, clrType); | |
} |
There doesn't seem to be any consistency between explicit types and var
in the project.
But if someone of the organization feels the same way, I'll make the changes.
|
||
Type clrType = Model.GetClrType(structured); | ||
|
||
Expression source = Bind(node.Source); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we stick with using var for consistency ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See previous comment.
{ | ||
List<Product> productList = new List<Product>(); | ||
|
||
Product p1 = new DerivedProduct |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we stick with using var for consistency ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See previous comment.
@@ -1584,6 +1720,22 @@ public IQueryable<Customer> Get() | |||
return _customers.AsQueryable(); | |||
} | |||
} | |||
|
|||
public class ProductsForTypeCastController : ODataController |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we document these public classes and methods ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, we're in a test file, so the other classes in this file have no comments.
But if someone of the organization feels the same way, I'll make the changes.
@clemvnt Will you continue working on this PR? |
Yes. I'm waiting for a new version of Microsoft.OData.Core with the OData/odata.net#2879 fix before publishing the PR. |
I've changed the base branch to main. This PR requires the next version of Microsoft.OData.Core (8.0.2). Other than that, it's ready. |
This PR is ready. |
@clemvnt Are you still working on this PR? |
I fixed the merge conflict. The PR is ready. cc. @WanjohiSammy |
test/Microsoft.AspNetCore.OData.Tests/Query/Query/ApplyQueryOptionTest.cs
Outdated
Show resolved
Hide resolved
@clemvnt Please check out the failing tests Also test for derived complex type (in particular) as per scenario:
Request URL
|
|
@clemvnt The specific scenario I asked you to verify is one where the derived type is a complex type, not entity type. Here's an illustration of a scenario that seem to proceed unexpected results (sharing only the bits you'd need to repro): // Data models
namespace Microsoft.AspNetCore.OData.E2E.Tests.DollarApply
{
public class MyCustomer
{
public int Id { get; set; }
public MyAddress Address { get; set; }
}
public class MyAddress
{
public string Street { get; set; }
}
public class MyHomeAddress : MyAddress
{
public string HomeNo { get; set; }
public MyCity City { get; set; }
}
public class MyCity
{
public string Name { get; set; }
}
}
// Edm model
var builder = new ODataConventionModelBuilder();
builder.EntitySet<MyCustomer>("MyCustomers");
builder.ComplexType<MyAddress>();
builder.ComplexType<MyHomeAddress>();
builder.ComplexType<MyCity>();
var model = builder.GetEdmModel();
// Controller
public class MyCustomersController : ODataController
{
private static readonly List<MyCustomer> myCustomers = new List<MyCustomer>
{
new MyCustomer
{
Id = 1,
Address = new MyHomeAddress { Street = "High Street", HomeNo = "#1", City = new MyCity { Name = "Belfast" } }
},
new MyCustomer
{
Id = 2,
Address = new MyHomeAddress { Street = "Moore Street", HomeNo = "#2", City = new MyCity { Name = "Dublin" } }
}
};
[EnableQuery]
public ActionResult<IEnumerable<MyCustomer>> Get()
{
return myCustomers;
}
}
// Tests
[Fact]
public async Task TestPrimitivePropertyInDerivedTypeAsync()
{
var queryUrl = "default/MyCustomers?$apply=groupby((Address/Microsoft.AspNetCore.OData.E2E.Tests.DollarApply.MyHomeAddress/HomeNo))";
var request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
var client = CreateClient(); // Initialize client
var response = await client.SendAsync(request);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
// Response as expected: {"value":[{"Address":{"HomeNo":"#1"}},{"Address":{"HomeNo":"#2"}}]}
}
[Fact]
public async Task TestComplexPropertyInDerivedTypeAsync()
{
var queryUrl = "default/MyCustomers?$apply=groupby((Address/Microsoft.AspNetCore.OData.E2E.Tests.DollarApply.MyHomeAddress/City/Name))";
var request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
var client = CreateClient(); // Initialize client
var response = await client.SendAsync(request);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
// Response NOT as expected: {"value":[{"Address":{}},{"Address":{}}]}
} |
Thank you for the clarification. It seems the issue arises from the AspNetCoreOData/src/Microsoft.AspNetCore.OData/Formatter/Serialization/ODataResourceSerializer.cs Lines 334 to 335 in 0ceeb08
Looking at the For context, in my specific use case, I don’t encounter this issue because I bypass the OData formatter. Instead, I manually apply OData clauses to |
@clemvnt I'll need to do some digging to figure out how we can remedy it. At least we now know about it. If it's okay with you, we could merge your fix since it addresses a gap, then open a new ticket to track this scenario. |
Thinking about it some more...
Somehow it works for the primitive |
Sure, I’d be happy to create a new issue for this case and include all the information I have gathered about it.
The EDM property isn’t found even for the primitive AspNetCoreOData/src/Microsoft.AspNetCore.OData/Formatter/Serialization/ODataResourceSerializer.cs Lines 368 to 372 in 0ceeb08
For the |
Hello,
Cast type support in group by.
This PR needs OData/odata.net#2879.
Issues
#1180
Description
For the following group by :
groupby((MyAddress/Fully.Qualified.Namespace.HomeAddress/HomeNO))
The response was an error :
Now, the response is :