Skip to content

Commit

Permalink
Handle parsing array fields (apidoc#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
omargho authored and NicolasCARPi committed Jan 12, 2020
1 parent 04fa5e9 commit b5e506a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
4 changes: 2 additions & 2 deletions template/utils/send_sample_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ define([
}
} // for

//handle nested fields
param = utils.handleNestedFields(param, paramType);
//handle nested objects and parsing fields
param = utils.handleNestedAndParsingFields(param, paramType);

//add url search parameter
if (header['Content-Type'] == 'application/json' ){
Expand Down
20 changes: 19 additions & 1 deletion template/utils/send_sample_request_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,23 @@ define([], function () {
return result
}

return {handleNestedFields: handleNestedFieldsForAllParams};
function handleArraysAndObjectFields(param, paramType) {
var result = Object.assign({}, param);
Object.keys(paramType).forEach(function (key) {
if (result[key] && (paramType[key].endsWith('[]') || paramType[key] === 'Object')) {
try {
result[key] = JSON.parse(result[key]);
} catch (e) {;}
}
});
return result
}

function handleNestedAndParsingFields(param, paramType) {
var result = handleArraysAndObjectFields(param, paramType);
result = handleNestedFieldsForAllParams(result, paramType);
return result;
}

return {handleNestedAndParsingFields};
});
85 changes: 82 additions & 3 deletions test/template_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('send sample request utils', function () {
}
};

var result = sendSampleRequestUtils.handleNestedFields(param, paramType);
var result = sendSampleRequestUtils.handleNestedAndParsingFields(param, paramType);
should.deepEqual(result, expectedResult);
done();
});
Expand All @@ -65,7 +65,7 @@ describe('send sample request utils', function () {
'profile.birthday.year': 'Number',
};

var result = sendSampleRequestUtils.handleNestedFields(param, paramType);
var result = sendSampleRequestUtils.handleNestedAndParsingFields(param, paramType);
should.deepEqual(result, param);
done();
});
Expand All @@ -86,9 +86,88 @@ describe('send sample request utils', function () {
'profile.birthday.year': 'Number',
};

var result = sendSampleRequestUtils.handleNestedFields(param, paramType);
var result = sendSampleRequestUtils.handleNestedAndParsingFields(param, paramType);
should.deepEqual(result, param);
done();
});

it('should handle array of strings field', function (done) {
var param = {
names: '["john","doe"]',
};

var paramType = {
names: 'String[]',
};

var expectedResult = {
names: ['john', 'doe'],
};

var result = sendSampleRequestUtils.handleNestedAndParsingFields(param, paramType);
should.deepEqual(result, expectedResult);
done();
});

it('should handle Object and array of objects field', function (done) {
var param = {
data: '{"a":1,"b":"b"}',
arrayData: '[{"a":1},{"a":2}]'
};

var paramType = {
data: 'Object',
arrayData: 'Object[]',
};

var expectedResult = {
data: {a: 1, b: "b"},
arrayData: [{"a": 1}, {"a": 2}]
};

var result = sendSampleRequestUtils.handleNestedAndParsingFields(param, paramType);
should.deepEqual(result, expectedResult);
done();
});

it('should handle nested fields and parse array and object', function (done) {
var param = {
'profile': '',
'profile.name': 'john doe',
'profile.birthday': '',
'profile.birthday.day': 10,
'profile.birthday.year': 2030,
'profile.info': '',
'profile.info.skills': '["js","node"]',
'profile.info.job': '{"company":"piedpiper"}',
};
var paramType = {
'profile': 'Object',
'profile.name': 'String',
'profile.birthday': 'Object',
'profile.birthday.day': 'Number',
'profile.birthday.year': 'Number',
'profile.info': 'Object',
'profile.info.skills': 'String[]',
'profile.info.job': 'Object',
};

var expectedResult = {
profile: {
name: param['profile.name'],
birthday: {
day: param['profile.birthday.day'],
year: param['profile.birthday.year']
},
info: {
skills: ["js", "node"],
job: {company: 'piedpiper'}
}
}
};

var result = sendSampleRequestUtils.handleNestedAndParsingFields(param, paramType);
should.deepEqual(result, expectedResult);
done();
});
});

0 comments on commit b5e506a

Please sign in to comment.