Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] no way to create multiple instances of jsdocSwagger for different apis in a single application #167

Closed
paulish opened this issue Jun 23, 2021 · 1 comment · Fixed by #171
Labels
bug Something isn't working enhancement New feature or request good first issue Good for newcomers

Comments

@paulish
Copy link
Contributor

paulish commented Jun 23, 2021

Describe the bug
The second call to expressJSDocSwagger(app) returns the same instance as the first call.

To Reproduce

I wrote the following code to create api:

 const initRoutes = (basePath, docUri, routes) => new Promise(resolve => {
        const options = {
            info: {
                version: '2.0.0',
                title: 'some title',
                description: 'some description'
            },
            baseDir: __dirname,
            filesPattern: `${basePath}/*.js`,
            swaggerUIPath: docUri,
            apiDocsPath: `/v3${docUri}`,
            exposeSwaggerUI: true,
            exposeApiDocs: true,
            notRequiredAsNullable: true,
            swaggerUiOptions: {}
        };
        const instance = expressJSDocSwagger(app)(options);
        instance.on('finish', data => {
            init(data);
            for (const route of routes)
                app.use(route, require(`${basePath}${route === '/' ? '/index' : route}`));

            resolve();
        });
    });

And I call it this way:

   // regular api
   await initRoutes('./routes', '/api-docs', [
        '/', '/auth', '/clinic', ...
    ]);
    // doctor api
    await initRoutes('./doctorRoutes', '/api-docs-doctor', [
        '/doctor'
    ]);

The second call runs 'finish' event twice for the first group of routes and for the second. This happens since swaggerEvents.js does not create the second instance if instance has been already created:

const swaggerEvents = () => {
  if (api) return api; // <-- this line
  ...

Expected behavior

A new instance should be created.

@kevinccbsg kevinccbsg added bug Something isn't working enhancement New feature or request good first issue Good for newcomers labels Jul 19, 2021
@kevinccbsg
Copy link
Member

Hi, @paulish I think you're totally right.

We have to fix that in the swaggerEvents file as we are using always the same instance.

Apart from that, we have an example about this here -> https://github.com/BRIKEV/express-jsdoc-swagger/tree/master/examples/configuration/multipleInstance

Does that example help you?

We will change that single instance in the next release 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants