diff --git a/lib/swagger.js b/lib/swagger.js index 4be622abd..b29bd60a4 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -321,6 +321,25 @@ } } + SwaggerResource.prototype.getAbsoluteBasePath = function(relativeBasePath) { + var parts, pos, url; + url = this.api.basePath; + pos = url.lastIndexOf(relativeBasePath); + if (pos === -1) { + parts = url.split("/"); + url = parts[0] + "//" + parts[2]; + if (relativeBasePath.indexOf("/") === 0) { + return url + relativeBasePath; + } else { + return url + "/" + relativeBasePath; + } + } else if (relativeBasePath === "/") { + return url.substring(0, pos); + } else { + return url.substring(0, pos) + relativeBasePath; + } + }; + SwaggerResource.prototype.addApiDeclaration = function(response) { var endpoint, _i, _len, _ref; if (response.produces != null) { @@ -330,7 +349,7 @@ this.consumes = response.consumes; } if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) { - this.basePath = response.basePath; + this.basePath = response.basePath.indexOf("http") === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath; } this.addModels(response.models); if (response.apis) { diff --git a/src/swagger.coffee b/src/swagger.coffee index 9859c4609..7f4aae2e2 100644 --- a/src/swagger.coffee +++ b/src/swagger.coffee @@ -251,6 +251,27 @@ class SwaggerResource new SwaggerHttp().execute obj + # Constructs an absolute resource's basePath from relative one, using the @api basePath + # eg. if the @api.basePath = http://myhost.com:8090/mywebapp/v0/api-docs + # and the resource response contains a relative basePath='v0' + # then the function will return 'http://myhost.com:8090/mywebapp/v0' + getAbsoluteBasePath: (relativeBasePath) -> + url = @api.basePath + # first check if the base is a part of given url + pos = url.lastIndexOf(relativeBasePath) + if pos is -1 + # take the protocol, host and port parts only and glue the 'relativeBasePath' + parts = url.split("/") + url = parts[0] + "//" + parts[2] + if relativeBasePath.indexOf("/") is 0 + url + relativeBasePath + else + url + "/" + relativeBasePath + else if (relativeBasePath is "/") + url.substring(0, pos) + else + url.substring(0, pos) + relativeBasePath + addApiDeclaration: (response) -> if response.produces? @produces = response.produces @@ -260,7 +281,7 @@ class SwaggerResource # If there is a basePath in response, use that or else use # the one from the api object if response.basePath? and response.basePath.replace(/\s/g,'').length > 0 - @basePath = response.basePath + @basePath = if response.basePath.indexOf("http") is -1 then @getAbsoluteBasePath(response.basePath) else response.basePath @addModels(response.models)