How to define default values for [FromQuery] parameters for the Swagger UI?

≯℡__Kan透↙ 提交于 2019-12-13 03:36:59

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/56065200/how-to-define-default-values-for-fromquery-parameters-for-the-swagger-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!