Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static class RelationalJsonUtilities

writer.Flush();

return Encoding.UTF8.GetString(stream.ToArray());
return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);

void WriteJson(Utf8JsonWriter writer, IComplexType complexType, object? value, bool collection)
{
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Update/ModificationCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ void HandleJson(List<IColumnModification> columnModifications)
writer.Flush();

var value = writer.BytesCommitted > 0
? Encoding.UTF8.GetString(stream.ToArray())
? Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length)
: null;

columnModifications.Add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,51 +43,56 @@ public SqlServerModificationCommand(in NonTrackedModificationCommandParameters m
/// </summary>
protected override void ProcessSinglePropertyJsonUpdate(ref ColumnModificationParameters parameters)
{
// See: Issue #34432
// TODO: Move more of this logic to the type mapping. Issue #34432
var property = parameters.Property!;
var mapping = property.GetRelationalTypeMapping();
var propertyProviderClrType = (mapping.Converter?.ProviderClrType ?? property.ClrType).UnwrapNullableType();
var value = parameters.Value;

// JSON-compatible non-string values (bool, numeric, null) are sent directly as non-string parameters.
if (value is null
|| propertyProviderClrType == typeof(bool)
|| propertyProviderClrType.IsNumeric())
|| ((propertyProviderClrType == typeof(bool)
|| propertyProviderClrType.IsNumeric())
&& !property.IsPrimitiveCollection))
{
parameters = parameters with { Value = value, TypeMapping = mapping };

return;
}
else

var jsonValueReaderWriter = mapping.JsonValueReaderWriter;
if (jsonValueReaderWriter != null)
{
// Everything else must go as either a string parameter or a json parameter, depending on whether the json type
// is being used or not. To determine this, we get the JSON value and check if it is a string or some other
// type of JSON object.
var jsonValueReaderWriter = mapping.JsonValueReaderWriter;
if (jsonValueReaderWriter != null)
if (property.IsPrimitiveCollection)
{
var stringValue = jsonValueReaderWriter.ToJsonString(value);
if (!stringValue.StartsWith('\"'))
{
// This is actual JSON, so send with the original type mapping, which may indicate the column type is JSON.
parameters = parameters with { Value = stringValue };

return;
}
// This is a JSON array, so send with the original type mapping, which may indicate the column type is JSON.
parameters = parameters with { Value = jsonValueReaderWriter.ToJsonString(value) };

// Otherwise remove the quotes and send the value as a string.
value = stringValue[1..^1];
}
else if (mapping.Converter != null)
{
value = mapping.Converter.ConvertToProvider(value);
return;
}

// Otherwise, wrap the value in a simple JSON object to avoid double escaping.
parameters = parameters with
{
Value = value,
Value = jsonValueReaderWriter.ToJsonObjectString("", value),
TypeMapping = parameters.TypeMapping is SqlServerStructuralJsonTypeMapping
? SqlServerStringTypeMapping.UnicodeDefault
: parameters.TypeMapping
? parameters.TypeMapping
: SqlServerStructuralJsonTypeMapping.Default
};

return;
}
else if (mapping.Converter != null)
{
value = mapping.Converter.ConvertToProvider(value);
}

parameters = parameters with
{
Value = value,
TypeMapping = parameters.TypeMapping is SqlServerStructuralJsonTypeMapping
? SqlServerStringTypeMapping.UnicodeDefault
: parameters.TypeMapping
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,33 +141,42 @@ protected override void AppendUpdateColumnValue(
string name,
string? schema)
{
if (columnModification.JsonPath is not (null or "$"))
if (columnModification.JsonPath is null or "$")
{
stringBuilder.Append("JSON_MODIFY(");
updateSqlGeneratorHelper.DelimitIdentifier(stringBuilder, columnModification.ColumnName);
base.AppendUpdateColumnValue(updateSqlGeneratorHelper, columnModification, stringBuilder, name, schema);
return;
}

// using strict so that we don't remove json elements when they are assigned NULL value
stringBuilder.Append(", 'strict ");
stringBuilder.Append(columnModification.JsonPath);
stringBuilder.Append("', ");
stringBuilder.Append("JSON_MODIFY(");
updateSqlGeneratorHelper.DelimitIdentifier(stringBuilder, columnModification.ColumnName);

if (columnModification.Property is { IsPrimitiveCollection: false })
{
base.AppendUpdateColumnValue(updateSqlGeneratorHelper, columnModification, stringBuilder, name, schema);
}
else
{
stringBuilder.Append("JSON_QUERY(");
base.AppendUpdateColumnValue(updateSqlGeneratorHelper, columnModification, stringBuilder, name, schema);
stringBuilder.Append(")");
}
// using strict so that we don't remove json elements when they are assigned NULL value
stringBuilder.Append(", 'strict ");
stringBuilder.Append(columnModification.JsonPath);
stringBuilder.Append("', ");
var mapping = columnModification.Property?.GetRelationalTypeMapping();
var propertyProviderClrType = (mapping?.Converter?.ProviderClrType ?? columnModification.Property?.ClrType)?.UnwrapNullableType();

stringBuilder.Append(")");
if (columnModification.Value is null
|| propertyProviderClrType == typeof(bool)
|| (propertyProviderClrType != null && propertyProviderClrType.IsNumeric()))
{
base.AppendUpdateColumnValue(updateSqlGeneratorHelper, columnModification, stringBuilder, name, schema);
}
else if (columnModification.Property is { IsPrimitiveCollection: false })
{
stringBuilder.Append("JSON_VALUE(");
base.AppendUpdateColumnValue(updateSqlGeneratorHelper, columnModification, stringBuilder, name, schema);
stringBuilder.Append(", '$.\"\"')");
}
else
{
stringBuilder.Append("JSON_QUERY(");
base.AppendUpdateColumnValue(updateSqlGeneratorHelper, columnModification, stringBuilder, name, schema);
stringBuilder.Append(")");
}

stringBuilder.Append(")");
}

/// <summary>
Expand Down
33 changes: 31 additions & 2 deletions src/EFCore/Storage/Json/JsonValueReaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,38 @@ public string ToJsonString(object value)
ToJson(writer, value);

writer.Flush();
var buffer = stream.ToArray();

return Encoding.UTF8.GetString(buffer);
return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
}

/// <summary>
/// Returns a string containg a JSON object whith the specified property and value.
/// </summary>
/// <param name="propertyName">The property name.</param>
/// <param name="value">The value to write.</param>
/// <returns>The JSON representation of a JSON object whith the specified property and value.</returns>
public virtual string ToJsonObjectString(string propertyName, object? value)
{
Check.NotNull(propertyName);

using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream);

writer.WriteStartObject();
writer.WritePropertyName(propertyName);
if (value == null)
{
writer.WriteNullValue();
}
else
{
ToJson(writer, value);
}

writer.WriteEndObject();
writer.Flush();

return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3624,6 +3624,53 @@ public virtual Task Add_and_update_nested_optional_primitive_collection(bool? va
}
});

