-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
JSON escape regression from EF Core 8 to 9 #35400
Comments
/cc @maumar, IIRC you did the work in this area. |
related/dupe: #30315 |
Utf8JsonWriter that we use (starting in EF9) to construct JSON objects always escapes the string values by default for security reasons (https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/use-utf8jsonwriter#customize-character-escaping and https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-encoding). This is the source of the break. |
@sveinungf you can workaround the issue by using https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.unescape?view=net-9.0 #30744 is tracking the work to add global customization options for json reader/writer, which includes the encoder For now, you can also try to replace JsonReaderWriter with a custom implementation which would unescape string when it's reading it from the json reader. We only have metadata API for this at the moment - it's called SetJsonValueReaderWriterType. You would need to copy the implementation of current JsonStringReaderWriter and change FromJsonTyped to something like: public override string FromJsonTyped(ref Utf8JsonReaderManager manager, object? existingObject = null)
{
var result = manager.CurrentReader.GetString()!;
return Regex.Unescape(result);
} But keep in mind, Utf8JsonWriter is escaping everything for security reasons, so make sure you are not exposing your app to some problems, e.g. if the inputs are coming from untrusted source. |
Is this breaking change documented anywhere? This broke a lot of data in my application. |
I'm in the process of upgrading an application from EF Core 8 to EF Core 9, but I'm encountering a change in how JSON is being escaped.
I have an entity with my own
LocalizableString
type that I'm storing as JSON by usingToJson()
in the model configuration. For an existing row, if I attempt to update a property onLocalizableString
with a non-ASCII character, then it seems like the value is now escaped twice. When reading the value back from the database with EF, the value appears different than what EF stored.Include your code
Here is code to reproduce the problem:
The generated update query is slightly different depending on the EF version.
Here is the query for EF 8:
Here is the query for EF 9:
It seems to only be a problem when updating a property on the JSON serialized type. Replacing the instance works as expected.
Include provider and version information
EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 9.0
Operating system: Windows 11
IDE: Visual Studio Professional 2022 17.12
The text was updated successfully, but these errors were encountered: