Skip to content

Commit a1a3978

Browse files
authored
Merge pull request #241 from wirmar/fix/234_nullableStructSerialization
don't use ImmutableConverter for nullable structs
2 parents 2db0ce7 + 99814cc commit a1a3978

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public override bool CanConvert(Type typeToConvert)
1919
return false;
2020

2121
var nullableUnderlyingType = Nullable.GetUnderlyingType(typeToConvert);
22-
if (nullableUnderlyingType != null && nullableUnderlyingType.IsPrimitive)
22+
if (nullableUnderlyingType != null && nullableUnderlyingType.IsValueType)
2323
return false;
2424

2525
bool result;
@@ -40,7 +40,7 @@ public override bool CanConvert(Type typeToConvert)
4040
foreach (var parameter in parameters)
4141
{
4242
var hasMatchingProperty = properties.Any(p =>
43-
NameOfPropertyAndParameter.Matches(p.Name, parameter.Name, typeToConvert.IsAnonymous()));
43+
NameOfPropertyAndParameter.Matches(p.Name, parameter.Name, typeToConvert.IsAnonymous()));
4444
if (!hasMatchingProperty)
4545
{
4646
result = false;
@@ -90,7 +90,7 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
9090
{
9191
var parameterInfo = parameters[index];
9292
var value = valueOfProperty.First(prop =>
93-
NameOfPropertyAndParameter.Matches(prop.Key.Name, parameterInfo.Name, typeToConvert.IsAnonymous())).Value;
93+
NameOfPropertyAndParameter.Matches(prop.Key.Name, parameterInfo.Name, typeToConvert.IsAnonymous())).Value;
9494

9595
parameterValues[index] = value;
9696
}
@@ -120,6 +120,7 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp
120120
if (!(converter is ImmutableConverter))
121121
strippedOptions.Converters.Add(converter);
122122
}
123+
123124
JsonSerializer.Serialize(writer, value, strippedOptions);
124125
}
125126

tests/GraphQL.Client.Serializer.Tests/BaseSerializerTest.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ public async void DeserializeFromUtf8StreamTest(string json, IGraphQLResponse ex
5858
var jsonBytes = Encoding.UTF8.GetBytes(json);
5959
await using var ms = new MemoryStream(jsonBytes);
6060
var response = await DeserializeToUnknownType(expectedResponse.Data?.GetType() ?? typeof(object), ms);
61+
6162
//var response = await Serializer.DeserializeFromUtf8StreamAsync<object>(ms, CancellationToken.None);
6263

6364
response.Data.Should().BeEquivalentTo(expectedResponse.Data, options => options.WithAutoConversion());
6465

6566
if (expectedResponse.Errors is null)
6667
response.Errors.Should().BeNull();
67-
else {
68+
else
69+
{
6870
using (new AssertionScope())
6971
{
7072
response.Errors.Should().NotBeNull();
@@ -95,7 +97,7 @@ public async Task<IGraphQLResponse> DeserializeToUnknownType(Type dataType, Stre
9597
{
9698
MethodInfo mi = Serializer.GetType().GetMethod("DeserializeFromUtf8StreamAsync", BindingFlags.Instance | BindingFlags.Public);
9799
MethodInfo mi2 = mi.MakeGenericMethod(dataType);
98-
var task = (Task) mi2.Invoke(Serializer, new object[] { stream, CancellationToken.None });
100+
var task = (Task)mi2.Invoke(Serializer, new object[] { stream, CancellationToken.None });
99101
await task;
100102
var resultProperty = task.GetType().GetProperty("Result", BindingFlags.Public | BindingFlags.Instance);
101103
var result = resultProperty.GetValue(task);
@@ -105,9 +107,8 @@ public async Task<IGraphQLResponse> DeserializeToUnknownType(Type dataType, Stre
105107
[Fact]
106108
public async void CanDeserializeExtensions()
107109
{
108-
109110
var response = await ChatClient.SendQueryAsync(new GraphQLRequest("query { extensionsTest }"),
110-
() => new { extensionsTest = "" })
111+
() => new { extensionsTest = "" })
111112
;
112113

113114
response.Errors.Should().NotBeNull();
@@ -134,8 +135,8 @@ query Droid($id: String!) {
134135
name
135136
}
136137
}",
137-
new { id = id.ToString() },
138-
"Human");
138+
new { id = id.ToString() },
139+
"Human");
139140

140141
var response = await StarWarsClient.SendQueryAsync(graphQLRequest, () => new { Human = new { Name = string.Empty } });
141142

@@ -152,7 +153,6 @@ public async void CanDoSerializationWithPredefinedTypes()
152153
Assert.Equal(message, response.Data.AddMessage.Content);
153154
}
154155

155-
156156
public class WithNullable
157157
{
158158
public int? NullableInt { get; set; }
@@ -172,5 +172,25 @@ public void CanSerializeNullableInt()
172172

173173
action.Should().NotThrow();
174174
}
175+
176+
public class WithNullableStruct
177+
{
178+
public DateTime? NullableStruct { get; set; }
179+
}
180+
181+
[Fact]
182+
public void CanSerializeNullableStruct()
183+
{
184+
Action action = () => Serializer.SerializeToString(new GraphQLRequest
185+
{
186+
Query = "{}",
187+
Variables = new WithNullableStruct
188+
{
189+
NullableStruct = DateTime.Now
190+
}
191+
});
192+
193+
action.Should().NotThrow();
194+
}
175195
}
176196
}

0 commit comments

Comments
 (0)