Skip to content

Instantly share code, notes, and snippets.

@bitcloud
Created September 14, 2016 18:33
Show Gist options
  • Save bitcloud/795dec2220d1157d729d8c8451ccf994 to your computer and use it in GitHub Desktop.
Save bitcloud/795dec2220d1157d729d8c8451ccf994 to your computer and use it in GitHub Desktop.
swagger nested controllers issue
// store in controllers/test.js
"use strict";
module.exports.test = function createToken(req, res, next) {
res.end("OK");
};
"use strict";
var app = require("connect")();
var http = require("http");
var swaggerTools = require("swagger-tools");
var jsyaml = require("js-yaml");
var fs = require("fs");
var serverPort = 3000;
// swaggerRouter configuration
var options = {
swaggerUi: "/swagger.json",
controllers: "./controllers",
useStubs: process.env.NODE_ENV === "development" ? true : false
};
var spec = fs.readFileSync("./swagger.yaml", "utf8");
var swaggerDoc = jsyaml.safeLoad(spec);
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function(middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
app.use(middleware.swaggerRouter(options));
app.use(middleware.swaggerUi());
// Start the server
http.createServer(app).listen(serverPort, function() {
console.log("Your server is listening on port %d (http://localhost:%d)", serverPort, serverPort);
console.log("Swagger-ui is available on http://localhost:%d/docs", serverPort);
});
});
{
"name": "swtoissue",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"connect": "^3.2.0",
"js-yaml": "^3.3.0",
"swagger-tools": "https://github.com/apigee-127/swagger-tools.git"
},
"author": "",
"license": "ISC"
}
---
swagger: "2.0"
info:
description: "test"
version: "1.0.0"
title: "test"
consumes:
- "application/json"
produces:
- "application/json"
paths:
/test:
get:
summary: "test"
description: "test"
operationId: "test"
produces:
- "application/json"
responses:
200:
description: "Login sucessful."
default:
description: "Unexpected error"
x-swagger-router-controller: "test"
@bitcloud
Copy link
Author

this demonstrates the issues with the nested folders.

  • without any patch it doesn't start.
  • patch for resolving the path: file = path.resolve(child); in swagger-router.js:83 (should be better there instead of 89)
  • now it uses controllers as part of the handlerId

@whitlockjc
Copy link

Oh...so you're saying that right now, controllers, or whatever base search path was used`, is included in the controller name and it shouldn't be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment