Skip to content

Commit c076f20

Browse files
author
Dhwaneet Bhatt
committed
Fix an issue where % was getting double encoded in query params
1 parent 989648d commit c076f20

File tree

25 files changed

+542
-54
lines changed

25 files changed

+542
-54
lines changed

codegens/curl/lib/util.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ var self = module.exports = {
207207
},
208208

209209
/**
210-
* Encode param except the following characters- [,{,},]
210+
* Encode param except the following characters- [,{,},],%
211211
*
212212
* @param {String} param
213213
* @returns {String}
@@ -218,6 +218,7 @@ var self = module.exports = {
218218
.replace(/%7B/g, '{')
219219
.replace(/%5D/g, ']')
220220
.replace(/%7D/g, '}')
221+
.replace(/%25/g, '%')
221222
.replace(/'/g, '%27');
222223
},
223224

codegens/curl/test/unit/convert.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,13 @@ describe('curl convert function', function () {
650650
expect(outputUrlString).to.equal('https://postman-echo.com/get?key1={{value}}&key2=%27a%20b%20c%27');
651651
});
652652

653+
it('should not encode query params that are already encoded', function () {
654+
rawUrl = 'https://postman-echo.com/get?query=urn%3Ali%3Afoo%3A62324';
655+
urlObject = new sdk.Url(rawUrl);
656+
outputUrlString = getUrlStringfromUrlObject(urlObject);
657+
expect(outputUrlString).to.equal('https://postman-echo.com/get?query=urn%3Ali%3Afoo%3A62324');
658+
});
659+
653660
it('should discard disabled query params', function () {
654661
urlObject = new sdk.Url({
655662
protocol: 'https',

codegens/golang/lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var _ = require('./lodash'),
33
sanitizeMultiline = require('./util').sanitizeMultiline,
44
sanitizeOptions = require('./util').sanitizeOptions,
55
addFormParam = require('./util').addFormParam,
6+
getUrlStringfromUrlObject = require('./util').getUrlStringfromUrlObject,
67
isFile = false,
78
self;
89

@@ -243,7 +244,7 @@ self = module.exports = {
243244
}
244245
codeSnippet += `${indent}"net/http"\n${indent}"io/ioutil"\n)\n\n`;
245246

246-
codeSnippet += `func main() {\n\n${indent}url := "${encodeURI(request.url.toString())}"\n`;
247+
codeSnippet += `func main() {\n\n${indent}url := "${getUrlStringfromUrlObject(request.url)}"\n`;
247248
codeSnippet += `${indent}method := "${request.method}"\n\n`;
248249

249250
if (bodySnippet !== '') {

codegens/golang/lib/util.js

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
module.exports = {
1+
const _ = require('./lodash');
2+
3+
const self = module.exports = {
24
/**
3-
* sanitizes input string by handling escape characters eg: converts '''' to '\'\''
4-
* and trim input if required
5-
*
6-
* @param {String} inputString
7-
* @param {Boolean} [trim] - indicates whether to trim string or not
8-
* @returns {String}
9-
*/
5+
* sanitizes input string by handling escape characters eg: converts '''' to '\'\''
6+
* and trim input if required
7+
*
8+
* @param {String} inputString
9+
* @param {Boolean} [trim] - indicates whether to trim string or not
10+
* @returns {String}
11+
*/
1012
sanitize: function (inputString, trim) {
1113
if (typeof inputString !== 'string') {
1214
return '';
@@ -20,13 +22,13 @@ module.exports = {
2022
},
2123

2224
/**
23-
* sanitizes input string by handling escape characters eg: converts '''' to '\'\''
24-
* and trim input if required
25-
*
26-
* @param {String} inputString
27-
* @param {Boolean} [trim] - indicates whether to trim string or not
28-
* @returns {String}
29-
*/
25+
* sanitizes input string by handling escape characters eg: converts '''' to '\'\''
26+
* and trim input if required
27+
*
28+
* @param {String} inputString
29+
* @param {Boolean} [trim] - indicates whether to trim string or not
30+
* @returns {String}
31+
*/
3032
sanitizeMultiline: function (inputString, trim) {
3133
if (typeof inputString !== 'string') {
3234
return '';
@@ -38,6 +40,90 @@ module.exports = {
3840

3941
},
4042

43+
/**
44+
*
45+
* @param {Object} urlObject The request sdk request.url object
46+
* @returns {String} The final string after parsing all the parameters of the url including
47+
* protocol, auth, host, port, path, query, hash
48+
* This will be used because the url.toString() method returned the URL with non encoded query string
49+
* and hence a manual call is made to getQueryString() method with encode option set as true.
50+
*/
51+
getUrlStringfromUrlObject: function (urlObject) {
52+
var url = '';
53+
if (!urlObject) {
54+
return url;
55+
}
56+
if (urlObject.protocol) {
57+
url += (urlObject.protocol.endsWith('://') ? urlObject.protocol : urlObject.protocol + '://');
58+
}
59+
if (urlObject.auth && urlObject.auth.user) {
60+
url = url + ((urlObject.auth.password) ?
61+
urlObject.auth.user + ':' + urlObject.auth.password : urlObject.auth.user) + '@';
62+
}
63+
if (urlObject.host) {
64+
url += urlObject.getHost();
65+
}
66+
if (urlObject.port) {
67+
url += ':' + urlObject.port.toString();
68+
}
69+
if (urlObject.path) {
70+
url += urlObject.getPath();
71+
}
72+
if (urlObject.query && urlObject.query.count()) {
73+
let queryString = self.getQueryString(urlObject);
74+
queryString && (url += '?' + queryString);
75+
}
76+
if (urlObject.hash) {
77+
url += '#' + urlObject.hash;
78+
}
79+
80+
return self.sanitize(url, false);
81+
},
82+
83+
/**
84+
* @param {Object} urlObject
85+
* @returns {String}
86+
*/
87+
getQueryString: function (urlObject) {
88+
let isFirstParam = true,
89+
params = _.get(urlObject, 'query.members'),
90+
result = '';
91+
if (Array.isArray(params)) {
92+
result = _.reduce(params, function (result, param) {
93+
if (param.disabled === true) {
94+
return result;
95+
}
96+
97+
if (isFirstParam) {
98+
isFirstParam = false;
99+
}
100+
else {
101+
result += '&';
102+
}
103+
104+
return result + self.encodeParam(param.key) + '=' + self.encodeParam(param.value);
105+
}, result);
106+
}
107+
108+
return result;
109+
},
110+
111+
/**
112+
* Encode param except the following characters- [,{,},],%
113+
*
114+
* @param {String} param
115+
* @returns {String}
116+
*/
117+
encodeParam: function (param) {
118+
return encodeURIComponent(param)
119+
.replace(/%5B/g, '[')
120+
.replace(/%7B/g, '{')
121+
.replace(/%5D/g, ']')
122+
.replace(/%7D/g, '}')
123+
.replace(/%25/g, '%')
124+
.replace(/'/g, '%27');
125+
},
126+
41127
/**
42128
* sanitizes input options
43129
*

codegens/java-okhttp/lib/okhttp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function convert (request, options, callback) {
189189
if (options.includeBoilerplate) {
190190
headerSnippet = 'import java.io.*;\n' +
191191
'import okhttp3.*;\n' +
192-
'public class main {\n' +
192+
'public class Main {\n' +
193193
indentString + 'public static void main(String []args) throws IOException{\n';
194194
footerSnippet = indentString.repeat(2) + 'System.out.println(response.body().string());\n' +
195195
indentString + '}\n}\n';

codegens/java-okhttp/test/newman/newman.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ var runNewmanTest = require('../../../../test/codegen/newman/newmanTestUtil').ru
44
describe.skip('convert for different request types', function () {
55
var options = {indentCount: 3, indentType: 'Space', includeBoilerplate: true},
66
testConfig = {
7-
compileScript: 'javac -cp *: main.java',
8-
runScript: 'java -cp *: main',
9-
fileName: 'main.java',
7+
compileScript: 'javac -cp *: Main.java',
8+
runScript: 'java -cp *: Main',
9+
fileName: 'Main.java',
1010
skipCollections: ['redirectCollection']
1111
};
1212
runNewmanTest(convert, options, testConfig);

codegens/java-okhttp/test/unit/convert.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('okhttp convert function', function () {
2525
}
2626
snippetArray = snippet.split('\n');
2727
for (var i = 0; i < snippetArray.length; i++) {
28-
if (snippetArray[i].startsWith('public class main {')) {
28+
if (snippetArray[i].startsWith('public class Main {')) {
2929
expect(snippetArray[i + 1].substr(0, 4)).to.equal(SINGLE_SPACE.repeat(4));
3030
expect(snippetArray[i + 1].charAt(4)).to.not.equal(SINGLE_SPACE);
3131
}
@@ -39,7 +39,7 @@ describe('okhttp convert function', function () {
3939
expect.fail(null, null, error);
4040
return;
4141
}
42-
expect(snippet).to.include('import java.io.*;\nimport okhttp3.*;\npublic class main {\n');
42+
expect(snippet).to.include('import java.io.*;\nimport okhttp3.*;\npublic class Main {\n');
4343
});
4444
});
4545

codegens/java-unirest/lib/parseRequest.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ var _ = require('./lodash'),
33
sanitize = require('./util').sanitize;
44

55
/**
6-
* Encode param except the following characters- [,{,},]
6+
* Encode param except the following characters- [,{,},],%
77
*
88
* @param {String} param
99
* @returns {String}
1010
*/
1111
function encodeParam (param) {
1212
return encodeURIComponent(param)
1313
.replace(/%5B/g, '[')
14-
.replace(/%7B/g, '{')
1514
.replace(/%5D/g, ']')
16-
.replace(/%7D/g, '}')
15+
.replace(/%25/g, '%')
1716
.replace(/'/g, '%27');
1817
}
1918

codegens/java-unirest/lib/unirest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function convert (request, options, callback) {
186186
if (options.includeBoilerplate) {
187187
headerSnippet = 'import com.mashape.unirest.http.*;\n' +
188188
'import java.io.*;\n' +
189-
'public class main {\n' +
189+
'public class Main {\n' +
190190
indentString + 'public static void main(String []args) throws Exception{\n';
191191
footerSnippet = indentString.repeat(2) + 'System.out.println(response.getBody());\n' +
192192
indentString + '}\n}\n';

codegens/java-unirest/test/newman/newman.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ var runNewmanTest = require('../../../../test/codegen/newman/newmanTestUtil').ru
33

44
describe('Convert for different types of request', function () {
55
var testConfig = {
6-
runScript: 'java -cp *: main',
7-
compileScript: 'javac -cp *: main.java',
8-
fileName: 'main.java',
6+
runScript: 'java -cp *: Main',
7+
compileScript: 'javac -cp *: Main.java',
8+
fileName: 'Main.java',
99
skipCollections: ['formdataCollection', 'emptyFormdataCollection', 'unsupportedMethods']
1010
},
1111
options = {includeBoilerplate: true};

0 commit comments

Comments
 (0)