Ensure there is always at least 1 valid candidate (#16344)

(cherry picked from commit eb6bb99eaf)
This commit is contained in:
Mole
2024-05-22 13:29:17 +02:00
committed by Bjarke Berg
parent fd2138c1fd
commit 5f082df3ab

View File

@@ -72,7 +72,8 @@ internal class EagerMatcherPolicy : MatcherPolicy, IEndpointSelectorPolicy
}
// 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;
}
@@ -85,6 +86,14 @@ internal class EagerMatcherPolicy : MatcherPolicy, IEndpointSelectorPolicy
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.
@@ -123,7 +132,7 @@ internal class EagerMatcherPolicy : MatcherPolicy, IEndpointSelectorPolicy
// 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);
}