Skip to content

Commit c4aa7e0

Browse files
Copilotrenemadsen
andcommitted
Fix: Return byte[] type mapping for JsonTypePlaceholder
When EF Core passes JsonTypePlaceholder (for complex JSON types), return a MySqlJsonTypeMapping<byte[]> instead of MySqlJsonTypeMapping<string>. This makes ClrType != typeof(string), so the CustomizeDataReaderExpression logic correctly converts string to MemoryStream for complex JSON types. Regular JSON columns still use MySqlJsonTypeMapping<string> with no conversion. This resolves both issues: - Complex JSON types get string → MemoryStream conversion (fixes coercion error) - Regular JSON columns stay as strings (no SQL generation issues) Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
1 parent 2f2e9b3 commit c4aa7e0

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/EFCore.MySql/Storage/Internal/MySqlTypeMappingSource.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ private readonly RelationalTypeMapping _binaryRowVersion6
8989

9090
// JSON default mapping
9191
private MySqlJsonTypeMapping<string> _jsonDefaultString;
92+
// JSON mapping for complex types (JsonTypePlaceholder)
93+
private MySqlJsonTypeMapping<byte[]> _jsonComplex;
9294

9395
// Scaffolding type mappings
9496
private readonly MySqlCodeGenerationMemberAccessTypeMapping _codeGenerationMemberAccess = MySqlCodeGenerationMemberAccessTypeMapping.Default;
@@ -135,6 +137,7 @@ private void Initialize()
135137
: null;
136138

137139
_jsonDefaultString = new MySqlJsonTypeMapping<string>("json", null, null, _options.NoBackslashEscapes, _options.ReplaceLineBreaksWithCharFunction);
140+
_jsonComplex = new MySqlJsonTypeMapping<byte[]>("json", null, null, _options.NoBackslashEscapes, _options.ReplaceLineBreaksWithCharFunction);
138141

139142
_storeTypeMappings
140143
= new Dictionary<string, RelationalTypeMapping[]>(StringComparer.OrdinalIgnoreCase)
@@ -321,10 +324,11 @@ private RelationalTypeMapping FindRawMapping(RelationalTypeMappingInfo mappingIn
321324
// fail immediately.
322325

323326
// Special case for JSON columns: EF Core passes JsonTypePlaceholder as the CLR type
324-
// when creating JSON columns for complex types/collections. Return our JSON mapping.
327+
// when creating JSON columns for complex types/collections. Return our JSON mapping
328+
// with byte[] as ClrType so the CustomizeDataReaderExpression logic triggers conversion.
325329
if (clrType?.Name == "JsonTypePlaceholder" && storeTypeName.Equals("json", StringComparison.OrdinalIgnoreCase))
326330
{
327-
return _jsonDefaultString;
331+
return _jsonComplex;
328332
}
329333

330334
return clrType == null

0 commit comments

Comments
 (0)