From 579f1bb9bed6f82ea20757f67eed965e78f65fb5 Mon Sep 17 00:00:00 2001 From: Marc Trudel Date: Tue, 23 Apr 2013 19:34:31 +0900 Subject: [PATCH] * Bugfix: was using path instead of resourcePath --- lib/swagger-express/index.js | 54 ++++++++++++++++++++---------------- package.json | 9 +++--- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/lib/swagger-express/index.js b/lib/swagger-express/index.js index 2af5ff4f..bfb1be23 100644 --- a/lib/swagger-express/index.js +++ b/lib/swagger-express/index.js @@ -1,5 +1,5 @@ - var _ = require('underscore'); +var async = require('async'); var fs = require('fs'); var path = require('path'); var yaml = require('js-yaml'); @@ -66,9 +66,11 @@ function getSwagger(fragment, fn) { for (var i = 0; i < fragment.tags.length; i++) { var tag = fragment.tags[i]; if ('swagger' === tag.title) { - yaml.loadAll(tag.description, fn); + return yaml.loadAll(tag.description, fn); } } + + return fn(false); } /** @@ -78,34 +80,36 @@ function getSwagger(fragment, fn) { * @param {Function} fn */ function readJsDoc(file, fn) { - parseJsDocs(file, function (err, doc) { + parseJsDocs(file, function (err, docs) { if (err) { fn(err); } - var resource = {}; - resource.apis = []; + var resource = { apis: [] }; - function onGetSwagger(api) { - if (api.resourcePath) { - descriptor.apis.push(api); - resource.resourcePath = api.resourcePath; - } else if (api.models) { - resource.models = api.models; - } else { - resource.apis.push(api); - } + async.eachSeries(docs, function (doc, cb) { + getSwagger(doc, function (api) { - if (i === doc.length - 1) { - resources[api.path] = resource; - fn(); - } - } + if (!api) { + return cb(); + } - for (var i = 0; i < doc.length; i++) { - getSwagger(doc[i], onGetSwagger); - } + if (api.resourcePath) { + descriptor.apis.push(api); + resource.resourcePath = api.resourcePath; + } else if (api.models) { + resource.models = api.models; + } else { + resource.apis.push(api); + } + + cb(); + }); + }, function (err) { + resources[resource.resourcePath] = resource; + fn(); + }); }); } @@ -211,10 +215,12 @@ exports.init = function (app, opt) { match = regex.exec(req.path); - if (match !== null) { + if (match) { result = _.clone(descriptor); - if (match[1] !== null) { + if (match[1]) { + + console.log(resources); resource = resources[match[1]]; if (!resource) { diff --git a/package.json b/package.json index 355ad4ca..754fc29c 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,11 @@ "author": "fliptoo ", "main": "./index", "dependencies": { - "express": "*" - , "underscore": "*" - , "doctrine": "*" - , "js-yaml": "*" + "express": "*", + "underscore": "*", + "doctrine": "*", + "js-yaml": "*", + "async": "~0.2.7" }, "devDependencies": { "jade": "*",