How to document dynamic query parameter names in OpenAPI (Swagger)?

家住魔仙堡 提交于 2019-12-28 04:26:19

问题


Is there any way to document the following query?

GET api/v1/users?name1=value1&name2=value

where the query parameter names are dynamic and will be received from the client.

I'm using the latest Swagger API.


回答1:


Free-form query parameters can be described using OpenAPI 3.0, but not OpenAPI 2.0 (Swagger 2.0). The parameter should have type: object with the serialization method style: form and explode: true. The object will serialized as ?prop1=value1&prop2=value2&..., where individual prop=value pairs are the object properties.

openapi: 3.0.1
...
paths:
  /users:
    get:
      parameters:
        - in: query
          name: params
          schema:
            type: object
            # If the parameter values are of specific type, e.g. string:
            additionalProperties:
              type: string
            # If the parameter values can be of different types
            # (e.g. string, number, boolean, ...)
            # additionalProperties: true

          # `style: form` and `explode: true` is the default serialization method
          # for query parameters, so these keywords can be omitted
          style: form
          explode: true

Free-form query parameters are supported in Swagger UI 3.15.0+ and Swagger Editor 3.5.6+. In the parameter editor, enter the parameter names and values in the JSON object format, e.g. { "prop1": "value1", "prop2": "value2" }. "Try it out" will send them as param=value query parameters:

Not sure about Codegen support though.




回答2:


@Helen's answer works perfectly even with Spring using the springdoc-openapi-ui library.

Dependency:

 <dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.1.43</version>
 </dependency>

In the API function, add the following parameter:

 @Parameter(in=ParameterIn.QUERY,
            name="params", style=ParameterStyle.FORM,
            schema=@Schema(type="object"), explode=Explode.TRUE,
            example="") String paramsObj


来源:https://stackoverflow.com/questions/49582559/how-to-document-dynamic-query-parameter-names-in-openapi-swagger

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