From d5992d19e587084e11fd7357cfa885919ddf5830 Mon Sep 17 00:00:00 2001 From: abhijitkane Date: Sun, 16 Dec 2018 17:29:45 +0530 Subject: [PATCH] Fix for https://github.com/postmanlabs/openapi-to-postman/issues/5 - headers that have refs to deeper components are correctly generated --- lib/util.js | 7 ++-- test/unit/base.test.js | 1 - test/unit/util.test.js | 84 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/lib/util.js b/lib/util.js index 545e19f17..072eca4cb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1062,8 +1062,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; @@ -1115,8 +1116,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; } diff --git a/test/unit/base.test.js b/test/unit/base.test.js index 424b4b343..86262d25e 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -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)); diff --git a/test/unit/util.test.js b/test/unit/util.test.js index dd9a089fb..0292abd4c 100644 --- a/test/unit/util.test.js +++ b/test/unit/util.test.js @@ -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 = { @@ -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': { @@ -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() {