繁体   English   中英

带有 FromQuery 属性的 HTTP GET 方法的 SwashBuckle Swagger-UI 示例请求

[英]SwashBuckle Swagger-UI Example Request for HTTP GET Method with FromQuery Attribute

我已经设法使用SwashBuckle.AspNetCoreSwashbuckle.AspNetCore.Filters为我的 Web API 添加示例以用于 POST 方法:

DTO

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

示例请求

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

控制器方法

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

这项工作非常好。 当我单击“试用”按钮时,我将“bar”作为属性 foo 的预填充值。

但是,当我尝试对 GET 请求执行相同操作时,例如,使用来自这样的查询的参数时,文本框未预填充值“bar”:

在此处输入图片说明

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

控制器方法

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

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

如果我没记错的话,你看到的值是:

那不是示例而是默认值。


这是我过去做过的事情:

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


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

"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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM