26

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.

Helen
  • 87,344
  • 17
  • 243
  • 314
Sharad Ahire
  • 758
  • 2
  • 16
  • 32

2 Answers2

41

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

openapi: 3.0.3
...
paths:
  /users:
    get:
      parameters:
        - in: query

          # Arbitrary name. It won't appear in the request URL, but will be used
          # in server & client code generated from this OAS document.
          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:

Free-form query parameters in Swagger UI

Not sure about Codegen support though.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Thanks! this was what I needed. Also, do you know if there is a way to export API documentation from a Spring Boot application for OpenAPI 3.0 ? – abisheksampath Nov 13 '18 at 18:14
  • 1
    @abisheksampath sorry I'm not familiar with Spring Boot. If you mean [Springfox](https://github.com/springfox/springfox/) specifically it doesn't seem to [support OpenAPI 3.0](https://github.com/springfox/springfox/issues/2022) yet (as of November 2018). – Helen Nov 13 '18 at 18:35
  • This doesn't work with codegen. It throws an error `Cannot convert undefined or null to object` when accessing `schema.properties` – Maria Ines Parnisari May 23 '20 at 01:00
  • @MariaInesParnisari send a bug report to the codegen project that you used. – Helen May 24 '20 at 13:59
3

@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
Helen
  • 87,344
  • 17
  • 243
  • 314
Nischal Revooru
  • 195
  • 2
  • 10
  • This can not work with a `Map`. The type is ignored : https://github.com/springdoc/springdoc-openapi/blob/master/springdoc-openapi-webmvc-core/src/main/java/org/springdoc/core/RequestBuilder.java#L35 – GuanacoBE Nov 11 '19 at 14:02