Fixed issue where invalid deserialisation was happening for param style matrix

This commit is contained in:
Vishal Shingala
2020-07-20 16:02:28 +05:30
parent 4eb820014d
commit 4ee4e36c5b
2 changed files with 103 additions and 2 deletions

View File

@@ -2410,8 +2410,8 @@ module.exports = {
// decide explodable params, starting value and separators between key-value and properties for serialisation
switch (style) {
case 'matrix':
isExplodable = true;
startValue = ';' + (paramSchema.type === 'object' ? '' : (paramName + _.isEmpty(paramValue) ? '' : '='));
isExplodable = paramSchema.type === 'object' || explode;
startValue = ';' + ((paramSchema.type === 'object' && explode) ? '' : (paramName + '='));
propSeparator = explode ? ';' : ',';
keyValueSeparator = explode ? '=' : ',';
break;

View File

@@ -2023,6 +2023,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
done();
});
});
describe('findCollectionVariablesFromPath function', function() {
it('should convert a url with scheme and path variables', function(done) {
@@ -2069,6 +2070,106 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
});
});
describe('deserialiseParamValue function', function() {
// As for style=form/deepObject and explode=true, value is split across different key-value pairs in query params
// avoiding them in testing (for this values deserialiseParamValue() will not be called)
var allUniqueParams = [
{ style: 'matrix', in: 'path', explode: false },
{ style: 'matrix', in: 'path', explode: true },
{ style: 'label', in: 'path', explode: false },
{ style: 'label', in: 'path', explode: true },
{ style: 'form', in: 'query', explode: false },
// { style: 'form', in: 'query', explode: true },
{ style: 'simple', in: 'header', explode: false },
{ style: 'simple', in: 'header', explode: true },
{ style: 'spaceDelimited', in: 'query', explode: false },
{ style: 'pipeDelimited', in: 'query', explode: false }
// { style: 'deepObject', in: 'query', explode: true }
];
// assign common values to all params
_.forEach(allUniqueParams, (param) => {
param.name = 'color';
param.required = true;
});
it('should work for primitive type params', function() {
var paramValues = [ // explode doesn't matter for primitive values
';color=blue', ';color=blue',
'.blue', '.blue',
'blue', 'blue'
],
deserialisedParamValue = 'blue';
// First 5 combinations in allUniqueParams are allowed as per OpenAPI specifications for primitive data type
_.forEach(allUniqueParams.slice(0, 5), (param, index) => {
let retVal = SchemaUtils.deserialiseParamValue(param, paramValues[index], 'REQUEST', {}, {});
expect(retVal).to.eql(deserialisedParamValue);
});
});
it('should work for different styles of arrays', function() {
var paramValues = [
';color=blue,black,brown',
';color=blue;color=black;color=brown',
'.blue.black.brown',
'.blue.black.brown',
'blue,black,brown',
'blue,black,brown',
'blue,black,brown',
'blue black brown',
'blue|black|brown'
],
deserialisedParamValue = ['blue', 'black', 'brown'];
// assign schema values to all params
_.forEach(allUniqueParams, (param) => {
param.schema = {
type: 'array',
items: { type: 'integer' }
};
});
// All combinations in allUniqueParams are allowed as per OpenAPI specifications for array data type
_.forEach(allUniqueParams, (param, index) => {
let retVal = SchemaUtils.deserialiseParamValue(param, paramValues[index], 'REQUEST', {}, {});
expect(retVal).to.eql(deserialisedParamValue);
});
});
it('should work for different styles of objects', function() {
var paramValues = [
';color=R,100,G,200,B,150',
';R=100;G=200;B=150',
'.R.100.G.200.B.150',
'.R=100.G=200.B=150',
'R,100,G,200,B,150',
'R,100,G,200,B,150',
'R=100,G=200,B=150',
'R 100 G 200 B 150',
'R|100|G|200|B|150'
],
deserialisedParamValue = { 'R': 100, 'G': 200, 'B': 150 };
// assign schema values to all params
_.forEach(allUniqueParams, (param) => {
param.schema = {
type: 'object',
properties: {
R: { type: 'integer' },
G: { type: 'integer' },
B: { type: 'integer' }
}
};
});
// All combinations in allUniqueParams are allowed as per OpenAPI specifications for object data type
_.forEach(allUniqueParams, (param, index) => {
let retVal = SchemaUtils.deserialiseParamValue(param, paramValues[index], 'REQUEST', {}, {});
expect(retVal).to.eql(deserialisedParamValue);
});
});
});
});
describe('Get header family function ', function() {