-
Notifications
You must be signed in to change notification settings - Fork 711
Pre-Aggregate API Version Models #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/Microsoft.AspNetCore.Mvc.Versioning/Versioning/ApiVersionCollator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
namespace Microsoft.AspNetCore.Mvc.Versioning | ||
{ | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Contracts; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// Represents an object that collates <see cref="ApiVersion">API versions</see> per <see cref="ActionDescriptor">action</see>. | ||
/// </summary> | ||
[CLSCompliant( false )] | ||
public class ApiVersionCollator : IActionDescriptorProvider | ||
{ | ||
/// <inheritdoc /> | ||
public int Order { get; protected set; } | ||
|
||
/// <inheritdoc /> | ||
public virtual void OnProvidersExecuted( ActionDescriptorProviderContext context ) | ||
{ | ||
foreach ( var actions in GroupActionsByController( context.Results ) ) | ||
{ | ||
var collatedModel = CollateModel( actions ); | ||
|
||
foreach ( var action in actions ) | ||
{ | ||
var model = action.GetProperty<ApiVersionModel>(); | ||
|
||
if ( model != null ) | ||
{ | ||
action.SetProperty( model.Aggregate( collatedModel ) ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual void OnProvidersExecuting( ActionDescriptorProviderContext context ) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Resolves and returns the logical controller name for the specified action. | ||
/// </summary> | ||
/// <param name="action">The <see cref="ActionDescriptor">action</see> to get the controller name from.</param> | ||
/// <returns>The logical name of the associated controller.</returns> | ||
/// <remarks> | ||
/// <para> | ||
/// The logical controller name is used to collate actions together and aggregate API versions. The | ||
/// default implementation uses the "controller" route parameter and falls back to the | ||
/// <see cref="ControllerActionDescriptor.ControllerName"/> property when available. | ||
/// </para> | ||
/// <para> | ||
/// The default implementation will also trim trailing numbers in the controller name by convention. For example, | ||
/// the type "Values2Controller" will have the controller name "Values2", which will be trimmed to just "Values". | ||
/// This behavior can be changed by using the <see cref="ControllerNameAttribute"/> or overriding the default | ||
/// implementation. | ||
/// </para> | ||
/// </remarks> | ||
protected virtual string GetControllerName( ActionDescriptor action ) | ||
{ | ||
Arg.NotNull( action, nameof( action ) ); | ||
|
||
if ( !action.RouteValues.TryGetValue( "controller", out var key ) ) | ||
{ | ||
if ( action is ControllerActionDescriptor controllerAction ) | ||
{ | ||
key = controllerAction.ControllerName; | ||
} | ||
} | ||
|
||
return TrimTrailingNumbers( key ); | ||
} | ||
|
||
IEnumerable<IEnumerable<ActionDescriptor>> GroupActionsByController( IEnumerable<ActionDescriptor> actions ) | ||
{ | ||
Contract.Requires( actions != null ); | ||
Contract.Ensures( Contract.Result<IEnumerable<IEnumerable<ActionDescriptor>>>() != null ); | ||
|
||
var groups = new Dictionary<string, List<ActionDescriptor>>( StringComparer.OrdinalIgnoreCase ); | ||
|
||
foreach ( var action in actions ) | ||
{ | ||
var key = GetControllerName( action ); | ||
|
||
if ( string.IsNullOrEmpty( key ) ) | ||
{ | ||
continue; | ||
} | ||
|
||
if ( !groups.TryGetValue( key, out var values ) ) | ||
{ | ||
groups.Add( key, values = new List<ActionDescriptor>() ); | ||
} | ||
|
||
values.Add( action ); | ||
} | ||
|
||
foreach ( var value in groups.Values ) | ||
{ | ||
yield return value; | ||
} | ||
} | ||
|
||
static string TrimTrailingNumbers( string name ) | ||
{ | ||
if ( string.IsNullOrEmpty( name ) ) | ||
{ | ||
return name; | ||
} | ||
|
||
var last = name.Length - 1; | ||
|
||
for ( var i = last; i >= 0; i-- ) | ||
{ | ||
if ( !char.IsNumber( name[i] ) ) | ||
{ | ||
if ( i < last ) | ||
{ | ||
return name.Substring( 0, i + 1 ); | ||
} | ||
|
||
return name; | ||
} | ||
} | ||
|
||
return name; | ||
} | ||
|
||
static ApiVersionModel CollateModel( IEnumerable<ActionDescriptor> actions ) => | ||
actions.Select( a => a.GetProperty<ApiVersionModel>() ).Where( m => m != null ).Aggregate(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
test/Microsoft.AspNetCore.Mvc.Versioning.Tests/Versioning/ApiVersionCollatorTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
namespace Microsoft.AspNetCore.Mvc.Versioning | ||
{ | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
public class ApiVersionCollatorTest | ||
{ | ||
[Theory] | ||
[MemberData( nameof( ActionDescriptorProviderContexts ) )] | ||
public void on_providers_executed_should_aggregate_api_version_models_by_controller( ActionDescriptorProviderContext context ) | ||
{ | ||
// arrange | ||
var collator = new ApiVersionCollator(); | ||
var expected = new[] { new ApiVersion( 1, 0 ), new ApiVersion( 2, 0 ), new ApiVersion( 3, 0 ) }; | ||
|
||
// act | ||
collator.OnProvidersExecuted( context ); | ||
|
||
// assert | ||
var actions = context.Results.Where( a => a.GetProperty<ApiVersionModel>() != null ); | ||
|
||
actions.All( a => a.GetProperty<ApiVersionModel>().SupportedApiVersions.SequenceEqual( expected ) ).Should().BeTrue(); | ||
} | ||
|
||
public static IEnumerable<object[]> ActionDescriptorProviderContexts | ||
{ | ||
get | ||
{ | ||
yield return new object[] { ActionsWithRouteValues }; | ||
yield return new object[] { ActionsByControllerName }; | ||
} | ||
} | ||
|
||
private static ActionDescriptorProviderContext ActionsWithRouteValues => | ||
new ActionDescriptorProviderContext() | ||
{ | ||
Results = | ||
{ | ||
new ActionDescriptor() | ||
{ | ||
RouteValues = new Dictionary<string, string>() | ||
{ | ||
["controller"] = "Values", | ||
["action"] = "Get", | ||
}, | ||
Properties = new Dictionary<object, object>() | ||
{ | ||
[typeof(ApiVersionModel)] = new ApiVersionModel( new ApiVersion( 1, 0 ) ), | ||
}, | ||
}, | ||
new ActionDescriptor() | ||
{ | ||
RouteValues = new Dictionary<string, string>() | ||
{ | ||
["page"] = "/Some/Page", | ||
}, | ||
}, | ||
new ActionDescriptor() | ||
{ | ||
RouteValues = new Dictionary<string, string>() | ||
{ | ||
["controller"] = "Values", | ||
["action"] = "Get", | ||
}, | ||
Properties = new Dictionary<object, object>() | ||
{ | ||
[typeof(ApiVersionModel)] = new ApiVersionModel( new ApiVersion( 2, 0 ) ), | ||
}, | ||
}, | ||
new ActionDescriptor() | ||
{ | ||
RouteValues = new Dictionary<string, string>() | ||
{ | ||
["controller"] = "Values", | ||
["action"] = "Get", | ||
}, | ||
Properties = new Dictionary<object, object>() | ||
{ | ||
[typeof(ApiVersionModel)] = new ApiVersionModel( new ApiVersion( 3, 0 ) ), | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
private static ActionDescriptorProviderContext ActionsByControllerName => | ||
new ActionDescriptorProviderContext() | ||
{ | ||
Results = | ||
{ | ||
new ControllerActionDescriptor() | ||
{ | ||
ControllerName = "Values", | ||
RouteValues = new Dictionary<string, string>() | ||
{ | ||
["action"] = "Get", | ||
}, | ||
Properties = new Dictionary<object, object>() | ||
{ | ||
[typeof(ApiVersionModel)] = new ApiVersionModel( new ApiVersion( 1, 0 ) ), | ||
}, | ||
}, | ||
new ActionDescriptor() | ||
{ | ||
RouteValues = new Dictionary<string, string>() | ||
{ | ||
["page"] = "/Some/Page", | ||
}, | ||
}, | ||
new ControllerActionDescriptor() | ||
{ | ||
ControllerName = "Values2", | ||
RouteValues = new Dictionary<string, string>() | ||
{ | ||
["action"] = "Get", | ||
}, | ||
Properties = new Dictionary<object, object>() | ||
{ | ||
[typeof(ApiVersionModel)] = new ApiVersionModel( new ApiVersion( 2, 0 ) ), | ||
}, | ||
}, | ||
new ControllerActionDescriptor() | ||
{ | ||
ControllerName = "Values3", | ||
RouteValues = new Dictionary<string, string>() | ||
{ | ||
["action"] = "Get", | ||
}, | ||
Properties = new Dictionary<object, object>() | ||
{ | ||
[typeof(ApiVersionModel)] = new ApiVersionModel( new ApiVersion( 3, 0 ) ), | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this guaranteed to be thread safe? I can see that Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor the Properties "property" is backed by a simple Dictionary but I'm unclear of the flow here and if there is a case where there can be multiple threads accessing this (which is the problem you are trying to solve with this PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To my knowledge - yes. All IActionDescriptorProvider instances run at startup and complete their work before taking requests. Mutating this state in the request pipeline is not guaranteed to be write-safe, which is the issue being observed and remedied.