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

Resolve types in Query params and Query string #9

Merged
merged 2 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
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
29 changes: 19 additions & 10 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,17 @@ helper = {
* Used to get url query string constructed from raml query parameters
*
* @param {Object} queryParameters - query parameters extracted from raml
* @param {Object} types - raml types map
*
* @returns {string} query string
*/
constructQueryStringFromQueryParams: function(queryParameters) {
constructQueryStringFromQueryParams: function(queryParameters, types) {
let SDKUrl = new SDK.Url();

_.forEach(queryParameters, (param, key) => {
SDKUrl.addQueryParams(new SDK.QueryParam({
key: key,
value: param.default || param.example || ''
value: param.default || param.example || safeSchemaFaker(param, types) || ''
}));
});

Expand All @@ -293,20 +294,28 @@ helper = {
* @returns {string} query string
*/
constructQueryString: function (queryString, types) {
let typeArray = queryString.type,
SDKUrl = new SDK.Url();
let SDKUrl = new SDK.Url(),
fakedQueryString;

if (typeof queryString === 'string') {
return queryString;
}

_.forEach(typeArray, (type) => {
_.forEach(_.get(types, type, {}).properties, (property, propertyKey) => {
SDKUrl.addQueryParams(propertyKey);
fakedQueryString = safeSchemaFaker(queryString, types);

if (_.isObject(fakedQueryString)) {
_.forEach(fakedQueryString, (value, key) => {
SDKUrl.addQueryParams(new SDK.QueryParam({
key: key,
value: value
}));
});
});
}
else {
SDKUrl.addQueryParams(fakedQueryString);
}

return SDKUrl.getQueryString();
return ('?' + SDKUrl.getQueryString());

},

Expand Down Expand Up @@ -411,7 +420,7 @@ helper = {

method.is && (method = addTraitsToMethod(method, globalParameters.traits));
if (method.queryParameters) {
url = url.concat(helper.constructQueryStringFromQueryParams(method.queryParameters));
url = url.concat(helper.constructQueryStringFromQueryParams(method.queryParameters, globalParameters.types));
}
if (method.queryString) {
url = url.concat(helper.constructQueryString(method.queryString, globalParameters.types));
Expand Down
32 changes: 29 additions & 3 deletions test/unit/converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,36 @@ describe('helper functions', function() {
});

it('should construct query string', function() {
let queryString = '?page&per_page',
modifiedQueryString = helper.constructQueryString(queryString, {});
let queryString = {
'name': 'queryString',
'type': [
'madeuptype'
]
},
types = {
'madeuptype': {
'type': [
'object'
],
'properties': {
'page': {
'name': 'page',
'type': [
'integer'
]
},
'per_page': {
'name': 'per_page',
'type': [
'integer'
]
}
}
}
},
modifiedQueryString = helper.constructQueryString(queryString, types);

expect(modifiedQueryString).to.equal('?page&per_page');
expect(modifiedQueryString).to.equal('?page=<integer>&per_page=<integer>');
});

it('should return contentType header', function() {
Expand Down