Skip to content

Check for duplicate endpoint names on startup #36353

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 3 commits into from
Sep 15, 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
23 changes: 21 additions & 2 deletions src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,33 @@ public override Task MatchAsync(HttpContext httpContext)
private Matcher CreateMatcher(IReadOnlyList<Endpoint> endpoints)
{
var builder = _matcherBuilderFactory();
var seenEndpointNames = new Dictionary<string, string?>();
for (var i = 0; i < endpoints.Count; i++)
{
// By design we only look at RouteEndpoint here. It's possible to
// register other endpoint types, which are non-routable, and it's
// ok that we won't route to them.
if (endpoints[i] is RouteEndpoint endpoint && endpoint.Metadata.GetMetadata<ISuppressMatchingMetadata>()?.SuppressMatching != true)
if (endpoints[i] is RouteEndpoint endpoint)
{
builder.AddEndpoint(endpoint);
// Validate that endpoint names are unique.
var endpointName = endpoint.Metadata.GetMetadata<IEndpointNameMetadata>()?.EndpointName;
if (endpointName is not null)
{
if (seenEndpointNames.TryGetValue(endpointName, out var existingEndpoint))
{
throw new InvalidOperationException($"Duplicate endpoint name '{endpointName}' found on '{endpoint.DisplayName}' and '{existingEndpoint}'. Endpoint names must be globally unique.");
}

seenEndpointNames.Add(endpointName, endpoint.DisplayName ?? endpoint.RoutePattern.RawText);
}

// We check for duplicate endpoint names on all endpoints regardless
// of whether they suppress matching because endpoint names can be
// used in OpenAPI specifications as well.
if (endpoint.Metadata.GetMetadata<ISuppressMatchingMetadata>()?.SuppressMatching != true)
{
builder.AddEndpoint(endpoint);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,102 @@ public void Matcher_UnsuppressedEndpoint_IsUsed()
Assert.Same(endpoint, Assert.Single(inner.Endpoints));
}

[Fact]
public void Matcher_ThrowsOnDuplicateEndpoints()
{
// Arrange
var expectedError = "Duplicate endpoint name 'Foo' found on '/bar' and '/foo'. Endpoint names must be globally unique.";
var dataSource = new DynamicEndpointDataSource();
var lifetime = new DataSourceDependentMatcher.Lifetime();
dataSource.AddEndpoint(new RouteEndpoint(
TestConstants.EmptyRequestDelegate,
RoutePatternFactory.Parse("/foo"),
0,
new EndpointMetadataCollection(new EndpointNameMetadata("Foo")),
"/foo"
));
dataSource.AddEndpoint(new RouteEndpoint(
TestConstants.EmptyRequestDelegate,
RoutePatternFactory.Parse("/bar"),
0,
new EndpointMetadataCollection(new EndpointNameMetadata("Foo")),
"/bar"
));

// Assert
var exception = Assert.Throws<InvalidOperationException>(
() => new DataSourceDependentMatcher(dataSource, lifetime, TestMatcherBuilder.Create));
Assert.Equal(expectedError, exception.Message);
}

[Fact]
public void Matcher_ThrowsOnDuplicateEndpointsFromMultipleSources()
{
// Arrange
var expectedError = "Duplicate endpoint name 'Foo' found on '/foo2' and '/foo'. Endpoint names must be globally unique.";
var dataSource = new DynamicEndpointDataSource();
var lifetime = new DataSourceDependentMatcher.Lifetime();
dataSource.AddEndpoint(new RouteEndpoint(
TestConstants.EmptyRequestDelegate,
RoutePatternFactory.Parse("/foo"),
0,
new EndpointMetadataCollection(new EndpointNameMetadata("Foo")),
"/foo"
));
dataSource.AddEndpoint(new RouteEndpoint(
TestConstants.EmptyRequestDelegate,
RoutePatternFactory.Parse("/bar"),
0,
new EndpointMetadataCollection(new EndpointNameMetadata("Bar")),
"/bar"
));
var anotherDataSource = new DynamicEndpointDataSource();
anotherDataSource.AddEndpoint(new RouteEndpoint(
TestConstants.EmptyRequestDelegate,
RoutePatternFactory.Parse("/foo2"),
0,
new EndpointMetadataCollection(new EndpointNameMetadata("Foo")),
"/foo2"
));

var compositeDataSource = new CompositeEndpointDataSource(new[] { dataSource, anotherDataSource });

// Assert
var exception = Assert.Throws<InvalidOperationException>(
() => new DataSourceDependentMatcher(compositeDataSource, lifetime, TestMatcherBuilder.Create));
Assert.Equal(expectedError, exception.Message);
}

[Fact]
public void Matcher_ThrowsOnDuplicateEndpointAddedLater()
{
// Arrange
var expectedError = "Duplicate endpoint name 'Foo' found on '/bar' and '/foo'. Endpoint names must be globally unique.";
var dataSource = new DynamicEndpointDataSource();
var lifetime = new DataSourceDependentMatcher.Lifetime();
dataSource.AddEndpoint(new RouteEndpoint(
TestConstants.EmptyRequestDelegate,
RoutePatternFactory.Parse("/foo"),
0,
new EndpointMetadataCollection(new EndpointNameMetadata("Foo")),
"/foo"
));

// Act (should be all good since no duplicate has been added yet)
var matcher = new DataSourceDependentMatcher(dataSource, lifetime, TestMatcherBuilder.Create);

// Assert that rerunning initializer throws AggregateException
var exception = Assert.Throws<AggregateException>(
() => dataSource.AddEndpoint(new RouteEndpoint(
TestConstants.EmptyRequestDelegate,
RoutePatternFactory.Parse("/bar"),
0,
new EndpointMetadataCollection(new EndpointNameMetadata("Foo")),
"/bar"
)));
Assert.Equal(expectedError, exception.InnerException.Message);
}

private class TestMatcherBuilder : MatcherBuilder
{
public static Func<MatcherBuilder> Create = () => new TestMatcherBuilder();
Expand Down