Skip to content

Commit

Permalink
Ensure there is always at least 1 valid candidate (#16344)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolajlauridsen authored May 22, 2024
1 parent 0f3160f commit eb6bb99
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Umbraco.Web.Website/Routing/EagerMatcherPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
}

// If there's only one candidate, we don't need to do anything.
if (candidates.Count < 2)
var candidateCount = candidates.Count;
if (candidateCount < 2)
{
return;
}
Expand All @@ -85,6 +86,14 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
RouteEndpoint? dynamicEndpoint = null;
for (var i = 0; i < candidates.Count; i++)
{
if (candidates.IsValidCandidate(i) is false)
{
// If the candidate is not valid we reduce the candidate count so we can later ensure that there is always
// at least 1 candidate.
candidateCount -= 1;
continue;
}

CandidateState candidate = candidates[i];

// If it's not a RouteEndpoint there's not much we can do to count it in the order.
Expand Down Expand Up @@ -123,7 +132,7 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates)
// Invalidate the dynamic route if another route has a lower order.
// This means that if you register your static route after the dynamic route, the dynamic route will take precedence
// This more closely resembles the existing behaviour.
if (dynamicEndpoint is not null && dynamicId is not null && dynamicEndpoint.Order > lowestOrder)
if (dynamicEndpoint is not null && dynamicId is not null && dynamicEndpoint.Order > lowestOrder && candidateCount > 1)
{
candidates.SetValidity(dynamicId.Value, false);
}
Expand Down

0 comments on commit eb6bb99

Please sign in to comment.