Skip to content

Commit bb460c4

Browse files
SamerJaser96dpopp07
authored andcommitted
fix: replace jsSpec with resolvedSpec in security-definitons-ibm
1 parent 7d5f982 commit bb460c4

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

src/plugins/validation/2and3/semantic-validators/security-definitions-ibm.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44

55
const each = require('lodash/each');
66

7-
module.exports.validate = function({ jsSpec, isOAS3 }, config) {
7+
module.exports.validate = function({ resolvedSpec, isOAS3 }, config) {
88
const result = {};
99
result.error = [];
1010
result.warning = [];
1111

1212
config = config.security_definitions;
1313

14-
const usedSchemes = {};
15-
const usedScopes = {};
14+
const definedSchemes = {};
15+
const definedScopes = {};
1616

1717
// collect the security requirements and all relevant scopes
1818

1919
const securityDefinitions = isOAS3
20-
? jsSpec.components && jsSpec.components.securitySchemes
21-
: jsSpec.securityDefinitions;
20+
? resolvedSpec.components && resolvedSpec.components.securitySchemes
21+
: resolvedSpec.securityDefinitions;
2222

2323
each(securityDefinitions, (scheme, name) => {
2424
if (name.slice(0, 2) === 'x-') return;
2525

26-
usedSchemes[name] = {};
27-
usedSchemes[name].used = false;
28-
usedSchemes[name].type = scheme.type;
26+
definedSchemes[name] = {};
27+
definedSchemes[name].used = false;
28+
definedSchemes[name].type = scheme.type;
2929

3030
// collect scopes in oauth2 schemes
3131
if (scheme.type.toLowerCase() === 'oauth2') {
@@ -34,19 +34,19 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
3434
each(scheme.flows, (flow, flowType) => {
3535
if (flow.scopes) {
3636
Object.keys(flow.scopes).forEach(scope => {
37-
usedScopes[scope] = {};
38-
usedScopes[scope].used = false;
39-
usedScopes[scope].scheme = name;
40-
usedScopes[scope].flow = flowType;
37+
definedScopes[scope] = {};
38+
definedScopes[scope].used = false;
39+
definedScopes[scope].scheme = name;
40+
definedScopes[scope].flow = flowType;
4141
});
4242
}
4343
});
4444
}
4545
} else {
4646
Object.keys(scheme.scopes).forEach(scope => {
47-
usedScopes[scope] = {};
48-
usedScopes[scope].used = false;
49-
usedScopes[scope].scheme = name;
47+
definedScopes[scope] = {};
48+
definedScopes[scope].used = false;
49+
definedScopes[scope].scheme = name;
5050
});
5151
}
5252
}
@@ -56,12 +56,12 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
5656
// security objects can exist at either:
5757

5858
// 1) the top level of the spec (global definition)
59-
if (jsSpec.security) {
60-
flagUsedDefinitions(jsSpec.security);
59+
if (resolvedSpec.security) {
60+
flagUsedDefinitions(resolvedSpec.security);
6161
}
6262

6363
// 2) within operations objects
64-
const paths = jsSpec.paths;
64+
const paths = resolvedSpec.paths;
6565
each(paths, (operations, pathName) => {
6666
if (pathName.slice(0, 2) === 'x-') return;
6767
each(operations, (operation, opName) => {
@@ -78,16 +78,16 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
7878
const name = Object.keys(scheme)[0];
7979

8080
// make sure this scheme was in the security definitions, then label as used
81-
if (usedSchemes[name]) {
82-
usedSchemes[name].used = true;
81+
if (definedSchemes[name]) {
82+
definedSchemes[name].used = true;
8383

84-
const type = usedSchemes[name].type;
84+
const type = definedSchemes[name].type;
8585
const scopesArray = scheme[name];
8686

8787
if (type.toLowerCase() === 'oauth2') {
8888
scopesArray.forEach(scope => {
89-
if (usedScopes[scope]) {
90-
usedScopes[scope].used = true;
89+
if (definedScopes[scope]) {
90+
definedScopes[scope].used = true;
9191
}
9292
});
9393
}
@@ -96,7 +96,7 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
9696
}
9797

9898
// check what has been used and what has not been
99-
each(usedSchemes, (info, name) => {
99+
each(definedSchemes, (info, name) => {
100100
if (info.used === false) {
101101
const checkStatus = config.unused_security_schemes;
102102
if (checkStatus !== 'off') {
@@ -111,7 +111,7 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
111111
}
112112
});
113113

114-
each(usedScopes, (info, name) => {
114+
each(definedScopes, (info, name) => {
115115
if (info.used === false) {
116116
const checkStatus = config.unused_security_scopes;
117117
if (checkStatus !== 'off') {

test/plugins/validation/2and3/security-definitions-ibm.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const expect = require('expect');
2+
const resolver = require('json-schema-ref-parser');
23
const {
34
validate
45
} = require('../../../../src/plugins/validation/2and3/semantic-validators/security-definitions-ibm');
@@ -51,7 +52,7 @@ describe('validation plugin - semantic - security-definitions-ibm', function() {
5152
}
5253
};
5354

54-
const res = validate({ jsSpec: spec }, config);
55+
const res = validate({ resolvedSpec: spec }, config);
5556
expect(res.errors.length).toEqual(0);
5657
expect(res.warnings.length).toEqual(1);
5758
expect(res.warnings[0].message).toEqual(
@@ -92,7 +93,7 @@ describe('validation plugin - semantic - security-definitions-ibm', function() {
9293
}
9394
};
9495

95-
const res = validate({ jsSpec: spec }, config);
96+
const res = validate({ resolvedSpec: spec }, config);
9697
expect(res.errors.length).toEqual(0);
9798
expect(res.warnings.length).toEqual(1);
9899
expect(res.warnings[0].message).toEqual(
@@ -115,13 +116,40 @@ describe('validation plugin - semantic - security-definitions-ibm', function() {
115116
}
116117
};
117118

118-
const res = validate({ jsSpec: spec }, config);
119+
const res = validate({ resolvedSpec: spec }, config);
119120
expect(res.errors.length).toEqual(0);
120121
expect(res.warnings.length).toEqual(0);
121122
});
122123
});
123124

124125
describe('OpenAPI 3', function() {
126+
it('should follow references to security schemes', async function() {
127+
const spec = {
128+
components: {
129+
schemas: {
130+
SecuritySchemeModel: {
131+
type: 'http',
132+
scheme: 'basic',
133+
descriptions: 'example text for def with unused security def'
134+
}
135+
},
136+
securitySchemes: {
137+
scheme1: {
138+
$ref: '#/components/schemas/SecuritySchemeModel'
139+
}
140+
}
141+
}
142+
};
143+
144+
const resolvedSpec = await resolver.dereference(spec);
145+
146+
const res = validate({ resolvedSpec, isOAS3: true }, config);
147+
expect(res.errors.length).toEqual(0);
148+
expect(res.warnings.length).toEqual(1);
149+
expect(res.warnings[0].message).toEqual(
150+
'A security scheme is defined but never used: scheme1'
151+
);
152+
});
125153
it('should warn about an unused security definition', function() {
126154
const spec = {
127155
components: {
@@ -158,7 +186,7 @@ describe('validation plugin - semantic - security-definitions-ibm', function() {
158186
}
159187
};
160188

161-
const res = validate({ jsSpec: spec, isOAS3: true }, config);
189+
const res = validate({ resolvedSpec: spec, isOAS3: true }, config);
162190
expect(res.errors.length).toEqual(0);
163191
expect(res.warnings.length).toEqual(1);
164192
expect(res.warnings[0].message).toEqual(
@@ -208,7 +236,7 @@ describe('validation plugin - semantic - security-definitions-ibm', function() {
208236
}
209237
};
210238

211-
const res = validate({ jsSpec: spec, isOAS3: true }, config);
239+
const res = validate({ resolvedSpec: spec, isOAS3: true }, config);
212240
expect(res.errors.length).toEqual(0);
213241
expect(res.warnings.length).toEqual(1);
214242
expect(res.warnings[0].message).toEqual(
@@ -258,7 +286,7 @@ describe('validation plugin - semantic - security-definitions-ibm', function() {
258286
}
259287
};
260288

261-
const res = validate({ jsSpec: spec, isOAS3: true }, config);
289+
const res = validate({ resolvedSpec: spec, isOAS3: true }, config);
262290
expect(res.errors.length).toEqual(0);
263291
expect(res.warnings.length).toEqual(0);
264292
});

0 commit comments

Comments
 (0)