-
Notifications
You must be signed in to change notification settings - Fork 717
Description
Symptoms
When API versioning is applied using conventions, controller action methods are matched using expressions. The defined conventions are occasionally not matched to the corresponding controller action during runtime execution. This behavior has also manifested as flaky tests in the test coverage. Thus far, the behavior is only exhibited in ASP.NET Core.
services.AddApiVersioning( options =>
{
options.Conventions.Controller<ValuesController>()
.HasApiVersion( 1, 0 )
.HasApiVersion( 2, 0 )
.Action( c => c.GetV2() ).MapToApiVersion( 2, 0 )
.Action( c => c.GetV2( default( int ) ) ).MapToApiVersion( 2, 0 );
} );Figure 1: API versioning using conventions with a method expression
Analysis
API version conventions are matched to a controller action method by using the hash code of the MethodInfo resolved from the developer-defined method expression for the action. At runtime, ASP.NET Web API or Core will provide a controller action who is bound to a method. The hash code of this method is used to look up the conventions. Although the two methods are semantically equivalent, the hash code does not always match. Oddly, the comparison of the two MethodInfo instances using equality appears to always return the correct behavior, even though the hash codes may be different.