Skip to content

Commit 000e6c4

Browse files
committed
Merge pull request #92 from rutchkiwi/master
Fix for base path "/"
2 parents 229da65 + 24eb00d commit 000e6c4

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/swagger.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,20 +372,24 @@ var SwaggerResource = function(resourceObj, api) {
372372
}
373373
}
374374

375-
SwaggerResource.prototype.getAbsoluteBasePath = function(relativeBasePath) {
376-
var parts, pos, url;
375+
SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) {
376+
var pos, url;
377377
url = this.api.basePath;
378378
pos = url.lastIndexOf(relativeBasePath);
379-
if (pos === -1) {
380-
parts = url.split("/");
381-
url = parts[0] + "//" + parts[2];
379+
var parts = url.split("/");
380+
var rootUrl = parts[0] + "//" + parts[2];
381+
//if the relative path is '/' return the root url
382+
if (relativeBasePath === '/'){
383+
return rootUrl
384+
}
385+
//if the relative path is not in the base path
386+
else if (pos === -1 ) {
382387
if (relativeBasePath.indexOf("/") === 0) {
383388
return url + relativeBasePath;
384389
} else {
385390
return url + "/" + relativeBasePath;
386391
}
387-
} else if (relativeBasePath === "/") {
388-
return url.substring(0, pos);
392+
//If the relative path is in the base path
389393
} else {
390394
return url.substring(0, pos) + relativeBasePath;
391395
}

src/swagger-model-spec.coffee

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ describe 'Swagger models version 1.2 spec', ->
5353
pet = window.swagger.apis.pet.models["Pet"]
5454
sample = pet.createJSONSample()
5555
log sample
56-
log JSON.stringify sample
56+
log JSON.stringify sample
57+
58+
it "verifies the getAbsoluteBasePath method for relativeBasePath '/'", ->
59+
window.SwaggerResource.prototype.api = {basePath : "http://localhost:8000/api-docs/"}
60+
expect(window.SwaggerResource.prototype.getAbsoluteBasePath("/")).toBe("http://localhost:8000")

0 commit comments

Comments
 (0)