Skip to content

Commit c00dd09

Browse files
authored
Merge pull request #101 from PhenX/fix-invalid-cast
Fix QualifiedNameSyntax visiting (fixes #100)
2 parents ac67d12 + dadb5f3 commit c00dd09

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, NullCondition
3434
.WithLeadingTrivia(node.GetLeadingTrivia())
3535
.WithTrailingTrivia(node.GetTrailingTrivia());
3636
}
37+
38+
public override SyntaxNode? VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
39+
{
40+
var expressionSyntax = (ExpressionSyntax?)Visit(node.Expression) ?? throw new ArgumentNullException("expression");
41+
42+
var syntaxNode = Visit(node.Name);
43+
44+
// Prevents invalid cast when visiting a QualifiedNameSyntax
45+
if (syntaxNode is QualifiedNameSyntax qst)
46+
{
47+
syntaxNode = qst.Right;
48+
}
49+
50+
return node.Update(expressionSyntax, VisitToken(node.OperatorToken), (SimpleNameSyntax)syntaxNode);
51+
}
3752

3853
public override SyntaxNode? VisitInvocationExpression(InvocationExpressionSyntax node)
3954
{
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT 7
2+
FROM [Entity] AS [e]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
5+
using Microsoft.EntityFrameworkCore;
6+
using VerifyXunit;
7+
using Xunit;
8+
9+
namespace EntityFrameworkCore.Projectables.FunctionalTests
10+
{
11+
public class Local
12+
{
13+
[Flags]
14+
public enum SampleEnum
15+
{
16+
One = 0b001,
17+
Two = 0b010,
18+
Four = 0b100,
19+
}
20+
}
21+
22+
[UsesVerify]
23+
public class NameSyntaxTests
24+
{
25+
public class Entity
26+
{
27+
public int Id { get; set; }
28+
29+
[Projectable]
30+
public Local.SampleEnum? Test => Local.SampleEnum.One | Local.SampleEnum.Two | Local.SampleEnum.Four;
31+
}
32+
33+
[Fact]
34+
public Task QualifiedNameSyntaxTest()
35+
{
36+
using var dbContext = new SampleDbContext<Entity>();
37+
38+
var query = dbContext.Set<Entity>()
39+
.Select(x => x.Test);
40+
41+
return Verifier.Verify(query.ToQueryString());
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)