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.4k stars 225 forks source link

Cannot load two different API specs #353

Open ronaldl29 opened 1 year ago

ronaldl29 commented 1 year ago
          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 https://github.com/scottie1984/swagger-ui-express/issues/305#issuecomment-1634463465

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 commented 1 year ago

Have you tried this https://github.com/scottie1984/swagger-ui-express#two-swagger-documents

ronaldl29 commented 1 year ago

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 commented 1 year ago

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

talha-ah commented 9 months ago

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

talha-ah commented 9 months ago

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 commented 2 months ago

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