From 17e8613b81e25ccf093e21f94e4bad406ce98cee Mon Sep 17 00:00:00 2001 From: Koen Bekkenutte <2912652+kbekkenutte@users.noreply.github.com> Date: Tue, 4 Jan 2022 03:10:58 +0800 Subject: [PATCH] Fixed a regression where relational member calls would fail to generate --- .../ExpressionSyntaxRewriter.cs | 10 ++++-- ...ratorTests.RelationalProperty.verified.txt | 18 +++++++++++ .../ProjectionExpressionGeneratorTests.cs | 31 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.RelationalProperty.verified.txt diff --git a/src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs b/src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs index 5d79a38..97bd789 100644 --- a/src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs +++ b/src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs @@ -127,7 +127,7 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, SemanticModel if (symbolInfo.Symbol is IMethodSymbol methodSymbol && methodSymbol.IsExtensionMethod) { } - else if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field /*&& SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol)*/) + else if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field) { bool rewrite = true; @@ -138,9 +138,13 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, SemanticModel { rewrite = false; } - if (targetSymbolInfo.Symbol?.ContainingType is not null && !_context.Compilation.HasImplicitConversion(targetSymbolInfo.Symbol.ContainingType, _targetTypeSymbol)) + if (targetSymbolInfo.Symbol?.ContainingType is not null) { - rewrite = false; + if (!_context.Compilation.HasImplicitConversion(targetSymbolInfo.Symbol.ContainingType, _targetTypeSymbol) || + !SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol)) + { + rewrite = false; + } } } else if (node.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression)) diff --git a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.RelationalProperty.verified.txt b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.RelationalProperty.verified.txt new file mode 100644 index 0000000..693e8d2 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.RelationalProperty.verified.txt @@ -0,0 +1,18 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using EntityFrameworkCore.Projectables; +using Foos; + +namespace EntityFrameworkCore.Projectables.Generated +#nullable disable +{ + public static class Foos_Bar_FooId + { + public static System.Linq.Expressions.Expression> Expression() + { + return (global::Foos.Bar @this) => + @this.Foo.Id; + } + } +} \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs index 9336438..eea6d27 100644 --- a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs +++ b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs @@ -988,6 +988,37 @@ public class Bar : Foo { return Verifier.Verify(result.GeneratedTrees[0].ToString()); } + [Fact] + public Task RelationalProperty() + { + var compilation = CreateCompilation(@" +using System; +using System.Linq; +using System.Collections.Generic; +using EntityFrameworkCore.Projectables; + +namespace Foos { + public class Foo { + public int Id { get; set; } + } + + public class Bar { + public Foo Foo { get; set; } + + [Projectable] + public int FooId => Foo.Id; + } +} +"); + + var result = RunGenerator(compilation); + + Assert.Empty(result.Diagnostics); + Assert.Single(result.GeneratedTrees); + + return Verifier.Verify(result.GeneratedTrees[0].ToString()); + } + #region Helpers Compilation CreateCompilation(string source, bool expectedToCompile = true)