Dharita Chokshi
Dharita Chokshi

Reputation: 1263

How to represent custom token in header in Swagger UI(swagger.json) in nodejs

I am creating a Restful server in ExpressJs. I have integrated swagger-jsdoc. Following is the related files. Below(header.png) is how I expect my header to look like in swagger UI. But, while I open my swagger UI (http://localhost:3000/api-docs/), I am not able to see Token tags (token and Authentication) in the header.

enter image description here

swagger.json

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Viswa API"
    },
    "host": "localhost:3000",
    "basePath": "/api",
    "tags": [{
        "name": "Customers",
        "description": "API for customers in the system"
    }],
    "schemes": [
        "http"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "securityDefinitions": {
        "Bearer": {
            "type": "apiKey",
            "name": "Authorization",
            "in": "header"
        },
        "JWT": {
            "type": "apiKey",
            "name": "token",
            "in": "header"
        }
    },
    "paths": {
        "/customer": {
            "post": {
                "tags": [
                    "Customers"
                ],
                "description": "Create new customer in system",
                "parameters": [{
                    "name": "customer",
                    "in": "body",
                    "description": "Customer that we want to create",
                    "schema": {
                        "$ref": "#/definitions/Customer"
                    }
                }],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "201": {
                        "description": "New customer is created",
                        "schema": {
                            "$ref": "#/definitions/Customer"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "Customer": {
            "required": [
                "email"
            ],
            "properties": {
                "customer_name": {
                    "type": "string"
                },
                "customer_email": {
                    "type": "string"
                }
            }
        }
    }
}

app.route

var apiRoutes = express.Router();
app.use('/api', apiRoutes);
// swagger definition
var swaggerUi = require('swagger-ui-express'),
swaggerDocument = require('../swagger.json');

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api/v1', apiRoutes);

Current Swagger UI:

Upvotes: 3

Views: 12091

Answers (3)

You can define the header parameters in the path definition. like this

 "paths": {
    "/customer": {
                parameters: [{ name: "Authorization", in: "header", type: "string", description: "auth token" }]

                }
        }

Upvotes: 0

Madhuka Dilhan
Madhuka Dilhan

Reputation: 1416

You can add a security tag

"security": [ { "Bearer": [] } ], 

After adding it your path section

"paths": {
        "/customer": {
            "post": {
                "security": [ { "Bearer": [] } ],
                "tags": [
                    "Customers"
                ],
                "description": "Create new customer in system",
                "parameters": [{
                    "name": "customer",
                    "in": "body",
                    "description": "Customer that we want to create",
                    "schema": {
                        "$ref": "#/definitions/Customer"
                    }
                }],
                "produces": [
                    "application/json"
                ],
                "responses": {
                    "201": {
                        "description": "New customer is created",
                        "schema": {
                            "$ref": "#/definitions/Customer"
                        }
                    }
                }
            }
        }
    },

Upvotes: 0

Hiten Rastogi
Hiten Rastogi

Reputation: 162

You are missing the security tag. You can define it either globally just below the securityDefinitions tag or one for each API endpoint.

Have a look at this question.

Upvotes: 2

Related Questions