John
John

Reputation: 203

Edit the swagger.json before loading in the UI

Is there a way to edit the swagger.json before loading it in the ui. My requirement is to manipulate the swagger.json before loading it in the UI, are there any hooks provided by swagger team to accomplish this

Upvotes: 2

Views: 2430

Answers (1)

Mike
Mike

Reputation: 11545

You should be able to manipulate the JSON before it is loaded onto the UI.

The /api-docs endpoint is what Swagger calls to serve the documentation. You can implement custom functionality to take place when this endpoint is served.

For example (in Node.JS):

app.get('/api-docs', function(req, res) {
  // manipulate JSON here
  res.json(<return manipulated JSON>);
});

You can look in the SwaggerUI index.js code to see where this is happening.

Swagger UI - index.js

var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
  url = decodeURIComponent(url[1]);
} else {
  url = "/api-docs";
}  

The default url is /api-docs so you can just override that. Also, you can break out your Swagger docs into multiple endpoints by adding a query parameter url with the value of your endpoint.

For example, a URL to a second set of Swagger docs for your API could be

www.mydomain.com?url=/second-set-of-docs

Upvotes: 3

Related Questions