Do not throw if RouteComparison compares two identical references

Fixes https://github.com/aspnet/AspNetCore/issues/13313
This commit is contained in:
Pranav K 2019-08-21 13:18:41 -07:00
parent 733218c652
commit 7bb56a8dba
No known key found for this signature in database
GPG Key ID: 1963DA6D96C3057A
4 changed files with 45 additions and 0 deletions

View File

@ -3,9 +3,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Microsoft.AspNetCore.Components.Routing
{
[DebuggerDisplay("Handler = {Handler}, Template = {Template}")]
internal class RouteEntry
{
public RouteEntry(RouteTemplate template, Type handler, string[] unusedRouteParameterNames)

View File

@ -114,6 +114,11 @@ namespace Microsoft.AspNetCore.Components
/// </summary>
internal static int RouteComparison(RouteEntry x, RouteEntry y)
{
if (ReferenceEquals(x, y))
{
return 0;
}
var xTemplate = x.Template;
var yTemplate = y.Template;
if (xTemplate.Segments.Length != y.Template.Segments.Length)

View File

@ -2,8 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
namespace Microsoft.AspNetCore.Components.Routing
{
[DebuggerDisplay("{TemplateText}")]
internal class RouteTemplate
{
public RouteTemplate(string templateText, TemplateSegment[] segments)

View File

@ -366,6 +366,41 @@ namespace Microsoft.AspNetCore.Components.Test.Routing
Assert.Equal("a/brilliant", routeTable.Routes[0].Template.TemplateText);
}
[Fact]
public void DoesNotThrowIfStableSortComparesRouteWithItself()
{
// Test for https://github.com/aspnet/AspNetCore/issues/13313
// Arrange & Act
var builder = new TestRouteTableBuilder();
builder.AddRoute("r16");
builder.AddRoute("r05");
builder.AddRoute("r09");
builder.AddRoute("r00");
builder.AddRoute("r13");
builder.AddRoute("r02");
builder.AddRoute("r03");
builder.AddRoute("r10");
builder.AddRoute("r15");
builder.AddRoute("r14");
builder.AddRoute("r12");
builder.AddRoute("r07");
builder.AddRoute("r11");
builder.AddRoute("r08");
builder.AddRoute("r06");
builder.AddRoute("r04");
builder.AddRoute("r01");
var routeTable = builder.Build();
// Act
Assert.Equal(17, routeTable.Routes.Length);
for (var i = 0; i < 17; i++)
{
var templateText = "r" + i.ToString().PadLeft(2, '0');
Assert.Equal(templateText, routeTable.Routes[i].Template.TemplateText);
}
}
[Theory]
[InlineData("/literal", "/Literal/")]
[InlineData("/{parameter}", "/{parameter}/")]