Skip to content

Commit

Permalink
Merge pull request #6 from postmanlabs/feature/allow-multi-level-comp…
Browse files Browse the repository at this point in the history
…onent-nesting

Fix for /issues/5
  • Loading branch information
abhijitkane committed Dec 17, 2018
2 parents 215c0eb + d5992d1 commit 5cca30f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/util.js
Expand Up @@ -1072,8 +1072,9 @@ module.exports = {
_.forOwn(response.headers, (value, key) => {
if (key !== 'Content-Type') {
if (value.$ref) {
// the convert to PmHeader function handles the
// schema-faking
header = this.getRefObject(value.$ref);
// header.name = value.$ref.split('/').slice(3)[0];
}
else {
header = value;
Expand Down Expand Up @@ -1125,8 +1126,8 @@ module.exports = {
var refObj, savedSchema;

savedSchema = $ref.split('/').slice(2);
// must have 2 segments after "#/components"
if (savedSchema.length !== 2) {
// must have min. 2 segments after "#/components"
if (savedSchema.length < 2) {
console.warn(`ref ${$ref} not found.`);
return null;
}
Expand Down
1 change: 0 additions & 1 deletion test/unit/base.test.js
Expand Up @@ -75,7 +75,6 @@ describe('INTERFACE FUNCTION TESTS ', function () {
});
});
});

describe('The converter must identify invalid specifications', function () {
var pathPrefix = INVALID_OPENAPI_PATH,
sampleSpecs = fs.readdirSync(path.join(__dirname, pathPrefix));
Expand Down
84 changes: 83 additions & 1 deletion test/unit/util.test.js
Expand Up @@ -457,6 +457,44 @@ describe('UTILITY FUNCTION TESTS ', function () {
});
});

describe('getRefObject', function() {
it('Should convert schemas where compnents have refs to other components', function (done) {
Utils.components = {
'responses': {
'TooManyRequests': {
'description': '`Too Many Requests`\n',
'headers': {
'Retry-After': {
'$ref': '#/components/responses/InternalError/headers/Retry-After'
}
}
},
'InternalError': {
'description': '`Internal Error`\n',
'headers': {
'Retry-After': {
'description': 'Some description',
'schema': {
'oneOf': [
{
'type': 'string',
'description': 'A date'
}
]
}
}
}
}
}
};
// deref compnents more than 2 levels deep
var resolvedObject = Utils.getRefObject('#/components/responses/InternalError/headers/Retry-After');
expect(resolvedObject.description).to.equal('Some description');
expect(resolvedObject.schema.oneOf.length).to.equal(1);
done();
});
});

describe('convertParamsWithStyle', function () {
it('should work for string params', function() {
var params = {
Expand Down Expand Up @@ -1303,7 +1341,7 @@ describe('UTILITY FUNCTION TESTS ', function () {
});

describe('convertToPmResponse function', function() {
it('sholud convert response with content field', function(done) {
it('should convert response with content field', function(done) {
var response = {
'description': 'A list of pets.',
'content': {
Expand Down Expand Up @@ -1361,6 +1399,50 @@ describe('UTILITY FUNCTION TESTS ', function () {
});
done();
});
it('should convert headers with refs', function(done) {
Utils.components = {
'responses': {
'TooManyRequests': {
'description': '`Too Many Requests`\n',
'headers': {
'Retry-After': {
'$ref': '#/components/responses/InternalError/headers/Retry-After'
}
}
},
'InternalError': {
'description': '`Internal Error`\n',
'headers': {
'Retry-After': {
'description': 'Some description',
'schema': {
'oneOf': [
{
'type': 'string',
'description': 'A date'
}
]
}
}
}
}
}
};
var response = {
'description': '`Too Many Requests`\\n',
'headers': {
'Retry-After': {
'$ref': '#/components/responses/InternalError/headers/Retry-After'
}
}
},
code = '200',
pmResponse = Utils.convertToPmResponse(response, code, null);

expect(pmResponse.headers.members[0].key).to.equal('Retry-After');
expect(pmResponse.headers.members[0].description).to.equal('Some description');
done();
});
});

describe('fixPathVariablesInUrl function', function() {
Expand Down

0 comments on commit 5cca30f

Please sign in to comment.