[ConditionalFact]
public virtual Task Edit_single_property_with_non_ascii_characters()
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
async context =>
{
var query = await context.JsonEntitiesBasic.ToListAsync();
var entity = query.Single(x => x.Id == 1);
entity.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf.SomethingSomething = "测试1";

var newEntity = new JsonEntityBasic
{
Id = 3,
Name = "ComprehensiveEntity",
OwnedCollectionRoot = [],
OwnedReferenceRoot = new JsonOwnedRoot
{
Name = "ReferenceRoot",
Number = 300,
OwnedCollectionBranch = [],
OwnedReferenceBranch = new JsonOwnedBranch
{
Id = 15,
Date = new DateTime(2023, 10, 5),
Enum = JsonEnum.Three,
Fraction = 99.99m,
OwnedCollectionLeaf = [],
OwnedReferenceLeaf = new JsonOwnedLeaf { SomethingSomething = "测试1" }
}
}
};

context.Add(newEntity);

ClearLog();
await context.SaveChangesAsync();
},
async context =>
{
var result = await context.Set<JsonEntityBasic>().SingleAsync(x => x.Id == 3);
Assert.Equal("测试1", result.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf.SomethingSomething);

result = await context.Set<JsonEntityBasic>().SingleAsync(x => x.Id == 1);
Assert.Equal("测试1", result.OwnedReferenceRoot.OwnedReferenceBranch.OwnedReferenceLeaf.SomethingSomething);
});

public void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());

Expand Down
4 changes: 1 addition & 3 deletions test/EFCore.Specification.Tests/JsonTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3773,9 +3773,7 @@ protected string ToJsonPropertyString(JsonValueReaderWriter jsonReaderWriter, ob
writer.WriteEndObject();
writer.Flush();

var buffer = stream.ToArray();

return Encoding.UTF8.GetString(buffer);
return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
}

protected object? FromJsonPropertyString(JsonValueReaderWriter jsonReaderWriter, string value, object? existingValue = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public enum SqlServerCondition
SupportsFunctions2019 = 1 << 13,
SupportsFunctions2022 = 1 << 14,
SupportsJsonType = 1 << 15,
SupportsVectorType = 1 << 15,
SupportsVectorType = 1 << 16,
}
Loading
Loading