Search code examples
openapiswagger-editor

OpenAPI parameter with brackets and variable name


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.


Solution

  • 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.

    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