如何为Swagger UI定义[FromQuery]参数的默认值? [英] How to define default values for [FromQuery] parameters for the Swagger UI?

查看:1115
本文介绍了如何为Swagger UI定义[FromQuery]参数的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动作:

  [HttpGet] 
[Route( foo)]
public ActionResult Foo([FromQuery] MyClass request)
{
var image = ToImage(WidgetType.MedianSalesPriceSqft,request);
return File(image.ToByteArray(), image / png);
}

在MyClass下面定义:

 公共类MyClass {
[DefaultValue( 90210)]
公共字符串Zip {get;组; }

[DefaultValue( 5361 Doverton Dr)]
公共字符串StreetAddress {get;组; }
}

当我打/swagger/index.html并想尝试一下时API,即使我定义了默认值,我也总是必须输入StreetAddress和Zip值。



Swagger当前提供了



只要您需要我的代码,就在这里:

https://github.com/heldersepu/csharp-proj/tree/master/WebApi_NetCore


I have an action:

[HttpGet]
[Route("foo")]
public ActionResult Foo([FromQuery] MyClass request)
{
    var image = ToImage(WidgetType.MedianSalesPriceSqft, request);
    return File(image.ToByteArray(), "image/png");
} 

below MyClass is defined:

public class MyClass {
    [DefaultValue("90210")]
    public string Zip { get; set; }

    [DefaultValue("5361 Doverton Dr")]
    public string StreetAddress { get; set; }               
}

When I hit /swagger/index.html and want to try out this API, I always have to enter the StreetAddress and Zip values, even though I have default values defined.

Swagger currently provides a schema filter that let's you provide default values for object properties as long as they are not set to be [FromQuery]. Am I missing something simple?

解决方案

It looks like the problem has been fixed on the beta version:
https://www.nuget.org/packages/Swashbuckle.AspNetCore/5.0.0-rc2

Lot of changes on the beta version, I had a couple of DocumentFilter and needed to be refactored,
I had to comment some stuff out that could not figure out how to do on the beta.

I added your same action and class, it looks like this:

Once you click on the try it out the values are populated

Just if you needed my code is here:
https://github.com/heldersepu/csharp-proj/tree/master/WebApi_NetCore

这篇关于如何为Swagger UI定义[FromQuery]参数的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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