Thursday, November 2, 2017

Change swagger 2.0 docs path url

Leave a Comment

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); 

1 Answers

Answers 1

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"    } } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment