Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, November 24, 2016

best way to tell swaggerui where the host is

best way to tell swaggerui where the host is


When I build my swagger.json file I do not know which host to use. However I can work it out when my page that hosts swaggerui loads (in fact I might want to offer the user a choice). I hoped to see an options.host on the config for the swaggerUI object - I dont see one. Is there an existing way of doing this that I cant find or do I simply have to hack my way through the code and add this capability (pointers to the best place to do it would be welcome)

Answer by Fitch for best way to tell swaggerui where the host is


Swagger has a built-in json definition for host config, or can accept multiple inputs.

{      "swagger": "2.0",      "info": {          "title": "Why API",          "description": "Don't make that mistake again",          "version": "0.0.1"      },        "host": "127.0.0.1:3000",      "schemes": [          "https"      ]  }  

Or

"host": "test.mydomain.com:3000",  "schemes": [      "https"  ],  

Or you can have a dynamic host by defining a var and calling a hostname or machine name or other environment variables.

dynamic example

if (typeof this.host === 'undefined' || this.host === '') {    this.host = location.host;  }  if (location.port) {    this.host = this.host + ':' + location.port;  }  

Answer by pm100 for best way to tell swaggerui where the host is


two ways

One modify swagger.js so that it accepts host option. swagger-UI passes options to swagger-js so that works. I submitted a pull to swagger-js with this fix

Second choice is that swagger-UI accepts a 'spec' parameter. This means that the hosting page can load the swagger.json file, JSON.parse it , set 'host' in it and then pass to swaggerUi constructor. This is harder for the caller but doesn't require code changes to swagger

Answer by Chris Crewdson for best way to tell swaggerui where the host is


Here is what I do, since the loaded in document is just a JSON object:

var swaggerDoc = require('./api/swagger.json');  if (process.env.NODE_ENV === 'development') {    swaggerDoc.host="localhost:" + process.env.PORT  }    // Initialize the Swagger middleware  swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {    // Other initialization  }  

This way you don't pollute your API specification with development environment configuration.

Answer by Eduardo Andrade for best way to tell swaggerui where the host is


This is how I did this using the Java client:

DefaultApi api = new DefaultApi();  api.getApiClient().setBasePath("http://localhost:8080");  //call the API  

I hope it helps!

Answer by Teyras for best way to tell swaggerui where the host is


In recent versions of Swagger UI it's possible to do this, for example in onComplete:

window.swaggerUi.api.setHost("your.host:4242");


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.