swagger-api / validator-badge

Validate your Swagger JSON/YAML today!
http://swagger.io
Apache License 2.0
210 stars 85 forks source link

Validation error badge #83

Closed rzachariah closed 8 years ago

rzachariah commented 8 years ago

I used swagger project create/edit to define a new node service. With swagger project verify, I see that my API definition is fine. It also works fine locally and in prod. However, when I deploy to an https endpoint with DNS, I get a validation error badge from the swagger docs page. If I click the badge, I get this message.

{
schemaValidationMessages: [
{
level: "error",
message: "Can't read from file https://digitalexhaust.ezesoft.net/api/collection/api-docs"
}
]
}

I first posted this problem on the swagger-tools issues page. They advised me that I was posting within the wrong project. They also diagnosed that the problem is that my service is on my intranet, and the validation service can't reach it.

Fair enough.

I'm not clear on how to disable swagger validation with my setup. I'm using swagger-express-mw. Could you give me a hint on how to do that?

Here's a snip from package.json.

    "swagger-express-mw": "^0.1.0",
    "swagger-tools": "0.10.1"

This is how I start the server.

'use strict';

const defaultRoutingPath = '/api/history';
const defaultPort = 3000;

var path = require('path');
var SwaggerExpress = require('swagger-express-mw');
var SwaggerUi = require('swagger-tools/middleware/swagger-ui');
var express = require('express');
var app = express();
var subpath = express();
module.exports = app; // for testing

var routingPath = process.env.ROUTING_PATH || defaultRoutingPath;
var port = process.env.PORT || defaultPort;
var host = process.env.HOST || `localhost:${port}`;
var nodeEnv = process.env.NODE_ENV;

var config = {
  appRoot: __dirname, // required config
};

SwaggerExpress.create(config, function(err, swaggerExpress) {
  if (err) { throw err; }

  app.use(routingPath, subpath);

  app.get('/', (req, res) => {
    res.redirect(path.join(routingPath, 'docs'));
  })

  app.get(routingPath, (req, res) => {
    res.redirect(path.join(routingPath, 'docs'));
  })

  configureSwagger(swaggerExpress.runner.swagger)

  // enable SwaggerUI
  subpath.use(swaggerExpress.runner.swaggerTools.swaggerUi({
    url: getApiDocsUrl(),
  }));

  // install middleware
  swaggerExpress.register(app);

  app.listen(port);

  if (swaggerExpress.runner.swagger.paths['/health']) {
    console.log(`try this:\ncurl ${getBaseUrl() + '/health'}`);
  }
});

function configureSwagger(swagger) {
  if (nodeEnv === 'production') {
    swagger.host = host;
    swagger.schemes = ['https'];
  } else {
    swagger.host = `localhost:${port}`;
    swagger.schemes = ['http'];
  }
  swagger.basePath = routingPath;
}

function getBaseUrl() {
  var scheme = 'http';
  if (nodeEnv === 'production') {
    scheme = 'https';
  }
  return `${scheme}://${host}${routingPath}`;
}

function getApiDocsUrl() {
  var baseUrl = getBaseUrl();
  return baseUrl + '/api-docs';
}

var gracefulShutdown = function () {
  console.log("Received kill signal, shutting down gracefully.");
  process.exit();
};

// listen for TERM signal .e.g. kill 
process.on('SIGTERM', gracefulShutdown);

// listen for INT signal e.g. Ctrl-C
process.on('SIGINT', gracefulShutdown);   
webron commented 8 years ago

@rzachariah - I don't want to throw you between projects, so I'll try to answer it here. This is not really related to the badge itself, but rather to the configuration of Swagger-UI, specifically SwaggerUi's initialization.

One of the initialization parameters for SwaggerUi is the validatorUrl. Setting it to null would disable the validator.

rzachariah commented 8 years ago

Hmm. I tried nulling that out like this.

  // enable SwaggerUI
  subpath.use(swaggerExpress.runner.swaggerTools.swaggerUi({
    url: getApiDocsUrl(),
    validatorUrl: null
  }));

No dice. :(

webron commented 8 years ago

I don't know what version of swagger-ui swagger-express-mw uses, and this could be some other issue with swagger-express-mw itself... don't know how to help with that I'm afraid.

rzachariah commented 8 years ago

Thanks. Interestingly, I haven't made any changes, but the validation error badge is suddenly gone today.

webron commented 8 years ago

Could be browser cache that didn't really reload the changes. Can we close the ticket?

rzachariah commented 8 years ago

Go for it. Thanks.