Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly encode slashes in path parameters #280

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 22 additions & 36 deletions lib/swagger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var ArrayModel = function(definition) {
this.name = "arrayModel";
this.definition = definition || {};
this.properties = [];

var requiredFields = definition.enum || [];
var innerType = definition.items;
if(innerType) {
Expand Down Expand Up @@ -301,7 +301,7 @@ PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
}
return returnVal;
};
/**
/**
* Resolves a spec's remote references
*/
var Resolver = function (){};
Expand Down Expand Up @@ -1460,22 +1460,8 @@ Operation.prototype.encodeQueryParam = function(arg) {
return encodeURIComponent(arg);
};

/**
* TODO revisit, might not want to leave '/'
**/
Operation.prototype.encodePathParam = function(pathParam) {
var encParts, part, parts, i, len;
pathParam = pathParam.toString();
if (pathParam.indexOf('/') === -1) {
return encodeURIComponent(pathParam);
} else {
parts = pathParam.split('/');
encParts = [];
for (i = 0, len = parts.length; i < len; i++) {
encParts.push(encodeURIComponent(parts[i]));
}
return encParts.join('/');
}
return encodeURIComponent(pathParam);
};

var Model = function(name, definition) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replying this.

return pathParam.toString().split('/').map(encodeURIComponent).join('/')

The above code does exactly the same thing

@fehguy should we merge this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does the same thing as the old code, but not the same thing as

encodeURIComponent(pathParam);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true.

encodeURIComponent('a/b') === "a%2Fb"
'a/b'.split('/').map(encodeURIComponent).join('/') === 'a/b'

if we decided to have an option to allow slash(/), we would need to skip encoding it. Your code skip encoding slash. My suggestion is to make that code shorter and less complex.

Based on @fehguy's comment we need to decide if we want to skip slash in encoding and if the answer to that is yes, we should make this behavior configurable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohsen1, according to the URI template spec:

  • slashes need to be encoded for {foo} (the only variant supported in Swagger 2.0), and
  • slashes don't need to be encoded for {+foo}.

So both can be supported easily and within the same URL once the spec gains support for level 2 URI templating.

Expand Down Expand Up @@ -1721,7 +1707,7 @@ Property.prototype.toString = function() {
}


var options = '';
var options = '';
var isArray = this.schema.type === 'array';
var type;

Expand Down Expand Up @@ -1783,11 +1769,11 @@ Property.prototype.toString = function() {
}

options += optionHtml('Enum', enumString);
}
}

if (options.length > 0)
str = '<span class="propWrap">' + str + '<table class="optionsWrapper"><tr><th colspan="2">' + this.name + '</th></tr>' + options + '</table></span>';

return str;
};

Expand Down Expand Up @@ -1882,7 +1868,7 @@ SwaggerClient.prototype.finish = function() {
this.isBuilt = true;
this.selfReflect();
this.success();
}
}
};

SwaggerClient.prototype.buildFrom1_1Spec = function (response) {
Expand Down Expand Up @@ -2173,11 +2159,11 @@ SwaggerResource.prototype.addOperations = function (resource_path, ops, consumes
o.summary,
o.notes,
type,
responseMessages,
this,
consumes,
produces,
o.authorizations,
responseMessages,
this,
consumes,
produces,
o.authorizations,
o.deprecated);

this.operations[op.nickname] = op;
Expand Down Expand Up @@ -2674,15 +2660,15 @@ SwaggerOperation.prototype.urlify = function (args) {
param = params[i];
if(param.paramType === 'query') {
if (queryParams !== '')
queryParams += '&';
queryParams += '&';
if (Array.isArray(param)) {
var output = '';
for(j = 0; j < param.length; j++) {
if(j > 0)
output += ',';
output += encodeURIComponent(param[j]);
}
queryParams += encodeURIComponent(param.name) + '=' + output;
var output = '';
for(j = 0; j < param.length; j++) {
if(j > 0)
output += ',';
output += encodeURIComponent(param[j]);
}
queryParams += encodeURIComponent(param.name) + '=' + output;
}
else {
if (typeof args[param.name] !== 'undefined') {
Expand Down Expand Up @@ -2751,7 +2737,7 @@ SwaggerOperation.prototype.asCurl = function (args) {
var results = [];
var i;

var headers = SwaggerRequest.prototype.setHeaders(args, {}, this);
var headers = SwaggerRequest.prototype.setHeaders(args, {}, this);
for(i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
if(param.paramType && param.paramType === 'header' && args[param.name]) {
Expand Down Expand Up @@ -3290,4 +3276,4 @@ e.Operation = Operation;
e.Model = Model;
e.addModel = addModel;
e.Resolver = Resolver;
})();
})();