Skip to content

Commit

Permalink
Fix ArgumentException in conditional expressions (#303)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
0xced committed Aug 24, 2023
1 parent 63b4f6c commit b2104ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
20 changes: 20 additions & 0 deletions Octokit.GraphQL.Core.UnitTests/QueryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,26 @@ public void Repository_PullRequest_CheckRun_Aliased_Id()
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact]
public void Repository_Parent_ConditionalExpression()
{
var expected = @"query {
repository(owner: ""foo"", name: ""bar"") {
parent {
name
}
}
}";

var expression = new Query()
.Repository("foo", "bar")
.Select(repository => repository.Parent == null ? null : repository.Parent.Name);

var query = expression.Compile();

Assert.Equal(expected, query.ToString(2), ignoreLineEndingDifferences: true);
}

class TestModelObject
{
public string StringField1;
Expand Down
8 changes: 4 additions & 4 deletions Octokit.GraphQL.Core/Core/Builders/QueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
Expand Down Expand Up @@ -204,7 +204,7 @@ protected override Expression VisitConditional(ConditionalExpression node)
}
else if (!falseNull)
{
ifTrue = Expression.Constant(null, ifFalse.Type);
ifTrue = Expression.Constant(null, node.Type);
}

if (!falseNull)
Expand All @@ -213,14 +213,14 @@ protected override Expression VisitConditional(ConditionalExpression node)
}
else if (!trueNull)
{
ifFalse = Expression.Constant(null, ifTrue.Type);
ifFalse = Expression.Constant(null, node.Type);
}

return Expression.Condition(
test,
ifTrue,
ifFalse,
!IsNullConstant(ifTrue) ? ifTrue.Type : ifFalse.Type);
node.Type);
}

protected override Expression VisitExtension(Expression node)
Expand Down

0 comments on commit b2104ef

Please sign in to comment.