Skip to content

6.1.1 fixes and 6.2 features #901

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 15 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ public class ApiVersionMetadata
Name = name ?? string.Empty;
}

/// <summary>
/// Initializes a new instance of the <see cref="ApiVersionMetadata"/> class.
/// </summary>
/// <param name="other">The other <see cref="ApiVersionMetadata">instance</see> to initialize from.</param>
protected ApiVersionMetadata( ApiVersionMetadata other )
{
if ( other == null )
{
throw new ArgumentNullException( nameof( other ) );
}

apiModel = other.apiModel;
endpointModel = other.endpointModel;
mergedModel = other.mergedModel;
Name = other.Name;
}

/// <summary>
/// Gets an empty API version information.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>6.1.0</VersionPrefix>
<AssemblyVersion>6.1.0.0</AssemblyVersion>
<VersionPrefix>6.2.0</VersionPrefix>
<AssemblyVersion>6.2.0.0</AssemblyVersion>
<TargetFrameworks>netstandard1.0;netstandard2.0;net6.0</TargetFrameworks>
<AssemblyTitle>API Versioning Abstractions</AssemblyTitle>
<Description>The abstractions library for API versioning.</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Added ApiVersion copy constructor

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@

namespace Asp.Versioning.Http.UsingMediaType.Controllers;

using Newtonsoft.Json.Linq;
using System.Web.Http;

