如何在Swagger/OpenAPI中指定一个查询字符串,其中参数由加号(+)分隔?

huangapple go评论59阅读模式
英文:

How do I specify a query string where the parameters are separated by + (pluses) in Swagger/OpenAPI?

问题

[ApiController]
[Route("api/v1/[controller]")]
public class CombinationsController : ControllerBase
{
    [HttpGet]
    public async Task<IActionResult> Get([FromQuery] string[] letters)
    {
        return NoContent();
    }
}
英文:

I my ASP.NET Core Web API project, I'm trying to define a route where I can query a database of words to find words that are combination of a given array of letters. For example, for the input array [&quot;d&quot;, &quot;g&quot;, &quot;o&quot;] I want to return words like dog or good.

Here's my controller with the desired GET method:

[ApiController]
[Route(&quot;api/v1/[controller]&quot;)]
public class CombinationsController : Controllerbase
{
    [HttpGet]
    public async Task&lt;IActionResult&gt; Get([FromQuery] string[] letters)
    {
        return NoContent();
    }
}

However, when I open this in Swagger and specify the desired query characters to be &quot;d&quot;, &quot;g&quot; and &quot;o&quot;, the resulting URL becomes api/v1/combinations?letters=d&amp;letters=g&amp;letters=o rather than a saner alternative such as api/v1/combinations?letters=d+g+o.

Is there a way to apply the saner alternative in ASP.NET Core when dealing with collections of strings?

答案1

得分: 1

以下是翻译好的部分:

The alternatives are comma, space or pipe delimited as described in the documentation:
可选的分隔符是逗号、空格或管道分隔,如文档中所述:

https://swagger.io/docs/specification/describing-parameters/
https://swagger.io/docs/specification/serialization/

I don't think there's a way within swagger to make the delimiters plusses.
我认为在Swagger中没有办法将分隔符设为加号。

You do, however, show comma delimiters.
但是,你确实显示逗号分隔符。

Comma query parameters relies on style: simple (the default as you have) and explode: true.
逗号查询参数依赖于样式:simple(就像你所拥有的默认设置)和explode: true。

I'm not sure there's a way to apply those settings just via attribute.
我不确定是否有办法仅通过属性来应用这些设置。

One of the extensions might allow that, but I've not come across it.
其中一个扩展可能允许这样做,但我还没有遇到过。

The attributes you use with swashbuckle work are used to generate JSON format objects to the open API standard.
你在Swashbuckle中使用的属性用于生成符合Open API标准的JSON格式对象。

You can extend the functionality using several interfaces.
你可以使用多个接口扩展功能。

IOperationFilter, IDocumentFilter etc.
IOperationFilter、IDocumentFilter等等。

What these do is allow you to fine-tune what JSON you get from the interpretation process.
这些接口允许你对从解释过程中获得的JSON进行微调。

例如:
例如:

https://www.c-sharpcorner.com/article/add-custom-parameters-in-swagger-using-asp-net-core-3-1/
https://blog.codingmilitia.com/2022/02/21/openapi-extensions-and-swashbuckle/

英文:

The alternatives are comma, space or pipe delimited as described in the documentation:

https://swagger.io/docs/specification/describing-parameters/

and

https://swagger.io/docs/specification/serialization/

I don't think there's a way within swagger to make the delimiters plusses.

You do, however, show comma delimiters.

Comma query parameters relies on style: simple ( the default as you have ) and explode: true.

如何在Swagger/OpenAPI中指定一个查询字符串,其中参数由加号(+)分隔?

I'm not sure there's a way to apply those settings just via attribute.

One of the extensions might allow that, but I've not come across it.

The attributes you use with swashbuckle work are used to generate json format objects to the open api standard.

You can extend the functionality using several interfaces.

IOperationFilter, IDocumentFilter etc.

What these do is allow you to fine tune what json you get from interpretation process.

eg

https://www.c-sharpcorner.com/article/add-custom-parameters-in-swagger-using-asp-net-core-3-1/

https://blog.codingmilitia.com/2022/02/21/openapi-extensions-and-swashbuckle/

huangapple
  • 本文由 发表于 2023年2月8日 18:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定