From b70f24594dfa907dcc10251976fac7d28ad3fa2e Mon Sep 17 00:00:00 2001 From: Marc Gravell Date: Mon, 14 Aug 2023 14:42:04 +0100 Subject: [PATCH] reserve known DbCommand properties --- .../AnalyzerReleases.Unshipped.md | 1 + .../DapperInterceptorGenerator.cs | 29 ++++++++++++++++++- .../CodeAnalysis/Diagnostics.cs | 4 ++- .../CommandProperties.output.netfx.txt | 4 +-- .../Interceptors/CommandProperties.output.txt | 4 +-- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Dapper.AOT.Analyzers/AnalyzerReleases.Unshipped.md b/src/Dapper.AOT.Analyzers/AnalyzerReleases.Unshipped.md index 509a36dd..e318d0f8 100644 --- a/src/Dapper.AOT.Analyzers/AnalyzerReleases.Unshipped.md +++ b/src/Dapper.AOT.Analyzers/AnalyzerReleases.Unshipped.md @@ -39,6 +39,7 @@ DAP030 | Library | Error | Diagnostics DAP031 | Library | Error | Diagnostics DAP032 | Library | Error | Diagnostics DAP033 | Library | Warning | Diagnostics +DAP034 | Library | Warning | Diagnostics DAP100 | Library | Error | Diagnostics DAP101 | Library | Error | Diagnostics DAP102 | Library | Error | Diagnostics diff --git a/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs b/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs index b8723cba..cb57929a 100644 --- a/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs +++ b/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs @@ -1203,7 +1203,12 @@ private static void WriteCommandProperties(SourceProductionContext ctx, CodeWrit bool firstForType = true; // defer starting the if-test in case all invalid foreach (var prop in grp) { - if (!HasPublicSettableInstanceMember(type, prop.Name)) + if (IsReserved(prop.Name)) + { + ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.CommandPropertyReserved, prop.Location, prop.Name)); + continue; + } + else if (!HasPublicSettableInstanceMember(type, prop.Name)) { ctx.ReportDiagnostic(Diagnostic.Create(Diagnostics.CommandPropertyNotFound, prop.Location, type.Name, prop.Name)); continue; @@ -1259,6 +1264,28 @@ static bool HasPublicSettableInstanceMember(ITypeSymbol type, string name) } return false; } + + static bool IsReserved(string name) + { + switch (name) + { + // pretty much everything on DbCommand + case nameof(DbCommand.CommandText): + case nameof(DbCommand.CommandTimeout): + case nameof(DbCommand.CommandType): + case nameof(DbCommand.Connection): + case nameof(DbCommand.Parameters): + case nameof(DbCommand.Site): + case nameof(DbCommand.Transaction): + case nameof(DbCommand.UpdatedRowSource): + // see SpecialCommandFlags + case "InitialLONGFetchSize": + case "BindByName": + return true; + default: + return false; + } + } } private static void WriteRowFactory(CodeWriter sb, ITypeSymbol type, int index) diff --git a/src/Dapper.AOT.Analyzers/CodeAnalysis/Diagnostics.cs b/src/Dapper.AOT.Analyzers/CodeAnalysis/Diagnostics.cs index ecd2a793..224ebcb4 100644 --- a/src/Dapper.AOT.Analyzers/CodeAnalysis/Diagnostics.cs +++ b/src/Dapper.AOT.Analyzers/CodeAnalysis/Diagnostics.cs @@ -75,7 +75,9 @@ internal static readonly DiagnosticDescriptor MemberRowCountHintDuplicated = new("DAP032", "Member-level row-count hint duplicated", "Only a single member should be marked [EstimatedRowCount]", Category.Library, DiagnosticSeverity.Error, true), CommandPropertyNotFound = new("DAP033", "Command property not found", - "Command property {0}.{1} was not found or was not valid", Category.Library, DiagnosticSeverity.Warning, true), + "Command property {0}.{1} was not found or was not valid; attribute will be ignored", Category.Library, DiagnosticSeverity.Warning, true), + CommandPropertyReserved = new("DAP034", "Command property reserved", + "Command property {1} is reserved for internal usage; attribute will be ignored", Category.Library, DiagnosticSeverity.Warning, true), // TypeAccessor TypeAccessorCollectionTypeNotAllowed = new("DAP100", "TypeAccessors does not allow collection types", diff --git a/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.netfx.txt b/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.netfx.txt index 0a4efa1f..0a7500d5 100644 --- a/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.netfx.txt +++ b/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.netfx.txt @@ -4,7 +4,7 @@ Hidden DAP000 L1 C1 Dapper.AOT handled 6 of 6 enabled call-sites using 5 interceptors, 7 commands and 1 readers Warning DAP033 Interceptors/CommandProperties.input.cs L12 C17 -Command property SqlCommand.InvalidName was not found or was not valid +Command property SqlCommand.InvalidName was not found or was not valid; attribute will be ignored Warning DAP033 Interceptors/CommandProperties.input.cs L12 C17 -Command property SqlCommand.InvalidName was not found or was not valid +Command property SqlCommand.InvalidName was not found or was not valid; attribute will be ignored diff --git a/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.txt b/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.txt index 0a4efa1f..0a7500d5 100644 --- a/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.txt +++ b/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.txt @@ -4,7 +4,7 @@ Hidden DAP000 L1 C1 Dapper.AOT handled 6 of 6 enabled call-sites using 5 interceptors, 7 commands and 1 readers Warning DAP033 Interceptors/CommandProperties.input.cs L12 C17 -Command property SqlCommand.InvalidName was not found or was not valid +Command property SqlCommand.InvalidName was not found or was not valid; attribute will be ignored Warning DAP033 Interceptors/CommandProperties.input.cs L12 C17 -Command property SqlCommand.InvalidName was not found or was not valid +Command property SqlCommand.InvalidName was not found or was not valid; attribute will be ignored