Skip to content

Different behavior when using fluent api versus attributes - reporting deprecated apis - Asp.Net Core #47

Closed
@AdamDotNet

Description

@AdamDotNet

Given the ASP.NET Core controller class

[ApiVersion("1.0", Deprecated = true)]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ValuesController : Controller
{
    [HttpGet]
    [MapToApiVersion("1.0")]
    public IActionResult Version1Method()
    {
        return Json(new { ApiVersion = HttpContext.GetRequestedApiVersion().ToString("v"), Hey = 1, Look = 2 });
    }

    [HttpGet]
    [MapToApiVersion("2.0")]
    public IActionResult Version2Method()
    {
        return Json(new { ApiVersion = HttpContext.GetRequestedApiVersion().ToString("v"), Hey = 2, Look = 4 });
    }
}

and Startup includes

services.AddApiVersioning(options => options.ReportApiVersions = true);

The response headers include the supported and deprecated versions

api-deprecated-versions: 1.0
api-supported-versions: 2.0

But with the following controller class

[Route("api/v{version:apiVersion}/[controller]")]
public class ValuesController : Controller
{
    [HttpGet]
    public IActionResult Version1Method()
    {
        return Json(new { ApiVersion = HttpContext.GetRequestedApiVersion().ToString("v"), Hey = 1, Look = 2 });
    }

    [HttpGet]
    public IActionResult Version2Method()
    {
        return Json(new { ApiVersion = HttpContext.GetRequestedApiVersion().ToString("v"), Hey = 2, Look = 4 });
    }
}

And Startup includes

services.AddApiVersioning(options =>
{
    options.ReportApiVersions = true;
    options.Conventions.Controller<ValuesController>()
        .HasDeprecatedApiVersion(1, 0)
        .HasApiVersion(2, 0)
        .Action(c => c.Version1Method()).MapToApiVersion(1, 0)
        .Action(c => c.Version2Method()).MapToApiVersion(2, 0);
});

The response headers only include the supported versions

api-supported-versions: 2.0

Expectation was that by ReportingApiVersions being set to true that the headers would be the same whether I used the fluent api or attributes. Either the attributes are erroneously advertising deprecated versions, or the fluent api are erroneously not advertising the deprecated versions. My assumption is the attributes are doing the right thing, advertising both the supported and deprecated versions.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions