Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change swagger 2.0 docs path url

This is how I configure swagger :

 const openapi = Openapi.initialize({
    paths: openApiPaths,
    app,
    apiDoc,
  });
  const openApiSpec = openapi.apiDoc;

  console.log(openApiSpec);

  app.use(swaggerUI(openApiSpec));

How can I change the base path /docs/ to /projectName/docs/ ?

I did not find any relevant answer to that

EDIT

My api doc is described as below in it's own file:

export const apiDoc = {
  'x-express-openapi-additional-middleware': [checkBodyValidity],
  swagger: '2.0',
  basePath: '/api/v1',
  info: {
    title: 'Documentation Rest API',
    version: 'v1',
  },
  paths: {},
  definitions: {}
}

CheckBodyValidity is kind of a middleware that check request params validity (not relevant for my question):

export const checkBodyValidity: any = (req, res, next) => {}

Swagger is initialized as below in a file named openapiSetup :

export async function init(app: any): Promise<any> {

[...]

  const openapi = Openapi.initialize({
    paths: openApiPaths,
    app,
    apiDoc,
  });
  const openApiSpec = openapi.apiDoc;

  app.use(swaggerUI(openApiSpec));

}

-> openApiPaths is the path{} part if the doc. It's constructed from directories and file names

Finally in express app :

await openapiSetup.init(app);
like image 650
An-droid Avatar asked Oct 17 '22 04:10

An-droid


1 Answers

What do you have in your YAML? Should be able to update your path by modifying:

# Relative URL to external documentation
externalDocs:
  url: /docs
  description: Find more info here

More info [here][1]

Edit:

Did you try adding the following to your apiDoc object.

export const apiDoc = {
  'x-express-openapi-additional-middleware': [checkBodyValidity],
  swagger: '2.0',
  basePath: '/api/v1',
  info: {
    title: 'Documentation Rest API',
    version: 'v1',
  },
  paths: {},
  definitions: {},
  externalDocs: {
     description: "Docs",
     url: "http://url/projectName/docs"
   }
}
like image 71
dzm Avatar answered Oct 21 '22 00:10

dzm