[ApiVersion( "2.0" )]
[Route( "api/values" )]
[RoutePrefix( "api/values" )]
public class Values2Controller : ApiController
{
public IHttpActionResult Get() => Ok( new { controller = GetType().Name, version = Request.GetRequestedApiVersion().ToString() } );
[Route]
public IHttpActionResult Get() =>
Ok( new { controller = GetType().Name, version = Request.GetRequestedApiVersion().ToString() } );

[Route( "{id}", Name = "GetByIdV2" )]
public IHttpActionResult Get( string id ) =>
Ok( new { controller = GetType().Name, Id = id, version = Request.GetRequestedApiVersion().ToString() } );

public IHttpActionResult Post( [FromBody] JToken json ) =>
CreatedAtRoute( "GetByIdV2", new { id = "42" }, json );
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ namespace Asp.Versioning.Http.UsingMediaType.Controllers;
using System.Web.Http;

[ApiVersion( "1.0" )]
[Route( "api/values" )]
[RoutePrefix( "api/values" )]
public class ValuesController : ApiController
{
public IHttpActionResult Get() => Ok( new { controller = GetType().Name, version = Request.GetRequestedApiVersion().ToString() } );
[Route]
public IHttpActionResult Get() =>
Ok( new { controller = GetType().Name, version = Request.GetRequestedApiVersion().ToString() } );

[Route( "{id}" )]
public IHttpActionResult Get( string id ) =>
Ok( new { controller = GetType().Name, Id = id, version = Request.GetRequestedApiVersion().ToString() } );
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task then_get_should_return_200( string controller, string apiVersi
}

[Fact]
public async Task then_get_should_return_415_for_an_unsupported_version()
public async Task then_get_should_return_406_for_an_unsupported_version()
{
// arrange
using var request = new HttpRequestMessage( Get, "api/values" )
Expand All @@ -49,6 +49,23 @@ public async Task then_get_should_return_415_for_an_unsupported_version()
var response = await Client.SendAsync( request );
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( NotAcceptable );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
}

[Fact]
public async Task then_post_should_return_415_for_an_unsupported_version()
{
// arrange
var entity = new { text = "Test" };
var mediaType = Parse( "application/json;v=3.0" );
using var content = new ObjectContent( entity.GetType(), entity, new JsonMediaTypeFormatter(), mediaType );

// act
var response = await Client.PostAsync( "api/values", content );
var problem = await response.Content.ReadAsProblemDetailsAsync();

// assert
response.StatusCode.Should().Be( UnsupportedMediaType );
problem.Type.Should().Be( ProblemDetailsDefaults.Unsupported.Type );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ protected override bool ShouldExploreAction(
return base.ShouldExploreAction( actionRouteParameterValue, actionDescriptor, route, apiVersion );
}

if ( actionDescriptor.ControllerDescriptor.ControllerType.IsMetadataController() )
{
if ( actionDescriptor.ActionName == nameof( MetadataController.GetServiceDocument ) )
{
if ( !Options.MetadataOptions.HasFlag( ODataMetadataOptions.ServiceDocument ) )
{
return false;
}
}
else
{
if ( !Options.MetadataOptions.HasFlag( ODataMetadataOptions.Metadata ) )
{
return false;
}
}
}

if ( Options.UseApiExplorerSettings )
{
var setting = actionDescriptor.GetCustomAttributes<ApiExplorerSettingsAttribute>().FirstOrDefault();
Expand Down Expand Up @@ -112,9 +130,10 @@ protected override bool ShouldExploreController(
throw new ArgumentNullException( nameof( route ) );
}

if ( typeof( MetadataController ).IsAssignableFrom( controllerDescriptor.ControllerType ) )
if ( controllerDescriptor.ControllerType.IsMetadataController() )
{
return false;
controllerDescriptor.ControllerName = "OData";
return Options.MetadataOptions > ODataMetadataOptions.None;
}

var routeTemplate = route.RouteTemplate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>6.1.0</VersionPrefix>
<AssemblyVersion>6.1.0.0</AssemblyVersion>
<VersionPrefix>6.2.0</VersionPrefix>
<AssemblyVersion>6.2.0.0</AssemblyVersion>
<TargetFrameworks>net45;net472</TargetFrameworks>
<RootNamespace>Asp.Versioning</RootNamespace>
<AssemblyTitle>ASP.NET Web API Versioning API Explorer for OData v4.0</AssemblyTitle>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Update OData query option exploration (#702, #853)

Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,25 @@ private void AppendPathFromConventions( IList<string> segments, string controlle
case UnboundOperation:
builder.Append( Context.Operation!.Name );
AppendParametersFromConvention( builder, Context.Operation );
break;
default:
var action = Context.ActionDescriptor;

if ( action.ControllerDescriptor.ControllerType.IsMetadataController() )
{
if ( action.ActionName == nameof( MetadataController.GetServiceDocument ) )
{
if ( segments.Count == 0 )
{
segments.Add( "/" );
}
}
else
{
segments.Add( "$metadata" );
}
}

break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ internal ODataRouteBuilderContext(
ActionDescriptor = actionDescriptor;
ParameterDescriptions = parameterDescriptions;
Options = options;
UrlKeyDelimiter = UrlKeyDelimiterOrDefault( configuration.GetUrlKeyDelimiter() ?? Services.GetService<IODataPathHandler>()?.UrlKeyDelimiter );
UrlKeyDelimiter = UrlKeyDelimiterOrDefault(
configuration.GetUrlKeyDelimiter() ??
Services.GetService<IODataPathHandler>()?.UrlKeyDelimiter );

var selector = Services.GetRequiredService<IEdmModelSelector>();
var model = selector.SelectModel( apiVersion );
Expand All @@ -64,7 +66,8 @@ internal ODataRouteBuilderContext(
Singleton = container.FindSingleton( controllerName );
Operation = ResolveOperation( container, actionDescriptor );
ActionType = GetActionType( actionDescriptor );
IsRouteExcluded = ActionType == ODataRouteActionType.Unknown;
IsRouteExcluded = ActionType == ODataRouteActionType.Unknown &&
!actionDescriptor.ControllerDescriptor.ControllerType.IsMetadataController();

if ( Operation?.IsAction() == true )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>6.1.0</VersionPrefix>
<AssemblyVersion>6.1.0.0</AssemblyVersion>
<VersionPrefix>6.2.0</VersionPrefix>
<AssemblyVersion>6.2.0.0</AssemblyVersion>
<TargetFrameworks>net45;net472</TargetFrameworks>
<RootNamespace>Asp.Versioning</RootNamespace>
<AssemblyTitle>API Versioning for ASP.NET Web API with OData v4.0</AssemblyTitle>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Minor version bump

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public void api_descriptions_should_group_versioned_controllers( HttpConfigurati
{
// arrange
var assembliesResolver = configuration.Services.GetAssembliesResolver();
var controllerTypes = configuration.Services.GetHttpControllerTypeResolver().GetControllerTypes( assembliesResolver );
var controllerTypes = configuration.Services
.GetHttpControllerTypeResolver()
.GetControllerTypes( assembliesResolver )
.Where( t => !typeof( MetadataController ).IsAssignableFrom( t ) );
var apiExplorer = new ODataApiExplorer( configuration );

// act
Expand All @@ -54,7 +57,10 @@ public void api_descriptions_should_flatten_versioned_controllers( HttpConfigura
{
// arrange
var assembliesResolver = configuration.Services.GetAssembliesResolver();
var controllerTypes = configuration.Services.GetHttpControllerTypeResolver().GetControllerTypes( assembliesResolver );
var controllerTypes = configuration.Services
.GetHttpControllerTypeResolver()
.GetControllerTypes( assembliesResolver )
.Where( t => !typeof( MetadataController ).IsAssignableFrom( t ) );
var apiExplorer = new ODataApiExplorer( configuration );

// act
Expand Down Expand Up @@ -86,6 +92,37 @@ public void api_descriptions_should_not_contain_metadata_controllers( HttpConfig
.NotContain( type => typeof( MetadataController ).IsAssignableFrom( type ) );
}

[Theory]
[InlineData( ODataMetadataOptions.ServiceDocument )]
[InlineData( ODataMetadataOptions.Metadata )]
[InlineData( ODataMetadataOptions.All )]
public void api_descriptions_should_contain_metadata_controllers( ODataMetadataOptions metadataOptions )
{
// arrange
var configuration = TestConfigurations.NewOrdersConfiguration();
var options = new ODataApiExplorerOptions( configuration ) { MetadataOptions = metadataOptions };
var apiExplorer = new ODataApiExplorer( configuration, options );

// act
var groups = apiExplorer.ApiDescriptions;

// assert
for ( var i = 0; i < groups.Count; i++ )
{
var group = groups[i];

if ( metadataOptions.HasFlag( ODataMetadataOptions.ServiceDocument ) )
{
group.ApiDescriptions.Should().Contain( item => item.RelativePath == "api" );
}

if ( metadataOptions.HasFlag( ODataMetadataOptions.Metadata ) )
{
group.ApiDescriptions.Should().Contain( item => item.RelativePath == "api/$metadata" );
}
}
}

[Theory]
[ClassData( typeof( TestConfigurations ) )]
public void api_description_group_should_explore_v3_actions( HttpConfiguration configuration )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Asp.Versioning.Description;

using Asp.Versioning.Controllers;
using Asp.Versioning.Conventions;
using Asp.Versioning.OData;
using Asp.Versioning.Simulators.Configuration;
Expand All @@ -24,6 +25,7 @@ public static HttpConfiguration NewOrdersConfiguration()
{
var configuration = new HttpConfiguration();
var controllerTypeResolver = new ControllerTypeCollection(
typeof( VersionedMetadataController ),
typeof( Simulators.V1.OrdersController ),
typeof( Simulators.V2.OrdersController ),
typeof( Simulators.V3.OrdersController ) );
Expand Down Expand Up @@ -57,9 +59,10 @@ public static HttpConfiguration NewPeopleConfiguration()
{
var configuration = new HttpConfiguration();
var controllerTypeResolver = new ControllerTypeCollection(
typeof( Simulators.V1.PeopleController ),
typeof( Simulators.V2.PeopleController ),
typeof( Simulators.V3.PeopleController ) );
typeof( VersionedMetadataController ),
typeof( Simulators.V1.PeopleController ),
typeof( Simulators.V2.PeopleController ),
typeof( Simulators.V3.PeopleController ) );

configuration.Services.Replace( typeof( IHttpControllerTypeResolver ), controllerTypeResolver );
configuration.AddApiVersioning();
Expand All @@ -79,8 +82,9 @@ public static HttpConfiguration NewProductAndSupplierConfiguration()
{
var configuration = new HttpConfiguration();
var controllerTypeResolver = new ControllerTypeCollection(
typeof( Simulators.V3.ProductsController ),
typeof( Simulators.V3.SuppliersController ) );
typeof( VersionedMetadataController ),
typeof( Simulators.V3.ProductsController ),
typeof( Simulators.V3.SuppliersController ) );

configuration.Services.Replace( typeof( IHttpControllerTypeResolver ), controllerTypeResolver );
configuration.AddApiVersioning();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>6.1.0</VersionPrefix>
<AssemblyVersion>6.1.0.0</AssemblyVersion>
<VersionPrefix>6.2.0</VersionPrefix>
<AssemblyVersion>6.2.0.0</AssemblyVersion>
<TargetFrameworks>net45;net472</TargetFrameworks>
<AssemblyTitle>ASP.NET Web API Versioning API Explorer</AssemblyTitle>
<Description>The API Explorer extensions for ASP.NET Web API Versioning.</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.

using Asp.Versioning.Routing;
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo( typeof( IBoundRouteTemplate ) )]
[assembly: TypeForwardedTo( typeof( IParsedRoute ) )]
[assembly: TypeForwardedTo( typeof( IPathContentSegment ) )]
[assembly: TypeForwardedTo( typeof( IPathLiteralSubsegment ) )]
[assembly: TypeForwardedTo( typeof( IPathParameterSubsegment ) )]
[assembly: TypeForwardedTo( typeof( IPathSegment ) )]
[assembly: TypeForwardedTo( typeof( IPathSeparatorSegment ) )]
[assembly: TypeForwardedTo( typeof( IPathSubsegment ) )]
[assembly: TypeForwardedTo( typeof( RouteParser ) )]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Minor version bump

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>6.1.0</VersionPrefix>
<AssemblyVersion>6.1.0.0</AssemblyVersion>
<VersionPrefix>6.2.0</VersionPrefix>
<AssemblyVersion>6.2.0.0</AssemblyVersion>
<TargetFrameworks>net45;net472</TargetFrameworks>
<AssemblyTitle>ASP.NET Web API Versioning</AssemblyTitle>
<Description>A service API versioning library for Microsoft ASP.NET Web API.</Description>
Expand Down
Loading