Created
September 14, 2016 18:33
-
-
Save bitcloud/795dec2220d1157d729d8c8451ccf994 to your computer and use it in GitHub Desktop.
swagger nested controllers issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// store in controllers/test.js | |
"use strict"; | |
module.exports.test = function createToken(req, res, next) { | |
res.end("OK"); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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" |
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
this demonstrates the issues with the nested folders.
file = path.resolve(child);
in swagger-router.js:83 (should be better there instead of 89)controllers
as part of the handlerId