From c30c5b5c7d77665a08dd58a8d5a861eff5c9fb1e Mon Sep 17 00:00:00 2001 From: Nick Young Date: Thu, 29 Dec 2016 21:36:57 +1000 Subject: [PATCH] Make securedBy: [ null ] consistent with no security. (#20) * 4.0.2: fixed Node 4 support, tested with NVM * Make `securedBy: [ null ]` consistent with no security. * Fix up lint complaints. * Force checkout in unix style, because the linter will complain. --- .gitattributes | 2 ++ consistency-helpers.js | 9 ++++++++- test/secured-by-null.raml | 26 ++++++++++++++++++++++++++ test/secured-by-null.spec.js | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 .gitattributes create mode 100644 test/secured-by-null.raml create mode 100644 test/secured-by-null.spec.js diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..5b64fca --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.js text eol=lf +*.raml text eol=lf \ No newline at end of file diff --git a/consistency-helpers.js b/consistency-helpers.js index ca10627..4df4f4d 100644 --- a/consistency-helpers.js +++ b/consistency-helpers.js @@ -9,7 +9,7 @@ function makeConsistent(obj, types) { obj.type = obj.type[0]; } - if (types && types && types[obj.type]) { + if (types && types[obj.type]) { Object.assign(obj, types[obj.type]); } } @@ -32,6 +32,13 @@ function makeConsistent(obj, types) { delete obj.structuredExample; } + // The RAML 1.0 spec allows that: + // "A securedBy node containing null as the array component indicates + // the method can be called without applying any security scheme." + if (Array.isArray(obj.securedBy) && obj.securedBy.length === 1 && obj.securedBy[0] === null) { + delete obj.securedBy; + } + Object.keys(obj).forEach((key) => { const value = obj[key]; makeConsistent(value, types); diff --git a/test/secured-by-null.raml b/test/secured-by-null.raml new file mode 100644 index 0000000..c216f73 --- /dev/null +++ b/test/secured-by-null.raml @@ -0,0 +1,26 @@ +#%RAML 1.0 +title: Secured By Null + +securitySchemes: + oauth_2_0: + type: OAuth 2.0 + describedBy: + headers: + Authorization: string + responses: + 401: + description: Invalid or expired token. + settings: + accessTokenUri: /token + authorizationGrants: [ client_credentials ] + +/A: + get: + securedBy: [ null ] + +/B: + get: + +/C: + get: + securedBy: [ oauth_2_0 ] \ No newline at end of file diff --git a/test/secured-by-null.spec.js b/test/secured-by-null.spec.js new file mode 100644 index 0000000..0449e61 --- /dev/null +++ b/test/secured-by-null.spec.js @@ -0,0 +1,34 @@ +/* eslint-env node, mocha */ + +'use strict'; + +const raml2obj = require('..'); +const assert = require('assert'); + +describe('raml2obj', () => { + describe('secured-by-null.raml', () => { + let obj; + + before((done) => { + raml2obj.parse('test/secured-by-null.raml').then((result) => { + obj = result; + done(); + }, (error) => { + console.log('error', error); + }); + }); + + it('should make securedBy consistent', () => { + const A = obj.resources[0]; + const B = obj.resources[1]; + const C = obj.resources[2]; + assert.strictEqual(A.relativeUri, '/A'); + assert.strictEqual(B.relativeUri, '/B'); + assert.strictEqual(C.relativeUri, '/C'); + assert.strictEqual(A.methods[0].securedBy, undefined); + assert.strictEqual(B.methods[0].securedBy, undefined); + assert.strictEqual(C.methods[0].securedBy.length, 1); + assert.strictEqual(C.methods[0].securedBy[0], 'oauth_2_0'); + }); + }); +});