Gabriel Borges
Gabriel Borges

Reputation: 83

"Request body" doesn't appears on Swagger UI

I'm trying to do a test with a POST request on my Swagger Documentation, but I'm having the following trouble:

On Swagger Editor I can test it normally, but on Swagger UI the Request body field doesn't appears, so I can't put my email and password to test the request.

Here you can see the Request body field with data examples ready to be edited:
Swagger Editor Example

And here you can see that Swagger UI doesn't shows it:
Swagger UI Example

Note: I've generated a nodejs-server.

My .yaml code:

openapi: '3.0.1'

info:
  version: 1.0.0
  title: Test
  description: Test openapi.

servers:
  - url: http://localhost:3005

paths:
  /login:
    post:
      summary: Login
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/login'
      responses:
        200:
          description: Test

components:
  schemas:
    login:
      type: object
      properties:
        email:
          type: string
          example: [email protected]
        password:
          type: string
          format: password
          example: example123
        expires:
          type: integer
          example: 86400
      required:
       - email
       - password

Upvotes: 2

Views: 15299

Answers (2)

Maksym
Maksym

Reputation: 1498

Although the 'request body' field does show up in the UI with the suggested update by @Helen, the posted body object still seems to fail to show up in the controller, probably since the bundled swagger-tools middleware is old as well. I had to make a few changes to node_modules/oas3-tools/middleware/swagger-metada.js for the requestBody to show up on req.swagger.params in the controller. I hope this can help to get a project through while an official oas3-tools code is in the works (if at all).

Line 162, after var parsers = _.reduce(parameters ... }, []) block:

if (swaggerMetadata.operation.requestBody && parsers.indexOf(bodyParser) === -1) parsers.push(bodyParser);

Line 225 after _.each(parameters, ... }); block:

if (req.body) swaggerMetadata.params.body = {value: req.body};

Upvotes: 0

Helen
Helen

Reputation: 97540

TL;DR: Update Swagger UI to the latest version.

Just to clarify the scenario (for future readers): The OP pasted this OpenAPI 3.0 definition into Swagger Editor, generated a Node.js server and ran it. The request body display issue is with Swagger UI that is part of this Node.js server (http://localhost:8080/docs).

The issue is that this Node.js server uses a very old version of Swagger UI. More specifically, it uses the oas3-tools package v. 1.0.1, which has Swagger UI v. 3.3.1 bundled with it. The latest Swagger UI version (as of this writing) is 3.22.1, and it does not have the described issue.

The solution is to update Swagger UI to the latest version:

  • There's an existing issue in the oas3-tools repository to update the bundled Swagger UI to the latest version. Consider submitting a PR to fix this.

  • As a quick fix, you can fork oas3-tools, update the bundled Swagger UI files in the <oas3-tools>\middleware\swagger-ui folder, and then update the package.json of the generated Node.js server to refer to your oas3-tools fork instead.

Upvotes: 1

Related Questions