通过Swashbuckle / Swagger输出重复的参数

时间:2017-01-16 04:32:18

标签: c# asp.net-web-api swagger swashbuckle

代码在帖子中更进一步。

问题:我希望Swashbuckle生成以下两个“GET”请求:

 1. mydomain.com/api/books?apikey=12345567891011121314151617181920 
 2. mydomain.com/api/books/1234?apikey=12345567891011121314151617181920 

Swashbuckle在#1上做得很好,而且它在Swagger UI中表现很好。但对于#2,它最终会生成(和调用):

mydomain.com/api/books/{Id}?id=1234&apikey==12345567891011121314151617181920 

毋庸置疑,GET失败了。 Swashbuckle从路由属性以及BookDetail对象中获取文字“Id”。事实上,它在UI中显示了两个ID,但我能够通过挂钩自定义IOperationFilter来解决这个问题,但这显然无法帮助纠正实际的GET请求路径。

我已经看过Duplicate parameter output in Swagger 2,但这个答案对我不起作用。我正在寻找Swashbuckle使用BookDetail对象的“Id”,以便Swagger UI显示描述(来自XML注释),同时支持基于路由的ID而不是基于查询字符串: mydomain.com/api/书籍/ 1234 ... 。我究竟做错了什么? (我在Swashbuckle 5.2.2上)。

代码

/// <summary>
/// Books controller
/// </summary>
[RoutePrefix("api")]
public class BooksController : ApiController
{
    /// <summary>
    /// Returns books matching the search query
    /// </summary>
    /// <param name="searchRequest"></param>
    /// <returns></returns>
    [Route("books")]
    public IHttpActionResult Get(BookSearch searchRequest)
    {
        //Do stuff with request

        return Ok();
    }

    /// <summary>
    /// Retruns a single book for the given ID
    /// </summary>
    /// <param name="detailRequest"></param>
    /// <returns></returns>
    [Route("books/{id:int}")]
    public IHttpActionResult Get(BookDetail detailRequest)
    {
        //Do stuff with request

        return Ok();
    }

    /// <summary>
    /// Book search
    /// </summary>
    public class BookSearch
    {
        /// <summary>
        /// API key
        /// </summary>
        [Required]
        public string ApiKey { get; set; }

        /// <summary>
        /// Search terms
        /// </summary>
        public string Query { get; set; }
    }

    /// <summary>
    /// Book detail
    /// </summary>
    public class BookDetail
    {
        /// <summary>
        /// API key
        /// </summary>
        [Required]
        public string ApiKey { get; set; }

        /// <summary>
        /// Book of the ID you want to retrieve
        /// </summary>
        [Required]
        public int Id { get; set; }

        /// <summary>
        /// Boolean indicating whether to include photos or not
        /// </summary>
        public bool IncludePhotos { get; set; }
    }
}

0 个答案:

没有答案