Skip to content

Commit

Permalink
Merge pull request #241 from wirmar/fix/234_nullableStructSerialization
Browse files Browse the repository at this point in the history
don't use ImmutableConverter for nullable structs
  • Loading branch information
rose-a authored May 29, 2020
2 parents 2db0ce7 + 99814cc commit a1a3978
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override bool CanConvert(Type typeToConvert)
return false;

var nullableUnderlyingType = Nullable.GetUnderlyingType(typeToConvert);
if (nullableUnderlyingType != null && nullableUnderlyingType.IsPrimitive)
if (nullableUnderlyingType != null && nullableUnderlyingType.IsValueType)
return false;

bool result;
Expand All @@ -40,7 +40,7 @@ public override bool CanConvert(Type typeToConvert)
foreach (var parameter in parameters)
{
var hasMatchingProperty = properties.Any(p =>
NameOfPropertyAndParameter.Matches(p.Name, parameter.Name, typeToConvert.IsAnonymous()));
NameOfPropertyAndParameter.Matches(p.Name, parameter.Name, typeToConvert.IsAnonymous()));
if (!hasMatchingProperty)
{
result = false;
Expand Down Expand Up @@ -90,7 +90,7 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
{
var parameterInfo = parameters[index];
var value = valueOfProperty.First(prop =>
NameOfPropertyAndParameter.Matches(prop.Key.Name, parameterInfo.Name, typeToConvert.IsAnonymous())).Value;
NameOfPropertyAndParameter.Matches(prop.Key.Name, parameterInfo.Name, typeToConvert.IsAnonymous())).Value;

parameterValues[index] = value;
}
Expand Down Expand Up @@ -120,6 +120,7 @@ public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOp
if (!(converter is ImmutableConverter))
strippedOptions.Converters.Add(converter);
}

JsonSerializer.Serialize(writer, value, strippedOptions);
}

Expand Down
34 changes: 27 additions & 7 deletions tests/GraphQL.Client.Serializer.Tests/BaseSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ public async void DeserializeFromUtf8StreamTest(string json, IGraphQLResponse ex
var jsonBytes = Encoding.UTF8.GetBytes(json);
await using var ms = new MemoryStream(jsonBytes);
var response = await DeserializeToUnknownType(expectedResponse.Data?.GetType() ?? typeof(object), ms);

//var response = await Serializer.DeserializeFromUtf8StreamAsync<object>(ms, CancellationToken.None);

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

if (expectedResponse.Errors is null)
response.Errors.Should().BeNull();
else {
else
{
using (new AssertionScope())
{
response.Errors.Should().NotBeNull();
Expand Down Expand Up @@ -95,7 +97,7 @@ public async Task<IGraphQLResponse> DeserializeToUnknownType(Type dataType, Stre
{
MethodInfo mi = Serializer.GetType().GetMethod("DeserializeFromUtf8StreamAsync", BindingFlags.Instance | BindingFlags.Public);
MethodInfo mi2 = mi.MakeGenericMethod(dataType);
var task = (Task) mi2.Invoke(Serializer, new object[] { stream, CancellationToken.None });
var task = (Task)mi2.Invoke(Serializer, new object[] { stream, CancellationToken.None });
await task;
var resultProperty = task.GetType().GetProperty("Result", BindingFlags.Public | BindingFlags.Instance);
var result = resultProperty.GetValue(task);
Expand All @@ -105,9 +107,8 @@ public async Task<IGraphQLResponse> DeserializeToUnknownType(Type dataType, Stre
[Fact]
public async void CanDeserializeExtensions()
{

var response = await ChatClient.SendQueryAsync(new GraphQLRequest("query { extensionsTest }"),
() => new { extensionsTest = "" })
() => new { extensionsTest = "" })
;

response.Errors.Should().NotBeNull();
Expand All @@ -134,8 +135,8 @@ query Droid($id: String!) {
name
}
}",
new { id = id.ToString() },
"Human");
new { id = id.ToString() },
"Human");

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

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


public class WithNullable
{
public int? NullableInt { get; set; }
Expand All @@ -172,5 +172,25 @@ public void CanSerializeNullableInt()

action.Should().NotThrow();
}

public class WithNullableStruct
{
public DateTime? NullableStruct { get; set; }
}

[Fact]
public void CanSerializeNullableStruct()
{
Action action = () => Serializer.SerializeToString(new GraphQLRequest
{
Query = "{}",
Variables = new WithNullableStruct
{
NullableStruct = DateTime.Now
}
});

action.Should().NotThrow();
}
}
}

0 comments on commit a1a3978

Please sign in to comment.