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

Fix to #32192 - Remove lookup of JsonTypeMapping via JsonElement #35788

Merged
merged 1 commit into from
Mar 16, 2025
Merged

Conversation

maumar
Copy link
Contributor

@maumar maumar commented Mar 14, 2025

When adding support for JSON mapped types, we decided to use JsonElement as a CLR type marker for JsonTypeMapping. This decision was in hindsight incorrect, it makes it hard for providers that actually support weak-typed json (npgsql) and throws cryptic exception (No coercion operator is defined between types 'System.IO.MemoryStream' and 'System.Text.Json.JsonElement) for providers that don't support it, when customers try to do it regardless. Fix is to create a dummy type and use that instead, so JsonElement is no longer recognized by base EFCore.

Fixes #32192
also fixes #34752

@maumar maumar force-pushed the fix32192 branch 2 times, most recently from ce9f999 to 12bbeb2 Compare March 14, 2025 01:19
@maumar maumar requested review from Copilot, roji and AndriySvyryd March 14, 2025 01:21
/// <summary>
/// A type representing CLR type of the JsonTypeMapping.
/// </summary>
public class JsonClrType
Copy link
Contributor Author

@maumar maumar Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name suggestions are welcome @dotnet/efteam

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move it to .Storage and make the default constructor private.

My naming suggestion is JsonTypePlaceholder

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes issues with JSON type mapping by introducing a new dummy CLR type (JsonClrType) to replace usage of JsonElement in EFCore, preventing misleading exceptions on providers that don’t support weak-typed JSON.

  • Introduces the JsonClrType dummy marker type.
  • Updates various Relational, SqlServer, and Sqlite mapping files to use JsonClrType instead of JsonElement.
  • Updates tests and scaffolding baselines accordingly.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/EFCore.Relational/Metadata/JsonClrType.cs New type definition for JsonClrType as a CLR marker.
test/EFCore.Relational.Specification.Tests/Query/AdHocMiscellaneousQueryRelationalTestBase.cs Added test to ensure a meaningful exception is thrown for a JsonElement property.
src/EFCore.Relational/Metadata/Internal/JsonColumnBase.cs Changed default type mapping lookup from JsonElement to JsonClrType.
src/EFCore.SqlServer/Storage/Internal/SqlServerTypeMappingSource.cs Modified type check for JSON mapping to use JsonClrType.
src/EFCore.Relational/Storage/JsonTypeMapping.cs Updated comments and mapping references from JsonElement to a generic description.
src/EFCore.Relational/Metadata/Internal/JsonViewColumn.cs Adjusted default store type mapping to use JsonClrType.
src/EFCore.Relational/Metadata/Internal/JsonColumn.cs Replaced JsonElement with JsonClrType in default mapping lookup.
src/EFCore.Sqlite.Core/Storage/Internal/SqliteJsonTypeMapping.cs Changed constructor parameter and mapping type to JsonClrType.
src/EFCore.Sqlite.Core/Storage/Internal/SqliteTypeMappingSource.cs Updated mapping dictionary entry from JsonElement to JsonClrType.
src/EFCore.SqlServer/Storage/Internal/SqlServerOwnedJsonTypeMapping.cs Adjusted base constructor call to use JsonClrType.
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextModelBuilder.cs Updated generic column accessors to reference JsonClrType.
src/EFCore.Relational/Metadata/Internal/RelationalModel.cs Changed lookup in relation to JSON column type mapping to use JsonClrType.
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/BigModel_with_JSON_columns/DbContextModelBuilder.cs Updated generic column accessors to reference JsonClrType.
@maumar maumar marked this pull request as ready for review March 14, 2025 01:24
@maumar maumar requested a review from a team as a code owner March 14, 2025 01:24
When adding support for JSON mapped types, we decided to use JsonElement as a CLR type marker for JsonTypeMapping. This decision was in hindsight incorrect, it makes it hard for providers that actually support weak-typed json (npgsql) and throws cryptic exception (No coercion operator is defined between types 'System.IO.MemoryStream' and 'System.Text.Json.JsonElement) for providers that don't support it, when customers try to do it regardless.
Fix is to create a dummy type and use that instead, so JsonElement is no longer recognized by base EFCore.

Fixes #32192
also fixes #34752
@maumar maumar merged commit f94e421 into main Mar 16, 2025
7 checks passed
@maumar maumar deleted the fix32192 branch March 16, 2025 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve exception for JsonElement with Sqlite Remove lookup of JsonTypeMapping via JsonElement
2 participants