Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly do you mean by this review? 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The devil is in the little things.

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();
}
}
}