Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ internal bool FindAddValidation(SyntaxNode syntaxNode, CancellationToken cancell
{
var node = (InvocationExpressionSyntax)context.Node;
var semanticModel = context.SemanticModel;
var symbol = semanticModel.GetSymbolInfo(node, cancellationToken).Symbol;
if (symbol is not IMethodSymbol methodSymbol
|| methodSymbol.ContainingType.Name != "ValidationServiceCollectionExtensions"
|| methodSymbol.ContainingAssembly.Name != "Microsoft.AspNetCore.Http.Abstractions")
{
return null;
}
return semanticModel.GetInterceptableLocation(node, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,62 @@ await VerifyEndpoint(compilation, "/complex-type", async (endpoint, serviceProvi
});
}

[Fact]
public async Task DoesNotEmitIfNotCorrectAddValidationCallExists()
{
// Arrange
var source = """
using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder();

builder.Services.AddValidation("example");

var app = builder.Build();

app.MapPost("/complex-type", (ComplexType complexType) => Results.Ok("Passed"));

app.Run();

public class ComplexType
{
[Range(10, 100)]
public int IntegerWithRange { get; set; } = 10;
}

public static class SomeExtensions
{
public static IServiceCollection AddValidation(this IServiceCollection services, string someString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this match the current signature of AddValidation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't quite figure out how to do that without the overload resolution favoring the AddValidation overload that is declared in the assembly. I suppose we could update the invocation code use a static method to call AddValidation instead of the extension method form.

{
// This is not the correct AddValidation method
return services;
}
}
""";
await Verify(source, out var compilation);
// Verify that we don't validate types if no AddValidation call exists
await VerifyEndpoint(compilation, "/complex-type", async (endpoint, serviceProvider) =>
{
var payload = """
{
"IntegerWithRange": 5
}
""";
var context = CreateHttpContextWithPayload(payload, serviceProvider);

await endpoint.RequestDelegate(context);

Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
});
}

[Fact]
public async Task DoesNotEmitForExemptTypes()
{
Expand Down
Loading