Skip to content

Commit d8c80f3

Browse files
author
Johny Lam
committed
require base path. match swagger json endpoints based on the full path which may include any parts from the basePath after the host.
1 parent 174580e commit d8c80f3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/swagger-express/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var fs = require('fs');
44
var path = require('path');
55
var yaml = require('js-yaml');
66
var coffee = require('coffee-script');
7+
var url = require('url');
78

89
var doctrine = require('doctrine');
910
var express = require('express');
@@ -218,7 +219,11 @@ function generate(opt) {
218219
throw new Error('\'swaggerUI\' is required.');
219220
}
220221

221-
if(opt.basePath) descriptor.basePath = opt.basePath;
222+
if (!opt.basePath) {
223+
throw new Error('\'basePath\' is required.');
224+
}
225+
226+
descriptor.basePath = opt.basePath;
222227
descriptor.apiVersion = (opt.apiVersion) ? opt.apiVersion : '1.0';
223228
descriptor.swaggerVersion = (opt.swaggerVersion) ? opt.swaggerVersion : '1.0';
224229
descriptor.swaggerURL = (opt.swaggerURL) ? opt.swaggerURL : '/swagger';
@@ -229,6 +234,7 @@ function generate(opt) {
229234
opt.swaggerVersion = descriptor.swaggerVersion;
230235
opt.swaggerURL = descriptor.swaggerURL;
231236
opt.swaggerJSON = descriptor.swaggerJSON;
237+
opt.fullSwaggerJSONPath = url.parse(opt.basePath + opt.swaggerJSON).path;
232238

233239
if (opt.apis) {
234240
opt.apis.forEach(function (api) {
@@ -273,7 +279,7 @@ exports.init = function (app, opt) {
273279

274280
return function (req, res, next) {
275281
var match, resource, result;
276-
var regex = new RegExp('^'+ opt.swaggerJSON +'(\/.*)?$');
282+
var regex = new RegExp('^'+ opt.fullSwaggerJSONPath +'(\/.*)?$');
277283

278284
match = regex.exec(req.path);
279285

0 commit comments

Comments
 (0)