SwashBuckle Swagger-UI具有FromQuery属性的HTTP GET方法的示例请求 [英] SwashBuckle Swagger-UI Example Request for HTTP GET Method with FromQuery Attribute

查看:44
本文介绍了SwashBuckle Swagger-UI具有FromQuery属性的HTTP GET方法的示例请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地使用 SwashBuckle.AspNetCore Swashbuckle.AspNetCore.Filters 为POST方法向Web API添加了示例:

DTO

 公共类ExampleDTO{公共字符串MyFoo {get;放;}} 

示例请求

 公共类ExampleDTOExample:IExamplesProvider< ExampleDTO>{公共ExampleDTO GetExamples(){返回新的ExampleDTO(){MyFoo =酒吧"};}} 

控制器方法

  [SwaggerOperation(摘要="...",描述="...",OperationId ="PostFoo")][SwaggerResponse(200,"Returns ...",typeof(int))][HttpPost][Route("post-foo")]公共ActionResult< int>PostFoo([FromBody] ExampleDTO请求){抛出新的NotImplementedException();} 

这项工作完全正常.当我单击试用"按钮时,我将"bar"作为属性foo的预填充值.

但是,当我尝试对GET请求执行相同的操作时,例如,使用类似这样的查询参数,则文本框不会预填充值"bar":

 公共类ExampleDTO{[FromQuery(Name ="foo")]公共字符串MyFoo {get;放;}} 

控制器方法

  [SwaggerOperation(摘要="...",描述="...",OperationId ="GetFoo")][SwaggerResponse(200,"Returns ...",typeof(int))][HttpGet][Route("get-foo")]公共ActionResult< int>GetFoo([FromQuery] ExampleDTO请求){抛出新的NotImplementedException();} 

如何强制文本框预填充示例值?到目前为止,我已经找到一种解决方案,用于指定不需要的默认值.我只想在Swagger UI中使用属性作为默认值.

解决方案

如果我没记错的话,您在以下位置看到的值:

那不是示例,而是默认值.


这是我过去所做的事情:

 "/attrib/{payId}":{得到": {标签":[属性"],"operationId":"Attribute_Get",消费":[],产生":["application/json","text/json",文本/html"],参数": [{"name":"payId","in":路径",必填":是的,"type":整数","format":"int32",默认值":123}] 


这是默认和示例的另一种情况

 公司":{必需的": [ID",我的身份"],特性": {ID": {"description":唯一的公司ID","example":"123","type":整数","format":"int32",默认值":456},我的身份": {示例":123,"type":整数","format":"int32"}, 

http://swagger-net-test.azurewebsites.net/swagger/ui/index#/Company/Company_Get2

您可以看到该示例不是Swagger UI中显示的内容

I've managed to add examples to my Web API with SwashBuckle.AspNetCore and Swashbuckle.AspNetCore.Filters for POST methods:

DTO

public class ExampleDTO
{
    public string MyFoo { get; set; }
}

Example Request

public class ExampleDTOExample : IExamplesProvider<ExampleDTO>
{
    public ExampleDTO GetExamples()
    {
        return new ExampleDTO()
        {
            MyFoo = "bar"
        };
    }
}

Controller Method

[SwaggerOperation(
    Summary = "...",
    Description = "...",
    OperationId = "PostFoo"
)]
[SwaggerResponse(200, "Returns ...", typeof(int))]
[HttpPost]
[Route("post-foo")]
public ActionResult<int> PostFoo([FromBody]ExampleDTO request)
{
    throw new NotImplementedException();
}

This work perfectly fine. When I click the "try it out" button, I have "bar" as prefilled value for the property foo.

However, when I'm trying to do the same for a GET request, e.g., with parameters from query like this, the text box is not prefilled with the value "bar":

public class ExampleDTO
{
    [FromQuery(Name = "foo")]
    public string MyFoo { get; set; }
}

Controller Method

[SwaggerOperation(
    Summary = "...",
    Description = "...",
    OperationId = "GetFoo"
)]
[SwaggerResponse(200, "Returns ...", typeof(int))]
[HttpGet]
[Route("get-foo")]
public ActionResult<int> GetFoo([FromQuery]ExampleDTO request)
{
    throw new NotImplementedException();
}

How can I force the text box to be prefilled with the example value? So far I've found a solution for specifying a default value which is not want I want. I only want to use attributes for a default value in Swagger UI.

解决方案

If I'm not mistaken the value you see on:

That is not the example but the default value.


Here is something that I've done in the past:

"/attrib/{payId}": {
    "get": {
        "tags": [
            "Attribute"
        ],
        "operationId": "Attribute_Get",
        "consumes": [],
        "produces": [
            "application/json",
            "text/json",
            "text/html"
        ],
        "parameters": [
            {
                "name": "payId",
                "in": "path",
                "required": true,
                "type": "integer",
                "format": "int32",
                "default": 123
            }
        ]

http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Attribute#/Attribute/Attribute_Get


Here is another case with both default and example

"Company": {
    "required": [
        "Id",
        "MyId"
    ],
    "properties": {
        "Id": {
            "description": "The Unique Company ID",
            "example": "123",
            "type": "integer",
            "format": "int32",
            "default": 456
        },
        "MyId": {
            "example": 123,
            "type": "integer",
            "format": "int32"
        },

http://swagger-net-test.azurewebsites.net/swagger/ui/index#/Company/Company_Get2

You can see that the example is not what is shown in the Swagger UI

这篇关于SwashBuckle Swagger-UI具有FromQuery属性的HTTP GET方法的示例请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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