-
Notifications
You must be signed in to change notification settings - Fork 494
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
LINQ-constructed document queries ignore Json annotations based on type constraint #484
Comments
This is an interesting problem. In v3 we allow a custom serializer. A lot of people have shown interest in using .NET core 3.0 system.text.json serializer because of the performance increase it has over Json.NET. Only option I see is if we added some support for a custom serializer for the property names. @simplynaveen20 If we just assume the user is always using the default Json.NET implementation could we add support for Json.NET attribute tags? |
What seems weird to me is that the query builder behaves differently depending on the constraint on T. When the concrete type is used as constraint, the annotations are considered correctly. When the interface is used, the annotations are not considered. Even though T is the same concrete type in both cases and the annotations are present on T in both functions per reflection. |
The reason this is happening is the constraint on T causes everything to be down cast to ITest as the type. Based on the code right now we don't check for derived types. You can see the code here. |
Will have a look to this POST GA |
Any updates on this issue? |
Any updates? The following changes in ExpressionToSQL.cs work for me: PropertyInfo targetProperty = inputExpression.Expression.Type.GetProperty(inputExpression.Member.Name);
string memberName = targetProperty.GetMemberName(context?.serializationOptions); Of course, this is just for demo purpose only ... :) |
@bchong95 can you take a look? |
Workaround, until this is fixed: apply the JsonProperty attributes on the interface properties. It's ugly, but it works. |
@thomaslevesque Upvote for the simple solution; banging my head against this last night for a few hours! |
Is this something that will be addressed in V3 or should we plan to work around? |
@leminh98 can you take a look at this? |
Hi all, we've merged what we hope will be a solution to this issue by allowing the custom serializer to be used for LINQ translations. This also unblocks having System.Text.Json attributes applied in LINQ queries. We plan to release this as part of the next preview package, but I also wanted to take this time to confirm these changes meet your needs before finalizing them as part of our public API. If you try the changes out, please follow up in the thread if this update addresses your concerns. If it doesn't, please let us know why. To use your own custom serializer for LINQ translations you must implement CosmosLinqSerializer and set it as a custom serializer on the CosmosClient. There is a sample of this for serialization with System.Text.Json in CosmosLinqSerializer.cs. |
Describe the bug
When using LINQ to create queries in a generic function with a constraint on the document's type parameter, the query generator ignores annotations on the concrete type. Specifically, the generator ignores Json serialization annotations on the properties, leading to incorrect queries.
To Reproduce
Expected behavior
Query generator should consider annotation on
Test
class. Expected output:Actual behavior
Query generator ignores annotation on
Test
class. Actual output:Environment summary
SDK Version: Microsoft.Azure.WebJobs.Extensions.CosmosDB 3.0.3, .NET Core 2.2
OS Version: Windows 10,
The text was updated successfully, but these errors were encountered: