Skip to content

Commit 46073f0

Browse files
authored
Merge pull request #261 from graphql-dotnet/230-argumentoutofrange-exception-with-systemtextjson
Migitate ArgumentOutOfRangeException with SystemTextJson
2 parents 812a740 + 73ce1f2 commit 46073f0

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,13 @@ public override bool CanConvert(Type typeToConvert)
3232
{
3333
var constructor = constructors[0];
3434
var parameters = constructor.GetParameters();
35-
var hasParameters = parameters.Length > 0;
36-
if (hasParameters)
35+
36+
if (parameters.Length > 0)
3737
{
3838
var properties = typeToConvert.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
39-
result = true;
40-
foreach (var parameter in parameters)
41-
{
42-
var hasMatchingProperty = properties.Any(p =>
43-
NameOfPropertyAndParameter.Matches(p.Name, parameter.Name, typeToConvert.IsAnonymous()));
44-
if (!hasMatchingProperty)
45-
{
46-
result = false;
47-
break;
48-
}
49-
}
39+
result = parameters
40+
.Select(parameter => properties.Any(p => NameOfPropertyAndParameter.Matches(p.Name, parameter.Name, typeToConvert.IsAnonymous())))
41+
.All(hasMatchingProperty => hasMatchingProperty);
5042
}
5143
else
5244
{
@@ -69,8 +61,8 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
6961
break;
7062
}
7163

72-
var jsonPropName = reader.GetString();
73-
var normalizedPropName = ConvertAndNormalizeName(jsonPropName, options);
64+
string jsonPropName = reader.GetString();
65+
string normalizedPropName = ConvertAndNormalizeName(jsonPropName, options);
7466
if (!namedPropertiesMapping.TryGetValue(normalizedPropName, out var obProp))
7567
{
7668
reader.Read();
@@ -86,7 +78,7 @@ public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS
8678
var ctor = typeToConvert.GetConstructors(BindingFlags.Public | BindingFlags.Instance).First();
8779
var parameters = ctor.GetParameters();
8880
var parameterValues = new object[parameters.Length];
89-
for (var index = 0; index < parameters.Length; index++)
81+
for (int index = 0; index < parameters.Length; index++)
9082
{
9183
var parameterInfo = parameters[index];
9284
var value = valueOfProperty.First(prop =>
@@ -142,7 +134,7 @@ private static IReadOnlyDictionary<string, PropertyInfo> GetNamedProperties(Json
142134
name = options.PropertyNamingPolicy?.ConvertName(property.Name) ?? property.Name;
143135
}
144136

145-
var normalizedName = NormalizeName(name, options);
137+
string normalizedName = NormalizeName(name, options);
146138
result.Add(normalizedName, property);
147139
}
148140

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

152144
private static string ConvertAndNormalizeName(string name, JsonSerializerOptions options)
153145
{
154-
var convertedName = options.PropertyNamingPolicy?.ConvertName(name) ?? name;
155-
return options.PropertyNameCaseInsensitive ? convertedName.ToLowerInvariant() : convertedName;
146+
string convertedName = options.PropertyNamingPolicy?.ConvertName(name) ?? name;
147+
return NormalizeName(convertedName, options);
156148
}
157149

158150
private static string NormalizeName(string name, JsonSerializerOptions options) => options.PropertyNameCaseInsensitive ? name.ToLowerInvariant() : name;
@@ -162,12 +154,12 @@ internal static class NameOfPropertyAndParameter
162154
{
163155
public static bool Matches(string propertyName, string parameterName, bool anonymousType)
164156
{
165-
if (propertyName is null && parameterName is null)
157+
if (string.IsNullOrEmpty(propertyName))
166158
{
167-
return true;
159+
return string.IsNullOrEmpty(parameterName);
168160
}
169161

170-
if (propertyName is null || parameterName is null)
162+
if (string.IsNullOrEmpty(parameterName))
171163
{
172164
return false;
173165
}
@@ -176,12 +168,10 @@ public static bool Matches(string propertyName, string parameterName, bool anony
176168
{
177169
return propertyName.Equals(parameterName, StringComparison.Ordinal);
178170
}
179-
else
180-
{
181-
var xRight = propertyName.AsSpan(1);
182-
var yRight = parameterName.AsSpan(1);
183-
return char.ToLowerInvariant(propertyName[0]).CompareTo(parameterName[0]) == 0 && xRight.Equals(yRight, StringComparison.Ordinal);
184-
}
171+
172+
var xRight = propertyName.AsSpan(1);
173+
var yRight = parameterName.AsSpan(1);
174+
return char.ToLowerInvariant(propertyName[0]).CompareTo(parameterName[0]) == 0 && xRight.Equals(yRight, StringComparison.Ordinal);
185175
}
186176
}
187177

tests/GraphQL.Client.Serializer.Tests/TestData/DeserializeResponseTestData.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ public IEnumerator<object[]> GetEnumerator()
9999
}
100100
})
101101
};
102+
103+
// add test for github issue #230 : https://github.com/graphql-dotnet/graphql-client/issues/230
104+
yield return new object[] {
105+
"{\"data\":{\"getMyModelType\":{\"id\":\"foo\",\"title\":\"The best Foo movie!\"}}}",
106+
new GraphQLResponse<GetMyModelTypeResponse> {
107+
Data = new GetMyModelTypeResponse
108+
{
109+
getMyModelType = new Movie
110+
{
111+
id = "foo",
112+
title = "The best Foo movie!"
113+
}
114+
},
115+
}
116+
};
102117
}
103118

104119
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
@@ -112,4 +127,16 @@ public class Friend
112127
public string Id { get; set; }
113128
public string? Name { get; set; }
114129
}
130+
131+
public class GetMyModelTypeResponse
132+
{
133+
//--- Properties ---
134+
public Movie getMyModelType { get; set; }
135+
}
136+
public class Movie
137+
{
138+
//--- Properties ---
139+
public string id { get; set; }
140+
public string title { get; set; }
141+
}
115142
}

0 commit comments

Comments
 (0)