Skip to content

Commit

Permalink
Extract unwrapping DataSourceLoadOptionsBase IList Filter to Converter (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpreyskurantov committed Apr 15, 2024
1 parent c4b2709 commit f72062e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
namespace DevExtreme.AspNet.Data.Tests {

public class CustomFilterCompilersTests {
static readonly JsonSerializerOptions TESTS_DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) {
Converters = { new ListConverter() }
};

[Fact]
public void OneToManyContains() {
Expand All @@ -35,8 +38,7 @@ public void OneToManyContains() {
var source = new[] { new Category(), new Category() };
source[0].Products.Add(new Product { Name = "Chai" });

var deserializedList = JsonSerializer.Deserialize<IList>(@"[ ""Products"", ""Contains"", ""ch"" ]");
var filter = Compatibility.UnwrapList(deserializedList);
var filter = JsonSerializer.Deserialize<IList>(@"[ ""Products"", ""Contains"", ""ch"" ]", TESTS_DEFAULT_SERIALIZER_OPTIONS);

var loadOptions = new SampleLoadOptions {
Filter = filter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
namespace DevExtreme.AspNet.Data.Tests {

public class FilterExpressionCompilerTests {
static readonly JsonSerializerOptions TESTS_DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) {
Converters = { new ListConverter() }
};

class DataItem1 {
public int IntProp { get; set; }
Expand Down Expand Up @@ -143,8 +146,7 @@ public void Not() {

[Fact]
public void IsUnaryWithJsonCriteria() {
var deserializedList = JsonSerializer.Deserialize<IList>("[\"!\", []]");
var crit = Compatibility.UnwrapList(deserializedList);
var crit = JsonSerializer.Deserialize<IList>("[\"!\", []]", TESTS_DEFAULT_SERIALIZER_OPTIONS);
var compiler = new FilterExpressionCompiler(typeof(object), false);
Assert.True(compiler.IsUnary(crit));
}
Expand Down Expand Up @@ -241,8 +243,7 @@ public void T105740() {

[Fact]
public void JsonObjects() {
var deserializedList = JsonSerializer.Deserialize<IList>(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]");
var crit = Compatibility.UnwrapList(deserializedList);
var crit = JsonSerializer.Deserialize<IList>(@"[ [ ""StringProp"", ""abc"" ], [ ""NullableProp"", null ] ]", TESTS_DEFAULT_SERIALIZER_OPTIONS);
var expr = Compile<DataItem1>(crit);
Assert.Equal(@"((obj.StringProp == ""abc"") AndAlso (obj.NullableProp == null))", expr.Body.ToString());
}
Expand Down
10 changes: 8 additions & 2 deletions net/DevExtreme.AspNet.Data/Compatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ static object GetNumber(ref Utf8JsonReader reader) {
throw new NotImplementedException();
}

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) {
throw new NotImplementedException();
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) => throw new NotImplementedException();
}

class ListConverter : JsonConverter<IList> {
public override IList Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
var deserializedList = JsonSerializer.Deserialize<IList>(ref reader);
return Compatibility.UnwrapList(deserializedList);
}

public override void Write(Utf8JsonWriter writer, IList value, JsonSerializerOptions options) => throw new NotImplementedException();
}

}
3 changes: 2 additions & 1 deletion net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text.Json.Serialization;

namespace DevExtreme.AspNet.Data {

Expand Down Expand Up @@ -56,6 +56,7 @@ public class DataSourceLoadOptionsBase {
/// <summary>
/// A filter expression.
/// </summary>
[JsonConverter(typeof(ListConverter))]
public IList Filter { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace DevExtreme.AspNet.Data.Helpers {
/// A parser for the data processing settings.
/// </summary>
public static class DataSourceLoadOptionsParser {
static readonly JsonSerializerOptions DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web);
static readonly JsonSerializerOptions DEFAULT_SERIALIZER_OPTIONS = new JsonSerializerOptions(JsonSerializerDefaults.Web) {
Converters = { new ListConverter() }
};

public const string
KEY_REQUIRE_TOTAL_COUNT = "requireTotalCount",
Expand Down Expand Up @@ -62,10 +64,8 @@ public static void Parse(DataSourceLoadOptionsBase loadOptions, Func<string, str
if(!String.IsNullOrEmpty(group))
loadOptions.Group = JsonSerializer.Deserialize<GroupingInfo[]>(group, DEFAULT_SERIALIZER_OPTIONS);

if(!String.IsNullOrEmpty(filter)) {
var deserializedList = JsonSerializer.Deserialize<IList>(filter);
loadOptions.Filter = Compatibility.UnwrapList(deserializedList);
}
if(!String.IsNullOrEmpty(filter))
loadOptions.Filter = JsonSerializer.Deserialize<IList>(filter, DEFAULT_SERIALIZER_OPTIONS);

if(!String.IsNullOrEmpty(totalSummary))
loadOptions.TotalSummary = JsonSerializer.Deserialize<SummaryInfo[]>(totalSummary, DEFAULT_SERIALIZER_OPTIONS);
Expand Down

0 comments on commit f72062e

Please sign in to comment.