Skip to content

Make sure route binding isn't case sensitive #35090

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
merged 1 commit into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext

if (parameterCustomAttributes.OfType<IFromRouteMetadata>().FirstOrDefault() is { } routeAttribute)
{
if (factoryContext.RouteParameters is { } routeParams && !routeParams.Contains(parameter.Name))
if (factoryContext.RouteParameters is { } routeParams && !routeParams.Contains(parameter.Name, StringComparer.OrdinalIgnoreCase))
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this using Linq? Can we turn this in to HashSet instead? or write a bespoke iterator? The Linq one will result in allocations: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Contains.cs#L33

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 think we should do that in another PR and did all the things. But I agree with you. There's too much LINQ usage here at startup

{
throw new InvalidOperationException($"{parameter.Name} is not a route paramter.");
}
Expand Down Expand Up @@ -255,7 +255,7 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
{
// We're in the fallback case and we have a parameter and route parameter match so don't fallback
// to query string in this case
if (factoryContext.RouteParameters is { } routeParams && routeParams.Contains(parameter.Name))
if (factoryContext.RouteParameters is { } routeParams && routeParams.Contains(parameter.Name, StringComparer.OrdinalIgnoreCase))
{
return BindParameterFromProperty(parameter, RouteValuesExpr, parameter.Name, factoryContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@ private RouteEndpointBuilder GetRouteEndpointBuilder(IEndpointRouteBuilder endpo
return Assert.IsType<RouteEndpointBuilder>(Assert.Single(GetBuilderEndpointDataSource(endpointRouteBuilder).EndpointBuilders));
}

public static object[][] MapMethods
{
get
{
void MapGet(IEndpointRouteBuilder routes, string template, Delegate action) =>
routes.MapGet(template, action);

void MapPost(IEndpointRouteBuilder routes, string template, Delegate action) =>
routes.MapPost(template, action);

void MapPut(IEndpointRouteBuilder routes, string template, Delegate action) =>
routes.MapPut(template, action);

void MapDelete(IEndpointRouteBuilder routes, string template, Delegate action) =>
routes.MapDelete(template, action);

return new object[][]
{
new object[] { (Action<IEndpointRouteBuilder, string, Delegate>)MapGet, "GET" },
new object[] { (Action<IEndpointRouteBuilder, string, Delegate>)MapPost, "POST" },
new object[] { (Action<IEndpointRouteBuilder, string, Delegate>)MapPut, "PUT" },
new object[] { (Action<IEndpointRouteBuilder, string, Delegate>)MapDelete, "DELETE" },
};
}
}

[Fact]
public void MapEndpoint_PrecedenceOfMetadata_BuilderMetadataReturned()
{
Expand Down Expand Up @@ -115,6 +141,82 @@ public async Task MapGetWithRouteParameter_BuildsEndpointWithRouteSpecificBindin
Assert.Null(httpContext.Items["input"]);
}

[Theory]
[MemberData(nameof(MapMethods))]
public async Task MapVerbWithExplicitRouteParameterIsCaseInsensitive(Action<IEndpointRouteBuilder, string, Delegate> map, string expectedMethod)
{
var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvdier()));

map(builder, "/{ID}", ([FromRoute] int? id, HttpContext httpContext) =>
{
if (id is not null)
{
httpContext.Items["input"] = id;
}
});

var dataSource = GetBuilderEndpointDataSource(builder);
// Trigger Endpoint build by calling getter.
var endpoint = Assert.Single(dataSource.Endpoints);

var methodMetadata = endpoint.Metadata.GetMetadata<IHttpMethodMetadata>();
Assert.NotNull(methodMetadata);
var method = Assert.Single(methodMetadata!.HttpMethods);
Assert.Equal(expectedMethod, method);

var routeEndpointBuilder = GetRouteEndpointBuilder(builder);
Assert.Equal($"/{{ID}} HTTP: {expectedMethod}", routeEndpointBuilder.DisplayName);
Assert.Equal($"/{{ID}}", routeEndpointBuilder.RoutePattern.RawText);

var httpContext = new DefaultHttpContext();

httpContext.Request.RouteValues["id"] = "13";

await endpoint.RequestDelegate!(httpContext);

Assert.Equal(13, httpContext.Items["input"]);
}

[Theory]
[MemberData(nameof(MapMethods))]
public async Task MapVerbWithRouteParameterDoesNotFallbackToQuery(Action<IEndpointRouteBuilder, string, Delegate> map, string expectedMethod)
{
var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvdier()));

map(builder, "/{ID}", (int? id, HttpContext httpContext) =>
{
if (id is not null)
{
httpContext.Items["input"] = id;
}
});

var dataSource = GetBuilderEndpointDataSource(builder);
// Trigger Endpoint build by calling getter.
var endpoint = Assert.Single(dataSource.Endpoints);

var methodMetadata = endpoint.Metadata.GetMetadata<IHttpMethodMetadata>();
Assert.NotNull(methodMetadata);
var method = Assert.Single(methodMetadata!.HttpMethods);
Assert.Equal(expectedMethod, method);

var routeEndpointBuilder = GetRouteEndpointBuilder(builder);
Assert.Equal($"/{{ID}} HTTP: {expectedMethod}", routeEndpointBuilder.DisplayName);
Assert.Equal($"/{{ID}}", routeEndpointBuilder.RoutePattern.RawText);

// Assert that we don't fallback to the query string
var httpContext = new DefaultHttpContext();

httpContext.Request.Query = new QueryCollection(new Dictionary<string, StringValues>
{
["id"] = "42"
});

await endpoint.RequestDelegate!(httpContext);

Assert.Null(httpContext.Items["input"]);
}

[Fact]
public void MapGetWithRouteParameter_ThrowsIfRouteParameterDoesNotExist()
{
Expand Down