Skip to content

Commit

Permalink
Make securedBy: [ null ] consistent with no security. (#20)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
nickwb authored and kevinrenskers committed Dec 29, 2016
1 parent abe18a8 commit c30c5b5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.js text eol=lf
*.raml text eol=lf
9 changes: 8 additions & 1 deletion consistency-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand All @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions test/secured-by-null.raml
Original file line number Diff line number Diff line change
@@ -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 ]
34 changes: 34 additions & 0 deletions test/secured-by-null.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});

0 comments on commit c30c5b5

Please sign in to comment.