-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Greater than 1 FromBody analyzer. #46494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mitchdenny
merged 7 commits into
dotnet:main
from
mitchdenny:greater-than-1-frombody-attribute-analyzer
Feb 21, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50b5904
Greater than 1 FromBody analyzer.
mitchdenny 6ac5e09
Remove routeUsage parameter.
mitchdenny 6544d6b
Update src/Framework/AspNetCoreAnalyzers/src/Analyzers/DiagnosticDesc…
mitchdenny 8c16298
Update src/Framework/AspNetCoreAnalyzers/src/Analyzers/DiagnosticDesc…
mitchdenny d07a6dd
PR feedback
mitchdenny fc414d7
Add AsParameters support.
mitchdenny 36a76cf
Update message to include AsParameters scenario.
mitchdenny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteHandlers/AtMostOneFromBodyAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Analyzers.RouteEmbeddedLanguage.Infrastructure; | ||
using Microsoft.AspNetCore.App.Analyzers.Infrastructure; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.RouteHandlers; | ||
|
||
using WellKnownType = WellKnownTypeData.WellKnownType; | ||
|
||
public partial class RouteHandlerAnalyzer : DiagnosticAnalyzer | ||
{ | ||
private static void AtMostOneFromBodyAttribute( | ||
in OperationAnalysisContext context, | ||
WellKnownTypes wellKnownTypes, | ||
IMethodSymbol methodSymbol) | ||
{ | ||
var fromBodyMetadataInterfaceType = wellKnownTypes.Get(WellKnownType.Microsoft_AspNetCore_Http_Metadata_IFromBodyMetadata); | ||
var asParametersAttributeType = wellKnownTypes.Get(WellKnownType.Microsoft_AspNetCore_Http_AsParametersAttribute); | ||
|
||
var asParametersDecoratedParameters = methodSymbol.Parameters.Where(p => p.HasAttribute(asParametersAttributeType)); | ||
|
||
foreach (var asParameterDecoratedParameter in asParametersDecoratedParameters) | ||
{ | ||
var fromBodyMetadataInterfaceMembers = asParameterDecoratedParameter.Type.GetMembers().Where( | ||
m => m.HasAttributeImplementingInterface(fromBodyMetadataInterfaceType) | ||
); | ||
|
||
if (fromBodyMetadataInterfaceMembers.Count() >= 2) | ||
{ | ||
ReportDiagnostics(context, fromBodyMetadataInterfaceMembers); | ||
} | ||
} | ||
|
||
var fromBodyMetadataInterfaceParameters = methodSymbol.Parameters.Where(p => p.HasAttributeImplementingInterface(fromBodyMetadataInterfaceType)); | ||
|
||
if (fromBodyMetadataInterfaceParameters.Count() >= 2) | ||
{ | ||
ReportDiagnostics(context, fromBodyMetadataInterfaceParameters); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wouldn't fail if there's a single |
||
} | ||
|
||
static void ReportDiagnostics(OperationAnalysisContext context, IEnumerable<ISymbol> symbols) | ||
{ | ||
foreach (var symbol in symbols) | ||
{ | ||
if (symbol.DeclaringSyntaxReferences.Length > 0) | ||
{ | ||
var syntax = symbol.DeclaringSyntaxReferences[0].GetSyntax(context.CancellationToken); | ||
var location = syntax.GetLocation(); | ||
context.ReportDiagnostic(Diagnostic.Create( | ||
DiagnosticDescriptors.AtMostOneFromBodyAttribute, | ||
location | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
src/Framework/AspNetCoreAnalyzers/test/RouteHandlers/AtMostOneFromBodyAttributeTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Security.Policy; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using VerifyCS = Microsoft.AspNetCore.Analyzers.Verifiers.CSharpAnalyzerVerifier<Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer>; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.RouteHandlers; | ||
|
||
public partial class AtMostOneFromBodyAttributeTest | ||
{ | ||
private TestDiagnosticAnalyzerRunner Runner { get; } = new(new RouteHandlerAnalyzer()); | ||
|
||
[Fact] | ||
public async Task Handler_With_No_FromBody_Attributes_Works() | ||
{ | ||
// Arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Builder; | ||
var webApp = WebApplication.Create(); | ||
webApp.MapPost(""/products/{productId}"", (string productId, Product product) => {}); | ||
|
||
public class Product | ||
{ | ||
} | ||
"; | ||
|
||
// Act | ||
await VerifyCS.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Fact] | ||
public async Task Handler_With_One_FromBody_Attributes_Works() | ||
{ | ||
// Arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Builder; | ||
var webApp = WebApplication.Create(); | ||
webApp.MapPost(""/products/{productId}"", (string productId, [FromBody]Product product) => {}); | ||
|
||
public class Product | ||
{ | ||
} | ||
"; | ||
|
||
// Act | ||
await VerifyCS.VerifyAnalyzerAsync(source); | ||
} | ||
|
||
[Fact] | ||
public async Task Handler_With_Two_FromBody_Attributes_Fails() | ||
mitchdenny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
// Arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Builder; | ||
var webApp = WebApplication.Create(); | ||
webApp.MapPost(""/products/{productId}"", (string productId, {|#0:[FromBody]Product product1|}, {|#1:[FromBody]Product product2|}) => {}); | ||
|
||
public class Product | ||
{ | ||
} | ||
"; | ||
|
||
var expectedDiagnostic1 = new DiagnosticResult(DiagnosticDescriptors.AtMostOneFromBodyAttribute).WithLocation(0); | ||
var expectedDiagnostic2 = new DiagnosticResult(DiagnosticDescriptors.AtMostOneFromBodyAttribute).WithLocation(1); | ||
|
||
// Act | ||
await VerifyCS.VerifyAnalyzerAsync( | ||
source, | ||
expectedDiagnostic1, | ||
expectedDiagnostic2 | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task MethodGroup_Handler_With_Two_FromBody_Attributes_Fails() | ||
{ | ||
// Arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Builder; | ||
var webApp = WebApplication.Create(); | ||
webApp.MapPost(""/products/{productId}"", MyHandlers.ProcessRequest); | ||
|
||
public static class MyHandlers | ||
{ | ||
public static void ProcessRequest(string productId, {|#0:[FromBody]Product product1|}, {|#1:[FromBody]Product product2|}) | ||
{ | ||
} | ||
} | ||
|
||
public class Product | ||
{ | ||
} | ||
"; | ||
|
||
var expectedDiagnostic1 = new DiagnosticResult(DiagnosticDescriptors.AtMostOneFromBodyAttribute).WithLocation(0); | ||
var expectedDiagnostic2 = new DiagnosticResult(DiagnosticDescriptors.AtMostOneFromBodyAttribute).WithLocation(1); | ||
|
||
// Act | ||
await VerifyCS.VerifyAnalyzerAsync( | ||
source, | ||
expectedDiagnostic1, | ||
expectedDiagnostic2 | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Handler_Handler_With_AsParameters_Argument_With_TwoFromBody_Attributes_Fails() | ||
{ | ||
// Arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Builder; | ||
var webApp = WebApplication.Create(); | ||
webApp.MapPost(""/products/{productId}"", ([AsParameters]GetProductRequest request) => {}); | ||
|
||
public class GetProductRequest | ||
{ | ||
{|#0:[FromBody] | ||
public Product Product1 { get; set; }|} | ||
|
||
{|#1:[FromBody] | ||
public Product Product2 { get; set; }|} | ||
} | ||
|
||
public class Product | ||
{ | ||
} | ||
"; | ||
|
||
var expectedDiagnostic1 = new DiagnosticResult(DiagnosticDescriptors.AtMostOneFromBodyAttribute).WithLocation(0); | ||
var expectedDiagnostic2 = new DiagnosticResult(DiagnosticDescriptors.AtMostOneFromBodyAttribute).WithLocation(1); | ||
|
||
// Act | ||
await VerifyCS.VerifyAnalyzerAsync( | ||
source, | ||
expectedDiagnostic1, | ||
expectedDiagnostic2 | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task Handler_Handler_With_AsParameters_Argument_With_OneFromBody_Attributes_Works() | ||
{ | ||
// Arrange | ||
var source = @" | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Builder; | ||
var webApp = WebApplication.Create(); | ||
webApp.MapPost(""/products/{productId}"", ([AsParameters]GetProductRequest request) => {}); | ||
|
||
public class GetProductRequest | ||
{ | ||
{|#0:[FromBody] | ||
public Product Product1 { get; set; }|} | ||
|
||
public Product Product2 { get; set; } | ||
} | ||
|
||
public class Product | ||
{ | ||
} | ||
"; | ||
|
||
// Act | ||
await VerifyCS.VerifyAnalyzerAsync(source); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.