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

Cannot load two different API specs #353

Open
ronaldl29 opened this issue Jul 13, 2023 · 6 comments
Open

Cannot load two different API specs #353

ronaldl29 opened this issue Jul 13, 2023 · 6 comments

Comments

@ronaldl29
Copy link

ronaldl29 commented Jul 13, 2023

          I'm running into the same issue as well. Thank you @danielnmai for the workaround.

However, is there a way to do this with 2 different URLs (without the explorer dropdown?)

Originally posted by @ronaldlong46 in #305 (comment)

Loading two separate API specs at two separate URLs like this:

app.use('/api-external', swaggerUi.serve, swaggerUi.setup(externalSwaggerDocs, config.swaggerUiOptions));
app.use('/api-doc', swaggerUi.serve, swaggerUi.setup(swaggerDocs, config.swaggerUiOptions));

It will only show whichever API specs is last for both routes.

@scottie1984
Copy link
Owner

@ronaldl29
Copy link
Author

With the way I have things set up currently with my Node.js application, I can't switch over to JSON for loading the swagger documents. Are you interested in having this bug fixed? I may be able to put some time in to investigate the issue @scottie1984

@scottie1984
Copy link
Owner

@ronaldlong46 yeah - if you can that would be great.

@talha-ah
Copy link

I have the same issue and I am unable to load two separate API specs. @ronaldlong46, @scottie1984 any update on the issue?

@talha-ah
Copy link

talha-ah commented Oct 16, 2023

I was able to have separate endpoints for separate specs.

const path = require("path");
const swaggerDocs = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");

const SPEC_1 = {
  definition: {
    openapi: "3.0.1",
    info: {
      version: "1.0.0",
      title: "API",
      description: "Dscription",
    },
    servers: [
      {
        url: "http://localhost:3000",
        description: "Development server",
      },
    ],
    paths: {
      "/auth/login": {
        post: {
          tags: ["auth"],
          summary: "User Login",
          description: "User Login",
          operationId: "Login",
          requestBody: {
            description: "User Login",
            content: {
              "application/json": {
                schema: {
                  type: "object",
                  properties: {
                    email: {
                      type: "string",
                    },
                    password: {
                      type: "string",
                    },
                  },
                },
              },
            },
          },
          responses: {
            200: {
              description: "Success",
              content: {},
            },
            400: {
              description: "Error",
              content: {},
            },
          },
        },
      },
    },
  },
  apis: [path.join(__dirname, "index.yaml")],
};

const SPEC_2 = {
  definition: {
    openapi: "3.0.1",
    info: {
      version: "1.0.0",
      title: "API 2",
      description: "Dscription 2",
    },
    servers: [
      {
        url: "http://localhost:3000",
        description: "Development server",
      },
    ],
    paths: {
      "/auth/login": {
        post: {
          tags: ["auth"],
          summary: "User Login",
          description: "User Login",
          operationId: "Login",
          requestBody: {
            description: "User Login",
            content: {
              "application/json": {
                schema: {
                  type: "object",
                  properties: {
                    email: {
                      type: "string",
                    },
                    password: {
                      type: "string",
                    },
                  },
                },
              },
            },
          },
          responses: {
            200: {
              description: "Success",
              content: {},
            },
            400: {
              description: "Error",
              content: {},
            },
          },
        },
      },
    },
  },
  apis: [path.join(__dirname, "index.yaml")],
};

const SCHEMA_1 = swaggerDocs(SPEC_1);
const SCHEMA_2 = swaggerDocs(SPEC_2);

app.use("/docs/1",swaggerUi.serveFiles(SCHEMA_1, {}),swaggerUi.setup(SCHEMA_1));
app.use("/docs/2",swaggerUi.serveFiles(SCHEMA_2, {}),swaggerUi.setup(SCHEMA_2),);

Codesandbox: https://codesandbox.io/p/sandbox/swagger-js-docs-c7f6w2?file=%2Findex.js%3A1%2C1-137%2C1

@swkang0513
Copy link

@talha-ah You saved my day! Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants