Skip to content

Commit b2104ef

Browse files
authored
Fix ArgumentException in conditional expressions (#303)
Fix ArgumentException in conditional expressions Before this commit, the following exception might be thrown: > System.ArgumentException: Argument types do not match > at System.Linq.Expressions.Expression.Condition(Expression test, Expression ifTrue, Expression ifFalse, Type type)
1 parent 63b4f6c commit b2104ef

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Octokit.GraphQL.Core.UnitTests/QueryBuilderTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,26 @@ public void Repository_PullRequest_CheckRun_Aliased_Id()
12641264
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
12651265
}
12661266

1267+
[Fact]
1268+
public void Repository_Parent_ConditionalExpression()
1269+
{
1270+
var expected = @"query {
1271+
repository(owner: ""foo"", name: ""bar"") {
1272+
parent {
1273+
name
1274+
}
1275+
}
1276+
}";
1277+
1278+
var expression = new Query()
1279+
.Repository("foo", "bar")
1280+
.Select(repository => repository.Parent == null ? null : repository.Parent.Name);
1281+
1282+
var query = expression.Compile();
1283+
1284+
Assert.Equal(expected, query.ToString(2), ignoreLineEndingDifferences: true);
1285+
}
1286+
12671287
class TestModelObject
12681288
{
12691289
public string StringField1;

Octokit.GraphQL.Core/Core/Builders/QueryBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Linq;
@@ -204,7 +204,7 @@ protected override Expression VisitConditional(ConditionalExpression node)
204204
}
205205
else if (!falseNull)
206206
{
207-
ifTrue = Expression.Constant(null, ifFalse.Type);
207+
ifTrue = Expression.Constant(null, node.Type);
208208
}
209209

210210
if (!falseNull)
@@ -213,14 +213,14 @@ protected override Expression VisitConditional(ConditionalExpression node)
213213
}
214214
else if (!trueNull)
215215
{
216-
ifFalse = Expression.Constant(null, ifTrue.Type);
216+
ifFalse = Expression.Constant(null, node.Type);
217217
}
218218

219219
return Expression.Condition(
220220
test,
221221
ifTrue,
222222
ifFalse,
223-
!IsNullConstant(ifTrue) ? ifTrue.Type : ifFalse.Type);
223+
node.Type);
224224
}
225225

226226
protected override Expression VisitExtension(Expression node)

0 commit comments

Comments
 (0)