Skip to content

Commit 5bf6447

Browse files
author
Chris Martinez
committed
Corrected group name generation from API version
1 parent b9ef010 commit 5bf6447

File tree

4 files changed

+5
-52
lines changed

4 files changed

+5
-52
lines changed

samples/webapi/SwaggerODataWebApiSample/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ public void Configuration( IAppBuilder builder )
6161
{
6262
foreach ( var group in apiExplorer.ApiDescriptions )
6363
{
64-
var apiVersion = group.ApiVersion;
6564
var description = "A sample application with Swagger, Swashbuckle, OData, and API versioning.";
6665

6766
if ( group.IsDeprecated )
6867
{
6968
description += " This API version has been deprecated.";
7069
}
7170

72-
info.Version( apiExplorer.GetGroupName( apiVersion ), $"Sample API {apiVersion}" )
71+
info.Version( group.Name, $"Sample API {group.ApiVersion}" )
7372
.Contact( c => c.Name( "Bill Mei" ).Email( "[email protected]" ) )
7473
.Description( description )
7574
.License( l => l.Name( "MIT" ).Url( "https://opensource.org/licenses/MIT" ) )

samples/webapi/SwaggerWebApiSample/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@ public void Configuration( IAppBuilder builder )
4646
{
4747
foreach ( var group in apiExplorer.ApiDescriptions )
4848
{
49-
var apiVersion = group.ApiVersion;
5049
var description = "A sample application with Swagger, Swashbuckle, and API versioning.";
5150

5251
if ( group.IsDeprecated )
5352
{
5453
description += " This API version has been deprecated.";
5554
}
5655

57-
info.Version( apiExplorer.GetGroupName( apiVersion ), $"Sample API {apiVersion}" )
56+
info.Version( group.Name, $"Sample API {group.ApiVersion}" )
5857
.Contact( c => c.Name( "Bill Mei" ).Email( "[email protected]" ) )
5958
.Description( description )
6059
.License( l => l.Name( "MIT" ).Url( "https://opensource.org/licenses/MIT" ) )

src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/ApiDescriptionGroupCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ApiDescriptionGroupCollection : KeyedCollection<ApiVersion, ApiDesc
2323
/// </summary>
2424
/// <param name="apiVersion">The <see cref="ApiVersion">API version</see> to get a description group for.</param>
2525
/// <returns>A new or existing <see cref="ApiDescriptionGroup">API description group</see>.</returns>
26-
public virtual ApiDescriptionGroup GetOrAdd( ApiVersion apiVersion ) => GetOrAdd( apiVersion, _ => null );
26+
public virtual ApiDescriptionGroup GetOrAdd( ApiVersion apiVersion ) => GetOrAdd( apiVersion, v => v.ToString() );
2727

2828
/// <summary>
2929
/// Gets or adds a new API description group for the specified API version.

src/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/Description/VersionedApiExplorer.cs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -188,57 +188,12 @@ protected virtual bool ShouldExploreController( string controllerRouteParameterV
188188
/// </summary>
189189
/// <param name="apiVersion">The <see cref="ApiVersion">API version</see> to retrieve a group name for.</param>
190190
/// <returns>The group name for the specified <paramref name="apiVersion">API version</paramref>.</returns>
191-
public virtual string GetGroupName( ApiVersion apiVersion )
191+
protected virtual string GetGroupName( ApiVersion apiVersion )
192192
{
193193
Arg.NotNull( apiVersion, nameof( apiVersion ) );
194194
Contract.Ensures( !IsNullOrEmpty( Contract.Result<string>() ) );
195195

196-
var format = new StringBuilder();
197-
var formatProvider = InvariantCulture;
198-
199-
if ( apiVersion.GroupVersion == null )
200-
{
201-
format.Append( 'v' );
202-
format.Append( apiVersion.MajorVersion ?? 0 );
203-
204-
if ( apiVersion.MinorVersion != null && apiVersion.MinorVersion.Value > 0 )
205-
{
206-
format.Append( '.' );
207-
format.Append( apiVersion.MinorVersion );
208-
}
209-
}
210-
else
211-
{
212-
format.Append( apiVersion.GroupVersion.Value.ToString( "yyyy-MM-dd", formatProvider ) );
213-
214-
if ( apiVersion.MajorVersion == null )
215-
{
216-
if ( apiVersion.MinorVersion != null )
217-
{
218-
format.Append( "-0." );
219-
format.Append( apiVersion.MinorVersion.Value );
220-
}
221-
}
222-
else
223-
{
224-
format.Append( '-' );
225-
format.Append( apiVersion.MajorVersion.Value );
226-
227-
if ( apiVersion.MinorVersion != null && apiVersion.MinorVersion.Value > 0 )
228-
{
229-
format.Append( '.' );
230-
format.Append( apiVersion.MinorVersion.Value );
231-
}
232-
}
233-
}
234-
235-
if ( !IsNullOrEmpty( apiVersion.Status ) )
236-
{
237-
format.Append( '-' );
238-
format.Append( apiVersion.Status );
239-
}
240-
241-
return format.ToString();
196+
return apiVersion.ToString( Options.GroupNameFormat, InvariantCulture );
242197
}
243198

244199
/// <summary>

0 commit comments

Comments
 (0)