Skip to content

Commit

Permalink
fix: dont return security if no securityschemes are present (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Aug 24, 2020
1 parent 5968d68 commit 0c87067
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
32 changes: 31 additions & 1 deletion packages/tooling/__tests__/operation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ describe('#isJson', () => {

describe('#getSecurity()', () => {
const security = [{ auth: [] }];
const securitySchemes = {
auth: {
type: 'http',
scheme: 'basic',
},
};

it('should return the security on this operation', () => {
expect(
Expand All @@ -205,6 +211,9 @@ describe('#getSecurity()', () => {
},
},
},
components: {
securitySchemes,
},
})
.operation('/things', 'post')
.getSecurity()
Expand All @@ -221,13 +230,16 @@ describe('#getSecurity()', () => {
},
},
security,
components: {
securitySchemes,
},
})
.operation('/things', 'post')
.getSecurity()
).toBe(security);
});

it('should default to empty array', () => {
it('should default to empty array if no security object defined', () => {
expect(
new Oas({
info: { version: '1.0' },
Expand All @@ -241,6 +253,24 @@ describe('#getSecurity()', () => {
.getSecurity()
).toStrictEqual([]);
});

it('should default to empty array if no securitySchemes are defined', () => {
expect(
new Oas({
info: { version: '1.0' },
paths: {
'/things': {
post: {
security,
},
},
},
components: {},
})
.operation('/things', 'post')
.getSecurity()
).toStrictEqual([]);
});
});

// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject
Expand Down
4 changes: 4 additions & 0 deletions packages/tooling/src/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class Operation {
}

getSecurity() {
if (!('components' in this.oas) || !('securitySchemes' in this.oas.components)) {
return [];
}

return this.security || this.oas.security || [];
}

Expand Down

0 comments on commit 0c87067

Please sign in to comment.