|
2 | 2 |
|
3 | 3 | namespace Asp.Versioning.ApiExplorer;
|
4 | 4 |
|
| 5 | +using Microsoft.AspNetCore.Mvc.Abstractions; |
5 | 6 | using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
6 | 7 | using Microsoft.AspNetCore.Mvc.ModelBinding;
|
7 | 8 | using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
|
@@ -92,6 +93,62 @@ public void versioned_api_explorer_should_apply_sunset_policy()
|
92 | 93 | .BeTrue();
|
93 | 94 | }
|
94 | 95 |
|
| 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 | + |
95 | 152 | private static IModelMetadataProvider NewModelMetadataProvider()
|
96 | 153 | {
|
97 | 154 | var provider = new Mock<IModelMetadataProvider>();
|
|
0 commit comments