Skip to content

Commit

Permalink
Merge pull request #261 from graphql-dotnet/230-argumentoutofrange-ex…
Browse files Browse the repository at this point in the history
…ception-with-systemtextjson

Migitate ArgumentOutOfRangeException with SystemTextJson
  • Loading branch information
rose-a authored Aug 15, 2020
2 parents 812a740 + 73ce1f2 commit 46073f0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
46 changes: 18 additions & 28 deletions src/GraphQL.Client.Serializer.SystemTextJson/ImmutableConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,13 @@ public override bool CanConvert(Type typeToConvert)
{
var constructor = constructors[0];
var parameters = constructor.GetParameters();
var hasParameters = parameters.Length > 0;
if (hasParameters)

if (parameters.Length > 0)
{
var properties = typeToConvert.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
result = true;
foreach (var parameter in parameters)
{
var hasMatchingProperty = properties.Any(p =>
NameOfPropertyAndParameter.Matches(p.Name, parameter.Name, typeToConvert.IsAnonymous()));
if (!hasMatchingProperty)
{
result = false;
break;
}
}
result = parameters
.Select(parameter => properties.Any(p => NameOfPropertyAndParameter.Matches(p.Name, parameter.Name, typeToConvert.IsAnonymous())))
.All(hasMatchingProperty => hasMatchingProperty);
}
else
{
Expand All @@ -69,8 +61,8 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
break;
}

var jsonPropName = reader.GetString();
var normalizedPropName = ConvertAndNormalizeName(jsonPropName, options);
string jsonPropName = reader.GetString();
string normalizedPropName = ConvertAndNormalizeName(jsonPropName, options);
if (!namedPropertiesMapping.TryGetValue(normalizedPropName, out var obProp))
{
reader.Read();
Expand All @@ -86,7 +78,7 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
var ctor = typeToConvert.GetConstructors(BindingFlags.Public | BindingFlags.Instance).First();
var parameters = ctor.GetParameters();
var parameterValues = new object[parameters.Length];
for (var index = 0; index < parameters.Length; index++)
for (int index = 0; index < parameters.Length; index++)
{
var parameterInfo = parameters[index];
var value = valueOfProperty.First(prop =>
Expand Down Expand Up @@ -142,7 +134,7 @@ private static IReadOnlyDictionary<string, PropertyInfo> GetNamedProperties(Json
name = options.PropertyNamingPolicy?.ConvertName(property.Name) ?? property.Name;
}

var normalizedName = NormalizeName(name, options);
string normalizedName = NormalizeName(name, options);
result.Add(normalizedName, property);
}

Expand All @@ -151,8 +143,8 @@ private static IReadOnlyDictionary<string, PropertyInfo> GetNamedProperties(Json

private static string ConvertAndNormalizeName(string name, JsonSerializerOptions options)
{
var convertedName = options.PropertyNamingPolicy?.ConvertName(name) ?? name;
return options.PropertyNameCaseInsensitive ? convertedName.ToLowerInvariant() : convertedName;
string convertedName = options.PropertyNamingPolicy?.ConvertName(name) ?? name;
return NormalizeName(convertedName, options);
}

private static string NormalizeName(string name, JsonSerializerOptions options) => options.PropertyNameCaseInsensitive ? name.ToLowerInvariant() : name;
Expand All @@ -162,12 +154,12 @@ internal static class NameOfPropertyAndParameter
{
public static bool Matches(string propertyName, string parameterName, bool anonymousType)
{
if (propertyName is null && parameterName is null)
if (string.IsNullOrEmpty(propertyName))
{
return true;
return string.IsNullOrEmpty(parameterName);
}

if (propertyName is null || parameterName is null)
if (string.IsNullOrEmpty(parameterName))
{
return false;
}
Expand All @@ -176,12 +168,10 @@ public static bool Matches(string propertyName, string parameterName, bool anony
{
return propertyName.Equals(parameterName, StringComparison.Ordinal);
}
else
{
var xRight = propertyName.AsSpan(1);
var yRight = parameterName.AsSpan(1);
return char.ToLowerInvariant(propertyName[0]).CompareTo(parameterName[0]) == 0 && xRight.Equals(yRight, StringComparison.Ordinal);
}

var xRight = propertyName.AsSpan(1);
var yRight = parameterName.AsSpan(1);
return char.ToLowerInvariant(propertyName[0]).CompareTo(parameterName[0]) == 0 && xRight.Equals(yRight, StringComparison.Ordinal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public IEnumerator<object[]> GetEnumerator()
}
})
};

// add test for github issue #230 : https://github.com/graphql-dotnet/graphql-client/issues/230
yield return new object[] {
"{\"data\":{\"getMyModelType\":{\"id\":\"foo\",\"title\":\"The best Foo movie!\"}}}",
new GraphQLResponse<GetMyModelTypeResponse> {
Data = new GetMyModelTypeResponse
{
getMyModelType = new Movie
{
id = "foo",
title = "The best Foo movie!"
}
},
}
};
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand All @@ -112,4 +127,16 @@ public class Friend
public string Id { get; set; }
public string? Name { get; set; }
}

public class GetMyModelTypeResponse
{
//--- Properties ---
public Movie getMyModelType { get; set; }
}
public class Movie
{
//--- Properties ---
public string id { get; set; }
public string title { get; set; }
}
}

0 comments on commit 46073f0

Please sign in to comment.