Skip to content

Commit 98cb88d

Browse files
authored
Merge pull request #225 from graphql-dotnet/fix-inconsistency-in-mapconverters
Fix inconsistency in mapconverters
2 parents 584d816 + 451791b commit 98cb88d

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

src/GraphQL.Client.Serializer.Newtonsoft/MapConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Runtime.InteropServices;
3+
using System.Linq;
44
using Newtonsoft.Json;
55
using Newtonsoft.Json.Linq;
66

@@ -24,11 +24,11 @@ public override Map ReadJson(JsonReader reader, Type objectType, Map existingVal
2424
throw new ArgumentException("This converter can only parse when the root element is a JSON Object.");
2525
}
2626

27-
private object ReadToken(JToken? token) =>
27+
private object? ReadToken(JToken? token) =>
2828
token switch
2929
{
3030
JObject jObject => ReadDictionary<Dictionary<string, object>>(jObject),
31-
JArray jArray => ReadArray(jArray),
31+
JArray jArray => ReadArray(jArray).ToList(),
3232
JValue jValue => jValue.Value,
3333
JConstructor _ => throw new ArgumentOutOfRangeException(nameof(token.Type),
3434
"cannot deserialize a JSON constructor"),
@@ -51,7 +51,7 @@ private TDictionary ReadDictionary<TDictionary>(JToken element) where TDictionar
5151
return result;
5252
}
5353

54-
private IEnumerable<object> ReadArray(JArray element)
54+
private IEnumerable<object?> ReadArray(JArray element)
5555
{
5656
foreach (var item in element)
5757
{

src/GraphQL.Client.Serializer.SystemTextJson/MapConverter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ private TDictionary ReadDictionary<TDictionary>(JsonElement element) where TDict
4141
return result;
4242
}
4343

44-
private IEnumerable<object> ReadArray(JsonElement value)
44+
private IEnumerable<object?> ReadArray(JsonElement value)
4545
{
4646
foreach (var item in value.EnumerateArray())
4747
{
4848
yield return ReadValue(item);
4949
}
5050
}
5151

52-
private object ReadValue(JsonElement value)
52+
private object? ReadValue(JsonElement value)
5353
=> value.ValueKind switch
5454
{
5555
JsonValueKind.Array => ReadArray(value).ToList(),
@@ -65,15 +65,15 @@ private object ReadValue(JsonElement value)
6565

6666
private object ReadNumber(JsonElement value)
6767
{
68-
if (value.TryGetInt32(out var i))
68+
if (value.TryGetInt32(out int i))
6969
return i;
70-
else if (value.TryGetInt64(out var l))
70+
else if (value.TryGetInt64(out long l))
7171
return l;
7272
else if (BigInteger.TryParse(value.GetRawText(), out var bi))
7373
return bi;
74-
else if (value.TryGetDouble(out var d))
74+
else if (value.TryGetDouble(out double d))
7575
return d;
76-
else if (value.TryGetDecimal(out var dd))
76+
else if (value.TryGetDecimal(out decimal dd))
7777
return dd;
7878

7979
throw new NotImplementedException($"Unexpected Number value. Raw text was: {value.GetRawText()}");
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Collections.Generic;
2+
using FluentAssertions;
3+
using FluentAssertions.Execution;
4+
using GraphQL.Client.Serializer.Newtonsoft;
5+
using GraphQL.Client.Serializer.SystemTextJson;
6+
using Newtonsoft.Json;
7+
using Xunit;
8+
9+
namespace GraphQL.Client.Serializer.Tests
10+
{
11+
public class ConsistencyTests
12+
{
13+
[Fact]
14+
public void MapConvertersShouldBehaveConsistent()
15+
{
16+
const string json = @"{
17+
""array"": [
18+
""some stuff"",
19+
""something else""
20+
],
21+
""string"": ""this is a string"",
22+
""boolean"": true,
23+
""number"": 1234.567,
24+
""nested object"": {
25+
""prop1"": false
26+
},
27+
""arrayOfObjects"": [
28+
{""number"": 1234.567},
29+
{""number"": 567.8}
30+
]
31+
}";
32+
33+
var newtonsoftSerializer = new NewtonsoftJsonSerializer();
34+
var systemTextJsonSerializer = new SystemTextJsonSerializer();
35+
36+
var newtonsoftMap = JsonConvert.DeserializeObject<Map>(json, newtonsoftSerializer.JsonSerializerSettings);
37+
var systemTextJsonMap = System.Text.Json.JsonSerializer.Deserialize<Map>(json, systemTextJsonSerializer.Options);
38+
39+
40+
using(new AssertionScope())
41+
{
42+
CompareMaps(newtonsoftMap, systemTextJsonMap);
43+
}
44+
45+
newtonsoftMap.Should().BeEquivalentTo(systemTextJsonMap, options => options
46+
.RespectingRuntimeTypes());
47+
}
48+
49+
private void CompareMaps(Dictionary<string, object> first, Dictionary<string, object> second)
50+
{
51+
foreach (var keyValuePair in first)
52+
{
53+
second.Should().ContainKey(keyValuePair.Key);
54+
second[keyValuePair.Key].Should().BeOfType(keyValuePair.Value.GetType());
55+
if(keyValuePair.Value is Dictionary<string, object> map)
56+
CompareMaps(map, (Dictionary<string, object>)second[keyValuePair.Key]);
57+
else
58+
keyValuePair.Value.Should().BeEquivalentTo(second[keyValuePair.Key]);
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)