带有 Swagger 的 MultipleApiVersions [英] MultipleApiVersions with Swagger

查看:39
本文介绍了带有 Swagger 的 MultipleApiVersions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 REST API 上启用版本控制,其中版本在标头中指定为 "api-version":2,供应商媒体类型为 "accept:application/vnd.company.resource-v2+jsonapplication/json; version=2",或在查询字符串"?version=2".该实现使用 IHttpRouteConstraint 和 RouteFactoryAttribute.这完美地工作.但是,Swagger 无法将正确的模型与正确的版本化文档匹配.operationId 始终来自版本 1 模型.

I am trying to enable versioning on a REST API, where the version is specified in the header, as "api-version":2, vendor media type, as "accept: application/vnd.company.resource-v2+json, application/json; version=2", or in a query string "?version=2". The implementation is using IHttpRouteConstraint and RouteFactoryAttribute. This works perfectly. However, Swagger is not able match the right model with the correct versioned document. The operationId is always from the version 1 model.

public class DevicesController : ApiController
{
    [HttpGet, RouteVersion( "api/devices", 1, Name = "v1.devices" )]
    [ResponseType( typeof( IEnumerable<Models.V1.Device> ) )]
    public IHttpActionResult GetV1( )
    {
        return Ok( new List<Models.V1.Device> { ... } );
    }

    [HttpGet, RouteVersion( "api/devices", 2, Name = "v2.devices" )]
    [ResponseType( typeof( IEnumerable<Models.V2.Device> ) )]
    public IHttpActionResult GetV2( )
    {
        return Ok( new List<Models.V2.Device> { ... } );
    }
}

V2 的 Swagger 文档具有错误的 operationId 和 MIME 类型.

Swagger docs for V2 has the wrong operationId and MIME Types.

"tags":
[
    "Devices"
],
"summary": "Get a list of device(s) by the device identifier",
"operationId": "Devices_GetV1",
"consumes": [ ],
"produces": 
[
    "application/vnd.safe.inventory-v1+json",
    "application/json",
    "text/json",
    "application/vnd.safe.inventory-v1+xml",
    "application/xml",
    "text/xml"
],
...

Swashbuckle 5.2.2

我已尝试按照这些帖子中的描述自定义 ApiExplorer:

I have tried customizing the ApiExplorer as descripted in these post:

github.com/domaindrivendev/Swashbuckle/issues/558
github.com/domaindrivendev/Swashbuckle/issues/317

我也尝试过构建自定义选择器.

I have also tried building a custom selector.

我也走这条路,但我认为这不是同一个问题.https://azure.microsoft.com/en-us/documentation/articles/app-service-api-dotnet-swashbuckle-customize/

I also went down this route as well, but I don’t think that is the same issue. https://azure.microsoft.com/en-us/documentation/articles/app-service-api-dotnet-swashbuckle-customize/

寻求任何帮助,我们将不胜感激.

Asking for any direction any help would be appreciated.

提前致谢.

推荐答案

尝试添加 Microsoft.AspNet.WebApi.Versioning.ApiExplorer 并使用 apiExplorer 如此处所述:https://github.com/Microsoft/aspnet-api-versioning/wiki/API-文档这对我有用.

Try add Microsoft.AspNet.WebApi.Versioning.ApiExplorer and use apiExplorer as explained here: https://github.com/Microsoft/aspnet-api-versioning/wiki/API-Documentation This works for me.

这篇关于带有 Swagger 的 MultipleApiVersions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