【问题标题】:MultipleApiVersions with Swagger带有 Swagger 的 MultipleApiVersions
【发布时间】:2016-01-16 03:16:53
【问题描述】:

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

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 类型错误。

"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:

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

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

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

寻求任何方向的任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: swagger


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我们想出了一个使用自定义 ApiExplorer 的解决方案,如 http://blog.ulriksen.net/versioned-iapiexplorer 中所述

      在 Swagger 配置中:使用 MultipleApiVersions

      c.MultipleApiVersions( ( apiDesc, version ) =>
      {
          var versionConstraint = apiDesc.Route.Constraints[ "version" ] as RouteVersionConstraint;
          return ( versionConstraint != null ) && versionConstraint.AllowedVersion == version.ConvertTo<int>( );
      },
      vc =>
      {
          vc.Version( "2", "API V2" ); //add this line when v2 is released
          vc.Version( "1", "API V1" );
      } );
      

      【讨论】:

      • 您好,您的 RouteVersionContraint 课程是什么?
      猜你喜欢
      • 2015-08-27
      • 2017-04-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      相关资源
      最近更新 更多