swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.38k stars 8.92k forks source link

GET parameters not exploded when `"in": "query","style": "form","explode": true` and the request can be `oneOf` a few different objects #10002

Open JohnRDOrazio opened 3 months ago

JohnRDOrazio commented 3 months ago

Q&A (please complete the following information)

Content & configuration

Example Swagger/OpenAPI definition:

{
  "openapi": "3.1.0",
  "servers": [
    {
      "description": "Petstore Swagger API",
      "url": "https://petstore.swagger.io/v2"
    }
  ],
  "info": {
    "description": "This is a sample Petstore API.",
    "version": "1.0",
    "title": "Petstore",
    "contact": {
      "email": "apiteam@swagger.io",
      "name": "Swagger API Team"
    },
    "license": {
      "name": "Apache 2.0",
      "url": "https://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "paths": {
    "/petsearch": {
      "get": {
        "tags": ["pet"],
        "security": [{}],
        "summary": "Everything about your Pets",
        "operationId": "retrievePetGET",
        "description": "Returns a list of available pets that fit your desires",
        "parameters": [
          {
            "in": "query",
            "name": "parameters",
            "style": "form",
            "explode": true,
            "schema": {
              "oneOf": [
                {
                  "$ref": "#/components/schemas/RequestBody"
                }
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK: available pets that correspond with your criteria",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pets"
                }
              },
              "application/yaml": {
                "schema": {
                  "$ref": "#/components/schemas/Pets"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "RequestBody": {
        "type": "object",
        "title": "Pet Search Request",
        "description": "Search for a pet that correponds to certain criteria",
        "additionalProperties": false,
        "properties": {
          "animal": {
            "type": "string",
            "enum": ["dog", "cat", "bird"]
          },
          "color": {
            "type": "string",
            "enum": ["black", "white", "multicolored"]
          }
        },
        "required": ["animal"]
      },
      "Pets": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "animal": {
              "type": "string",
              "enum": ["cat", "dog", "bird"]
            },
            "color": {
              "type": "string",
              "enum": ["black", "white", "multicolored"]
            }
          }
        }
      }
    }
  }
}

Swagger-UI configuration options:

const ui = SwaggerUIBundle({
  url: "openapi.json",
  dom_id: "#swagger-ui",
  deepLinking: true,
  requestSnippetsEnabled: true,
  requestSnippets: {
    generators: {
      curl_bash: {
        title: "cURL (bash)",
        syntax: "bash",
        default: true,
      },
      curl_powershell: {
        title: "cURL (PowerShell)",
        syntax: "powershell",
      },
      curl_cmd: {
        title: "cURL (CMD)",
        syntax: "bash",
      },
    },
    defaultExpanded: true,
    languages: ["curl_bash", "curl_powershell", "curl_cmd"],
  },
  presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
  plugins: [SwaggerUIBundle.plugins.DownloadUrl],
  layout: "StandaloneLayout",
});
?animal=dog&color=black

Describe the bug you're encountering

The parameters are expanded just fine if there is only one possible request object. But if you have more than one "type" of request that can be formulated, and you express this with a oneOf, the parameters are no longer expanded / exploded. The whole object is simply serialized as the value of the named parameter. Even if there is only a single object inside the oneOf the exploding still gets messed up.

To reproduce...

Here's a live example on codesandbox.

Steps to reproduce the behavior in the live example on codesandbox:

  1. Expand the GET operation in right preview panel
  2. Click on Try it out
  3. Click on Execute
  4. In the request snippets you will see that the parameters are not exploded but simply serialized as a single object

Expected behavior

The parameters should be exploded / expanded even when there are multiple "types" to choose from that are defined in an anyOf.

Screenshots

image

JohnRDOrazio commented 3 months ago

I thought it might of had to do with the Request Snippets, but it still happened when they were disabled, and I was able to pinpoint the issue to the use of oneOf.