swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Swagger-ui v3.x POST formData request with wrong Content-Type (Swagger 2.0)

Open gregorfelkar opened this issue 6 years ago • 4 comments

Q&A (please complete the following information)

  • OS: macOS
  • Browser: chrome
  • Version: 85
  • Method of installation: npm install swagger-ui
  • Swagger-UI version: 3.32.4
  • Swagger/OpenAPI version: Swagger 2.0

Swagger-UI configuration options:

SwaggerUI({
      dom_id: '#swagger-ui',
      spec: spec,
      docExpansion: ['none'],
      defaultModelsExpandDepth: 0,
      filter: true,
      displayRequestDuration: true,
      plugins: [AdvancedFilterPlugin],
      requestInterceptor: (req: any) => {
        req.url += req.url.indexOf('?') === -1 ? '?' + this.auth.getParams() : '&' + this.auth.getParams();
        req.headers.Authorization = this.auth.getHeaders().Authorization;
        return req;
      }
});

How can we help?

We have an issue with swagger-ui v3.x and swagger 2.0 specification. The issue occurs when you execute try it out with an POST request which has fromData parameters. We have noticed that content-type is set to application/json instead application/x-www-form-urlencoded. So our API reponds with

{
  "error": {
    "statusCode": 400,
    "name": "SyntaxError",
    "message": "Unexpected token e in JSON at position 0",
    "status": 400
  }
}

Request Content-Type from chrome inspector

Screenshot 2020-10-12 at 23 11 05

Part of spec file

Screenshot 2020-10-12 at 23 05 03

So our questions are:

  • Does swagger-ui v3.x fully supports swagger 2.0 specification?
  • Do we need to add some additional configurations for POST request to work or to have correct Content-Type?
  • Do we need to change/update specification?
  • Do we need to use swagger-ui v2.x for swagger 2.0 specification?

We can use swagger-ui v2.x but it would be nice to have search filter if this is possible in older version. Thank you in advance.

gregorfelkar avatar Oct 12 '20 21:10 gregorfelkar

Please post your OpenAPI JSON file as text.

hkosova avatar Oct 19 '20 10:10 hkosova

Our swagger.json file

{
  "swagger": "2.0",
  "info": {
    "version": "1.65.0",
    "title": "customer",
    "description": "Customer API"
  },
  "basePath": "/api",
  "paths": {
    "/Customers": {
      "get": {
        "tags": ["Customer"],
        "summary": "Find all instances of the model matched by filter from the data source.",
        "operationId": "Customer.find",
        "parameters": [
          {
            "name": "filter",
            "in": "query",
            "description": "Filter defining fields, where, include, order, offset, and limit - must be a JSON-encoded string (`{\"where\":{\"something\":\"value\"}}`).  See https://loopback.io/doc/en/lb3/Querying-data.html#using-stringified-json-in-rest-queries for more details.",
            "required": false,
            "type": "string",
            "format": "JSON"
          }
        ],
        "responses": {
          "200": {
            "description": "Request was successful",
            "schema": {
              "type": "array",
              "items": { "$ref": "#/definitions/Customer" }
            }
          }
        },
        "deprecated": false
      },
      "post": {
        "tags": ["Customer"],
        "summary": "Create a customer.",
        "description": "<div><strong>200</strong>: Success, customer created.</div>\n<div><strong>422</strong>: Input validation failed.</div>\n<div><strong>500</strong>: Internal server error.</div>",
        "operationId": "Customer.customCreate",
        "parameters": [
          {
            "name": "email",
            "in": "formData",
            "required": false,
            "type": "string"
          },
          {
            "name": "password",
            "in": "formData",
            "required": false,
            "type": "string"
          },
          {
            "name": "name",
            "in": "formData",
            "required": false,
            "type": "string"
          },
          {
            "name": "notificationOptions",
            "in": "formData",
            "required": false,
            "type": "string",
            "format": "JSON"
          },
          {
            "name": "passwordLessToken",
            "in": "formData",
            "required": false,
            "default": false,
            "type": "boolean"
          },
          {
            "name": "source",
            "in": "formData",
            "required": false,
            "default": "customCreate",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "Request was successful",
            "schema": { "$ref": "#/definitions/Customer" }
          }
        },
        "deprecated": false
      }
    }
  },
  "tags": [
    {
      "name": "Customer",
      "description": "A Customer is initialized with a default config."
    }
  ],
  "consumes": [
    "application/json",
    "application/x-www-form-urlencoded",
    "application/xml",
    "text/xml"
  ],
  "produces": [
    "application/json",
    "application/xml",
    "text/xml",
    "application/javascript",
    "text/javascript"
  ],
  "definitions": {
    "x-any": { "properties": {} },
    "Customer": {
      "description": "A Customer is initialized with a default config.",
      "properties": {
        "id": { "$ref": "#/definitions/ObjectID" },
        "contact": { "type": "string" },
        "name": { "type": "string" },
        "acceptLanguage": {
          "default": "en-US",
          "description": "The acceptable language for this user",
          "type": "string"
        },
        "preferredLanguage": {
          "description": "The language set for this user based on API and/or it's language header",
          "type": "string"
        },
        "source": { "default": "unknown", "type": "string" },
        "createdAt": { "type": "string", "format": "date-time" },
        "_version": {
          "default": "1.0.0",
          "description": "schema version # of a Mongo document",
          "type": "string"
        },
        "realm": { "type": "string" },
        "username": { "type": "string" },
        "email": { "type": "string" },
        "emailVerified": { "type": "boolean" }
      },
      "required": ["name", "_version", "email"],
      "additionalProperties": false
    },
    "ObjectID": { "type": "string", "pattern": "^[a-fA-F\\d]{24}$" }
  }
}

gregorfelkar avatar Oct 27 '20 13:10 gregorfelkar

In this example, the Content-Type for POST is derived from global consumes list which is defined at:

  "consumes": [
    "application/json",
    "application/x-www-form-urlencoded",
    "application/xml",
    "text/xml"
  ],

Swagger UI picks the first value which is application/json.

To resolve the issue you need to either remove global consumes values other than application/x-www-form-urlencoded, or override the consumes list for the POST operation:

      "post": {
        "tags": ["Customer"],
        "consumes": ["application/x-www-form-urlencoded"],   // <----------

hkosova avatar Oct 27 '20 13:10 hkosova

Yes that was our suspicion inspecting and comparing our swagger.json with your petstore example. Thank you for confirming that.

gregorfelkar avatar Oct 27 '20 13:10 gregorfelkar