diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorApplicationModelProvider.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorApplicationModelProvider.cs index e91b46c995..7bc6c6892d 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorApplicationModelProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorApplicationModelProvider.cs @@ -230,7 +230,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal // Internal for unit testing. internal BindingSource InferBindingSourceForParameter(ParameterModel parameter) { - if (ParameterExistsInAllRoutes(parameter.Action, parameter.ParameterName)) + if (ParameterExistsInAnyRoute(parameter.Action, parameter.ParameterName)) { return BindingSource.Path; } @@ -242,9 +242,8 @@ namespace Microsoft.AspNetCore.Mvc.Internal return bindingSource; } - private bool ParameterExistsInAllRoutes(ActionModel actionModel, string parameterName) + private bool ParameterExistsInAnyRoute(ActionModel actionModel, string parameterName) { - var parameterExistsInSomeRoute = false; foreach (var (route, _, _) in ActionAttributeRouteModel.GetAttributeRoutes(actionModel)) { if (route == null) @@ -253,16 +252,13 @@ namespace Microsoft.AspNetCore.Mvc.Internal } var parsedTemplate = TemplateParser.Parse(route.Template); - if (parsedTemplate.GetParameter(parameterName) == null) + if (parsedTemplate.GetParameter(parameterName) != null) { - return false; + return true; } - - // Ensure at least one route exists. - parameterExistsInSomeRoute = true; } - return parameterExistsInSomeRoute; + return false; } private bool IsComplexTypeParameter(ParameterModel parameter) diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/ApiBehaviorApplicationModelProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/ApiBehaviorApplicationModelProviderTest.cs index 935e0094e9..a9a01f5aba 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/ApiBehaviorApplicationModelProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/ApiBehaviorApplicationModelProviderTest.cs @@ -219,7 +219,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal } [Fact] - public void InferBindingSourceForParameter_ReturnsPath_IfParameterAppearsInAllRoutes() + public void InferBindingSourceForParameter_ReturnsPath_IfParameterAppearsInAnyRoutes_MulitpleRoutes() { // Arrange var actionName = nameof(ParameterBindingController.ParameterInMultipleRoutes); @@ -234,7 +234,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal } [Fact] - public void InferBindingSourceForParameter_DoesNotReturnPath_IfParameterDoesNotAppearInAllRoutes() + public void InferBindingSourceForParameter_ReturnsPath_IfParameterAppearsInAnyRoute() { // Arrange var actionName = nameof(ParameterBindingController.ParameterNotInAllRoutes); @@ -245,7 +245,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal var result = provider.InferBindingSourceForParameter(parameter); // Assert - Assert.Same(BindingSource.Query, result); + Assert.Same(BindingSource.Path, result); } [Fact] @@ -309,7 +309,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal } [Fact] - public void InferBindingSourceForParameter_DoesNotReturnPath_IfOneActionRouteOverridesControllerRoute() + public void InferBindingSourceForParameter_ReturnsPath_IfParameterPresentInNonOverriddenControllerRoute() { // Arrange var actionName = nameof(ParameterInController.MultipleRouteWithOverride); @@ -320,7 +320,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal var result = provider.InferBindingSourceForParameter(parameter); // Assert - Assert.Same(BindingSource.Query, result); + Assert.Same(BindingSource.Path, result); } [Fact]