Skip to content

Commit 6c9fc89

Browse files
authored
Merge pull request #935 from swagger-api/issue-825
Support allowEmptyValues
2 parents 6530b7d + 45b48e6 commit 6c9fc89

File tree

5 files changed

+130
-7
lines changed

5 files changed

+130
-7
lines changed

browser/swagger-client.js

Lines changed: 28 additions & 3 deletions
Large diffs are not rendered by default.

browser/swagger-client.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/types/operation.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,26 @@ Operation.prototype.urlify = function (args, maskPasswords) {
539539
} else if (param.in === 'formData') {
540540
formParams[param.name] = args[param.name];
541541
}
542+
} else if(param.in === 'query' && typeof args[param.name] === 'undefined' && param.allowEmptyValue === true) {
543+
if (querystring === '' && requestUrl.indexOf('?') < 0) {
544+
querystring += '?';
545+
} else {
546+
querystring += '&';
547+
}
548+
549+
if (typeof param.collectionFormat !== 'undefined' || param.type === 'array') {
550+
var qp;
551+
var collectionFormat = param.collectionFormat || 'multi';
552+
553+
if (Array.isArray(qp)) {
554+
querystring += this.encodeQueryCollection(collectionFormat, param.name, qp, isPassword);
555+
} else {
556+
querystring += this.encodeQueryCollection(collectionFormat, param.name, [qp], isPassword);
557+
}
558+
} else {
559+
querystring += this.encodeQueryKey(param.name) + '=' + this.encodeQueryParam('', isPassword);
560+
}
561+
542562
}
543563
}
544564
var url = this.scheme + '://' + this.host;
@@ -1222,7 +1242,12 @@ Operation.prototype.encodeQueryParam = function (arg, maskPasswords) {
12221242
if(maskPasswords) {
12231243
return "******";
12241244
}
1225-
return encodeURIComponent(arg);
1245+
if(arg) {
1246+
return encodeURIComponent(arg);
1247+
}
1248+
else {
1249+
return '';
1250+
}
12261251
};
12271252

12281253
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
],
1010
"description": "swagger-client is a javascript client for use with swaggering APIs.",
11-
"version": "2.1.30",
11+
"version": "2.1.31",
1212
"homepage": "http://swagger.io",
1313
"repository": {
1414
"type": "git",

test/client.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,4 +1583,77 @@ describe('SwaggerClient', function () {
15831583
done(exception);
15841584
});
15851585
});
1586+
1587+
it('honors allowEmptyValue #825', function(done) {
1588+
var spec = {
1589+
basePath: '/double/',
1590+
paths: {
1591+
'/foo': {
1592+
get: {
1593+
tags: [
1594+
'test'
1595+
],
1596+
operationId: 'slash',
1597+
parameters: [{
1598+
in: 'query',
1599+
allowEmptyValue: true,
1600+
name: 'happy',
1601+
type: 'string',
1602+
required: false
1603+
}],
1604+
}
1605+
}
1606+
}
1607+
};
1608+
new SwaggerClient({
1609+
url: 'http://localhost:8000/v2/swagger.json',
1610+
spec: spec,
1611+
usePromise: true
1612+
}).then(function(client) {
1613+
var mock = client.test.slash({}, {mock: true});
1614+
expect(mock.url).toBe('http://localhost:8000/double/foo?happy=');
1615+
1616+
done();
1617+
}).catch(function(exception) {
1618+
done(exception);
1619+
});
1620+
});
1621+
1622+
it('honors allowEmptyValue #825 in arrays', function(done) {
1623+
var spec = {
1624+
basePath: '/double/',
1625+
paths: {
1626+
'/foo': {
1627+
get: {
1628+
tags: [
1629+
'test'
1630+
],
1631+
operationId: 'slash',
1632+
parameters: [{
1633+
in: 'query',
1634+
allowEmptyValue: true,
1635+
name: 'happy',
1636+
type: 'array',
1637+
items: {
1638+
type: 'string'
1639+
},
1640+
required: false
1641+
}],
1642+
}
1643+
}
1644+
}
1645+
};
1646+
new SwaggerClient({
1647+
url: 'http://localhost:8000/v2/swagger.json',
1648+
spec: spec,
1649+
usePromise: true
1650+
}).then(function(client) {
1651+
var mock = client.test.slash({}, {mock: true});
1652+
expect(mock.url).toBe('http://localhost:8000/double/foo?happy=');
1653+
1654+
done();
1655+
}).catch(function(exception) {
1656+
done(exception);
1657+
});
1658+
});
15861659
});

0 commit comments

Comments
 (0)