Skip to content

Commit c86d2a4

Browse files
Correct regression from custom group names. Fixes #923
1 parent faf18a5 commit c86d2a4

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

src/AspNetCore/WebApi/src/Asp.Versioning.Mvc.ApiExplorer/VersionedApiDescriptionProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ public virtual void OnProvidersExecuted( ApiDescriptionProviderContext context )
156156
var groupResult = result.Clone();
157157
var metadata = action.GetApiVersionMetadata();
158158

159-
if ( string.IsNullOrEmpty( groupResult.GroupName ) || formatGroupName is null )
159+
if ( string.IsNullOrEmpty( groupResult.GroupName ) )
160160
{
161161
groupResult.GroupName = formattedVersion;
162162
}
163-
else
163+
else if ( formatGroupName is not null )
164164
{
165165
groupResult.GroupName = formatGroupName( groupResult.GroupName, formattedVersion );
166166
}

src/AspNetCore/WebApi/test/Asp.Versioning.Mvc.ApiExplorer.Tests/TestActionDescriptorCollectionProvider.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ internal sealed class TestActionDescriptorCollectionProvider : IActionDescriptor
99
{
1010
private readonly Lazy<ActionDescriptorCollection> collection = new( CreateActionDescriptors );
1111

12+
public TestActionDescriptorCollectionProvider() { }
13+
14+
public TestActionDescriptorCollectionProvider( ActionDescriptor action, params ActionDescriptor[] otherActions )
15+
{
16+
ActionDescriptor[] actions;
17+
18+
if ( otherActions.Length == 0 )
19+
{
20+
actions = new ActionDescriptor[] { action };
21+
}
22+
else
23+
{
24+
actions = new ActionDescriptor[otherActions.Length];
25+
actions[0] = action;
26+
Array.Copy( otherActions, 0, actions, 1, otherActions.Length );
27+
}
28+
29+
collection = new( () => new( actions, 0 ) );
30+
}
31+
1232
public ActionDescriptorCollection ActionDescriptors => collection.Value;
1333

1434
private static ActionDescriptorCollection CreateActionDescriptors()

src/AspNetCore/WebApi/test/Asp.Versioning.Mvc.ApiExplorer.Tests/VersionedApiDescriptionProviderTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Asp.Versioning.ApiExplorer;
44

5+
using Microsoft.AspNetCore.Mvc.Abstractions;
56
using Microsoft.AspNetCore.Mvc.ApiExplorer;
67
using Microsoft.AspNetCore.Mvc.ModelBinding;
78
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
@@ -92,6 +93,62 @@ public void versioned_api_explorer_should_apply_sunset_policy()
9293
.BeTrue();
9394
}
9495

96+
[Fact]
97+
public void versioned_api_explorer_should_preserve_group_name()
98+
{
99+
// arrange
100+
var metadata = new ApiVersionMetadata( ApiVersionModel.Empty, new ApiVersionModel( ApiVersion.Default ) );
101+
var descriptor = new ActionDescriptor() { EndpointMetadata = new[] { metadata } };
102+
var actionProvider = new TestActionDescriptorCollectionProvider( descriptor );
103+
var context = new ApiDescriptionProviderContext( actionProvider.ActionDescriptors.Items );
104+
var apiExplorer = new VersionedApiDescriptionProvider(
105+
Mock.Of<ISunsetPolicyManager>(),
106+
NewModelMetadataProvider(),
107+
Options.Create( new ApiExplorerOptions() ) );
108+
109+
context.Results.Add( new()
110+
{
111+
ActionDescriptor = descriptor,
112+
GroupName = "Test",
113+
} );
114+
115+
// act
116+
apiExplorer.OnProvidersExecuted( context );
117+
118+
// assert
119+
context.Results.Single().GroupName.Should().Be( "Test" );
120+
}
121+
122+
[Fact]
123+
public void versioned_api_explorer_should_use_custom_group_name()
124+
{
125+
// arrange
126+
var metadata = new ApiVersionMetadata( ApiVersionModel.Empty, new ApiVersionModel( ApiVersion.Default ) );
127+
var descriptor = new ActionDescriptor() { EndpointMetadata = new[] { metadata } };
128+
var actionProvider = new TestActionDescriptorCollectionProvider( descriptor );
129+
var context = new ApiDescriptionProviderContext( actionProvider.ActionDescriptors.Items );
130+
var options = new ApiExplorerOptions()
131+
{
132+
FormatGroupName = ( group, version ) => $"{group}-{version}",
133+
};
134+
var apiExplorer = new VersionedApiDescriptionProvider(
135+
Mock.Of<ISunsetPolicyManager>(),
136+
NewModelMetadataProvider(),
137+
Options.Create( options ) );
138+
139+
context.Results.Add( new()
140+
{
141+
ActionDescriptor = descriptor,
142+
GroupName = "Test",
143+
} );
144+
145+
// act
146+
apiExplorer.OnProvidersExecuted( context );
147+
148+
// assert
149+
context.Results.Single().GroupName.Should().Be( "Test-1.0" );
150+
}
151+
95152
private static IModelMetadataProvider NewModelMetadataProvider()
96153
{
97154
var provider = new Mock<IModelMetadataProvider>();

0 commit comments

Comments
 (0)