问题描述
我正在开发一个允许使用以下 URL 进行搜索的 API:
I am working on an API which allows searching with URLs like:
GET https://example.com/api/data?search[field1]=value1
GET https://example.com/api/data?search[field2]=value2
GET https://example.com/api/data?search[field1]=value1&search[field2]=value2
基本上,您可以通过将字段名称放在括号中来搜索一个或多个字段值.问题是,字段名称是由用户在其设置中定义的.字段名称将是一个字符串,但在全球范围内无法提前知道.
Basically, you can search for one or more field values by putting a field name in brackets. The problem is, the field names are defined by the user in their settings. The field name will be a string, but otherwise is not known ahead of time at a global level.
这个答案几乎就是我想要做的,我只是找不到定义值的方法括号内为任何字符串",而不是已知名称列表.
This answer is almost what I am looking to do, I just can't find a way to define the value inside the brackets to be "any string" rather than a list of known names.
推荐答案
search
参数可以定义为 deepObject 序列化的">自由形式对象style 和 minProperties: 1
强制搜索查询中至少存在一个字段.
The search
parameter can be defined as a free-form object with the deepObject
serialization style and minProperties: 1
to enforce the presence of at least one field in the search query.
确保您使用 OpenAPI 3.0 (openapi: 3.0.x
) 而不是 OpenAPI 2.0 (swagger: "2.0"
);后者不支持查询字符串中的对象.
Make sure you use OpenAPI 3.0 (openapi: 3.0.x
) and not OpenAPI 2.0 (swagger: "2.0"
); the latter does not support objects in query strings.
openapi: 3.0.2
...
paths:
/api/data:
get:
parameters:
- in: query
name: search
required: true
schema:
type: object
additionalProperties: true # Default value, may be omitted
minProperties: 1
# Optional example to use as a starting value for "try it out" in Swagger UI
example: >
{
"field1": "value1",
"field2": "value2"
}
style: deepObject
explode: true
responses:
200:
description: OK
这篇关于带括号和变量名的 OpenAPI 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!