Skip to content

Commit

Permalink
Prevent DataSourceLoadOptionsParser from System.InvalidOperationExcep…
Browse files Browse the repository at this point in the history
…tion on parsing binary filter with value null (#620)
  • Loading branch information
mpreyskurantov committed May 26, 2024
1 parent d259461 commit c94a149
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public void MustNotParseDates() {
Assert.IsType<string>(opts.Filter[1]);
}

[Fact]
public void MustParseNull() {
var opts = new SampleLoadOptions();

DataSourceLoadOptionsParser.Parse(opts, key => {
if(key == DataSourceLoadOptionsParser.KEY_FILTER)
return @"[ ""fieldName"", ""="", null ]";
return null;
});

Assert.Equal(new[] { "fieldName", "=", null }, opts.Filter.Cast<string>());
}

[Fact]
public void MustParseNumericAsString() {
var opts = new SampleLoadOptions();
Expand Down
21 changes: 21 additions & 0 deletions net/DevExtreme.AspNet.Data.Tests/DeserializeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections;
using System.Text.Json;
using Xunit;

namespace DevExtreme.AspNet.Data.Tests {

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

[Theory]
[InlineData(@"[""fieldName"",""="",null]")]
[InlineData(@"[[""fieldName1"",""="",""""],""and"",[""fieldName2"",""="",null]]")]
public void FilterOperandValueCanBeNull(string rawJsonCriteria) {
var deserializedList = JsonSerializer.Deserialize<IList>(rawJsonCriteria, TESTS_DEFAULT_SERIALIZER_OPTIONS);
Assert.Equal(3, deserializedList.Count);
}
}

}
19 changes: 19 additions & 0 deletions net/DevExtreme.AspNet.Data.Tests/DeserializeTestsEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if NEWTONSOFT_TESTS
using System.Collections;
using Newtonsoft.Json;
using Xunit;

namespace DevExtreme.AspNet.Data.Tests {

public class DeserializeTestsEx {
[Theory]
[InlineData(@"[""fieldName"",""="",null]")]
[InlineData(@"[[""fieldName1"",""="",""""],""and"",[""fieldName2"",""="",null]]")]
public void FilterOperandValueCanBeNull(string rawJsonCriteria) {
var deserializedList = JsonConvert.DeserializeObject<IList>(rawJsonCriteria);
Assert.Equal(3, deserializedList.Count);
}
}

}
#endif
3 changes: 3 additions & 0 deletions net/DevExtreme.AspNet.Data/Compatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public static IList UnwrapList(IList deserializedList) {
}

static object UnwrapJsonElement(object deserializeObject) {
if(deserializeObject == null)
return null;

if(!(deserializeObject is JsonElement jsonElement))
throw new InvalidOperationException();

Expand Down

0 comments on commit c94a149

Please sign in to comment.