scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.42k stars 225 forks source link

Operation stuck when security is used. #264

Closed mahajanankur closed 3 years ago

mahajanankur commented 3 years ago

I am exploring the security components in the swagger JSON, I am able to see the authorize button and the pop is also loading but when I add a token and authorize it. The APIs are not able to execute and they are stuck in an infinite loop. Please find below the JSON and my configurations.

const app = require('express')();
const config = require('config').get('swagger');
const swaggerUi = require('swagger-ui-express');
const swaggerRoutes = require('./src/swagger');

const port = config.get('port');

const option = {
    openapi: '3.0.0',
    explorer: false,
    info: {
        title: 'Example APIs',
        description: 'Example description',
        termsOfService: "https://example.com/",
        version: '1.0',
        contact: {
            email: "example@example.com"
        },
        license: {
            name: "Apache 2.0",
            url: "http://www.apache.org/licenses/LICENSE-2.0.html"
        }
    },
    produces: ['application/json'],
    basePath: "/v2",
    externalDocs: {
        description: "Find out more about example.",
        url: "https://example.com/"
    },
    tags: [
        {
            name: "exm",
            description: "Monitor, collect, and export data from an instance.",
            externalDocs: {
                description: "Find out more",
                url: "https://example.com"
            }
        }
    ],
    schemes: [
        "https"
    ],
    components: {
        securitySchemes: {
            bearerAuth: {
                type: "http",
                scheme: "bearer",
                bearerFormat: "JWT"
            }
        }
    },
    security: { bearerAuth: [] }
};

if (app.get('env') === 'development' && port !== undefined) {
    app.use('/', swaggerUi.serve, swaggerUi.setup({ ...option, ...swaggerRoutes }));
}

app.listen(port, () => {
    console.log(`Swagger UI is running on http://${config.get('host')}:${port}/`);
});

Browser console logs. swagger_error

Swagger UI screenshot. swagger_error_2

gresas commented 3 years ago

I'm not sure, but security property must be an array.

Try to use:

const option = { openapi: '3.0.0', explorer: false, (...) security: [{ bearerAuth: [] }] };

mahajanankur commented 3 years ago

@gresas really appreciate your help. It worked when I changed the security property to an array.