Skip to content
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

Handle Json ExtensionData in sql translation visitor #3777

Closed
onionhammer opened this issue Mar 24, 2023 · 16 comments · Fixed by #3834
Closed

Handle Json ExtensionData in sql translation visitor #3777

onionhammer opened this issue Mar 24, 2023 · 16 comments · Fixed by #3834
Assignees

Comments

@onionhammer
Copy link
Contributor

onionhammer commented Mar 24, 2023

Describe the bug
Given an object like this:

class MyEntity
{
    public string MyTypedProperty { get; set; }

    [JsonExtensionData]
    public Dictionary<string, System.Text.Json.JsonElement>? ExtensionData { get; set; }
}

Querying against an entity with extensiondata is not possible within a LINQ expression

{l => (Convert(l.ExtensionData.get_Item("MyTest"), Object) == Convert(100, Object))}

Should convert to:

WHERE root["MyTest"] == 100

Instead converts to

WHERE root["ExtensionData"]["MyTest"] == 100

The underlying entity in Cosmos will serialize like this;

{
    "MyTest": 100
}

So it is impossible to query for the "MyTest" field without dropping down to writing raw QueryDefinitions, in which case you do not have the benefit of strongly-typed expressions for the rest of your query

@onionhammer
Copy link
Contributor Author

Workaround: have your type extend IReadOnlyDictionary<>, however then you would have to provide your own custom JSON serialization/deserialization for this type in order to still serialize the additional (non-extension data) properties

@onionhammer
Copy link
Contributor Author

Note: I would be happy to fix this, but I already have 3 open pull requests on this repository that have not been looked at. Is it worth my time to try to contribute?

@neildsh
Copy link
Contributor

neildsh commented Apr 3, 2023

Hello @onionhammer my apologies that the ball was dropped on your other PRs. I will ping the people who were reviewing those PRs, and we will try to get them merged. We can take this particular issue forward once the other PRs have been resolved. Please note that the other PRs will likely not be merged before next week. Thanks.

@onionhammer
Copy link
Contributor Author

@neildsh Any progress on this front? Again, I would definitely be willing to take a crack at this if it means getting it done sooner, but this PR approval process is pretty arduous.

@onionhammer
Copy link
Contributor Author

@neildsh @adityasa @leminh98 Again happy to look into contributing this if I'll see traction on the PR reviews..

@adityasa
Copy link
Contributor

Hey @onionhammer, I am taking a look at this item. I have looked and approved 2 of your other prs.

@onionhammer
Copy link
Contributor Author

@adityasa ok, let me know if you want any help/tests written

@adityasa
Copy link
Contributor

adityasa commented Apr 26, 2023

@onionhammer are you talking about Newtonsoft's JsonExtensionData attribute or System.Text.Json.Serialization.JsonExtensiondata?

@adityasa
Copy link
Contributor

adityasa commented Apr 26, 2023

@onionhammer if you are open to making this change, we will be happy to review it. Let me know.

@onionhammer
Copy link
Contributor Author

onionhammer commented Apr 26, 2023

@onionhammer are you talking about Newtonsoft's JsonExtensionData attribute or System.Text.Json.Serialization.JsonExtensiondata?

Both should be handled.

I will look into how much effort it will be to implement later this week or early next week.

@adityasa
Copy link
Contributor

@onionhammer fyi test below baselines current behavior which shows that Newtonsoft property is already honored:
#3830

@onionhammer
Copy link
Contributor Author

@adityasa so could the code just not ignore the STJ attribute? Would that be the change?

@adityasa
Copy link
Contributor

adityasa commented May 1, 2023

@onionhammer, I am not sure if it's that straightforward. The sdk uses Newtonsoft.JSON for serialization/deserialization between client and the backend. Newtonsoft library doesn't understand/honor STJ attributes. As a result, this logic needs to be custom implemented on top of existing logic, with due back-compat considerations.

@onionhammer
Copy link
Contributor Author

onionhammer commented May 1, 2023

@adityasa OK I misunderstood what you meant previously. The functionality proposed in this issue doesnt work for either STJ or Newtonsoft attributes - right? I'm not proposing any changes to the way serialization of objects is done, only the behavior of generating cosmos SQL from LINQ.

This would be a breaking change for users who are using Newtonsoft document serializer, but also decorating their classes with STJ [ExtensionData] attribute.

If we want to avoid this breaking change, we will need to add back the ability to utilize the custom serializer during LINQ -> SQL generation; something that needs to be done anyways.., but I would argue is beyond the scope of this issue (a pre-requisite to this issue, perhaps?)

How do you want to proceed?

@onionhammer
Copy link
Contributor Author

onionhammer commented May 1, 2023

@adityasa After looking into this, supporting this is trivial - just a slight modification to GetMemberName and VisitMemberAccess should be needed.

Supporting STJ is still necessary, but support could be split into a separate PR.

Here's a patch you can try https://gist.github.com/onionhammer/24f6f148324a4ecae6cc1bb8a89ea9a8

@onionhammer
Copy link
Contributor Author

And then there were 4 (open PRs)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants