You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using Pomelo and Newtonsoft.Json if you have an entity with a column marked as json when querying for a field in this column, Pomelo generates a query that uses a MySQL function to deal with JSON values but when the path to the property is generated it does not take into account that the properties may be being serialized in a format other than the default. Pomelo does take into account the JsonProperty attribute but with Newtonsoft.Json you can also configure globally how you want to serialize property names (for example in camelCase) and it is not being taken into account
and will return 0 rows in response, wrongly, due to '$.StringData.StringValue'
if we change the definition of the entities by adding the JsonProperty attribute it works fine, but it is very inconvenient and goes against the idea of defining this behavior globally
If Newtonsoft.Json is configured globally, Pomelo does not use the serialization convention for property names defined there when generating the paths for MySQL Json functions, resulting in invalid queries and/or not returning the expected values
Suggestion
I reviewed the Pomelo source code a bit and although I can't assimilate it 100%, I have seen that somewhere, for example, in MySqlJsonPocoTranslator you do:
I think we should allow users to configure default JSON settings via the model, and then allow them to override those for entity properties or using the MySqlJsonNewtonsoftPocoValueConverter<T>.
There might be other areas in an app (or even within a DbContext model) that need to use JSON (de-/)serialization with different settings.
Steps to reproduce
using
Pomelo
andNewtonsoft.Json
if you have an entity with a column marked asjson
when querying for a field in this column, Pomelo generates a query that uses a MySQL function to deal with JSON values but when the path to the property is generated it does not take into account that the properties may be being serialized in a format other than the default.Pomelo
does take into account theJsonProperty
attribute but withNewtonsoft.Json
you can also configure globally how you want to serialize property names (for example incamelCase
) and it is not being taken into accountEXAMPLE
Given:
an entity instance like:
will be saved on the db like:
(note the camelCase usage in property names)
the query:
is translated to:
and will return 0 rows in response, wrongly, due to
'$.StringData.StringValue'
if we change the definition of the entities by adding the
JsonProperty
attribute it works fine, but it is very inconvenient and goes against the idea of defining this behavior globallygiven:
the serialization of the entity will be the same and the query is translated to:
and it will return 1 row
also if we do not use the global configuration
the entity will be saved like:
and the query is translated to:
retrieving 1 row as expected
The issue
If
Newtonsoft.Json
is configured globally,Pomelo
does not use the serialization convention for property names defined there when generating the paths for MySQL Json functions, resulting in invalid queries and/or not returning the expected valuesSuggestion
I reviewed the
Pomelo
source code a bit and although I can't assimilate it 100%, I have seen that somewhere, for example, inMySqlJsonPocoTranslator
you do:Pomelo.EntityFrameworkCore.MySql/src/EFCore.MySql/Query/ExpressionTranslators/Internal/MySqlJsonPocoTranslator.cs
Lines 45 to 48 in 27f2273
and
Pomelo.EntityFrameworkCore.MySql/src/EFCore.MySql.Json.Newtonsoft/Query/Internal/MySqlJsonNewtonsoftPocoTranslator.cs
Lines 20 to 21 in 27f2273
so, maybe you could check with the
Newtonsoft.Json
global configuration like this:Further technical details
MySQL version: 8.0.32-mysql (docker image)
Operating system: windows (host), linux (container)
Pomelo.EntityFrameworkCore.MySql version: 7.0.0
Pomelo.EntityFrameworkCore.MySql.Json.Newtonsoft: 7.0.0
Microsoft.AspNetCore.App version: 6.0.18
PS: I attached a code where this error can be reproduced : PomeloTest.zip
The text was updated successfully, but these errors were encountered: