Skip to content

Commit b5d7491

Browse files
committed
resolve the webhook/pathItems issue
1 parent 10890ac commit b5d7491

4 files changed

Lines changed: 89 additions & 43 deletions

File tree

packages/core/src/rules/oas3/__tests__/no-unused-components.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,49 @@ describe('Oas3 no-unused-components', () => {
321321
`);
322322
});
323323

324+
it('should not report securitySchemes referenced via SecurityRequirement from webhooks or components.pathItems', async () => {
325+
const document = parseYamlToDocument(
326+
outdent`
327+
openapi: 3.1.0
328+
components:
329+
securitySchemes:
330+
webhook_key:
331+
type: apiKey
332+
name: webhook-key
333+
in: header
334+
shared_key:
335+
type: apiKey
336+
name: shared-key
337+
in: header
338+
pathItems:
339+
shared:
340+
get:
341+
security:
342+
- shared_key: []
343+
responses:
344+
'200':
345+
description: ok
346+
webhooks:
347+
newPet:
348+
post:
349+
security:
350+
- webhook_key: []
351+
responses:
352+
'200':
353+
description: ok
354+
`,
355+
'foobar.yaml'
356+
);
357+
358+
const results = await lintDocument({
359+
externalRefResolver: new BaseResolver(),
360+
document,
361+
config: await createConfig({ rules: { 'no-unused-components': 'error' } }),
362+
});
363+
364+
expect(results).toEqual([]);
365+
});
366+
324367
it('should not report securitySchemes referenced via SecurityRequirement', async () => {
325368
const document = parseYamlToDocument(
326369
outdent`

packages/core/src/rules/oas3/no-unused-components.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type ComponentInfo = {
1111

1212
export const NoUnusedComponents: Oas3Rule = () => {
1313
const components = new Map<string, ComponentInfo>();
14+
const securitySchemeLocations = new Map<string, Location>();
1415
const usedSecuritySchemeComponents = new Set<string>();
1516

1617
function registerComponent(
@@ -59,6 +60,15 @@ export const NoUnusedComponents: Oas3Rule = () => {
5960
});
6061
}
6162
});
63+
securitySchemeLocations.forEach((location, name) => {
64+
if (!usedSecuritySchemeComponents.has(name)) {
65+
report({
66+
message: `Security scheme: "${name}" is never used.`,
67+
location: location.key(),
68+
reference: 'https://redocly.com/docs/cli/rules/oas/no-unused-components',
69+
});
70+
}
71+
});
6272
},
6373
},
6474
NamedSchemas: {
@@ -100,15 +110,8 @@ export const NoUnusedComponents: Oas3Rule = () => {
100110
},
101111
},
102112
NamedSecuritySchemes: {
103-
SecurityScheme(_securityScheme, { location, key, report }) {
104-
const name = key.toString();
105-
if (!usedSecuritySchemeComponents.has(name)) {
106-
report({
107-
message: `Security scheme: "${name}" is never used.`,
108-
location: location.key(),
109-
reference: 'https://redocly.com/docs/cli/rules/oas/no-unused-components',
110-
});
111-
}
113+
SecurityScheme(_securityScheme, { location, key }) {
114+
securitySchemeLocations.set(key.toString(), location);
112115
},
113116
},
114117
SecurityRequirement(requirements) {

tests/e2e/lint/oas3.1/snapshot.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ referenced from openapi.yaml:180:7 at #/components/schemas/Problem
1919
Warning was generated by the no-invalid-schema-examples rule.
2020

2121

22-
[2] openapi.yaml:175:5 at #/components/securitySchemes/mtls
22+
[2] openapi.yaml:179:5 at #/components/schemas/Problem
2323

24-
Security scheme: "mtls" is never used.
24+
Component: "Problem" is never used.
2525

26-
173 | components:
27-
174 | securitySchemes:
28-
175 | mtls:
29-
| ^^^^
30-
176 | type: mutualTLS
31-
177 | pathItems: {}
26+
177 | pathItems: {}
27+
178 | schemas:
28+
179 | Problem:
29+
| ^^^^^^^
30+
180 | id: https://tools.ietf.org/rfc/rfc7807.txt
31+
181 | $schema: 'http://json-schema.org/draft-06/schema#'
3232

3333
Warning was generated by the no-unused-components rule.
3434

3535
Reference: https://redocly.com/docs/cli/rules/oas/no-unused-components
3636

3737

38-
[3] openapi.yaml:179:5 at #/components/schemas/Problem
38+
[3] openapi.yaml:175:5 at #/components/securitySchemes/mtls
3939

40-
Component: "Problem" is never used.
40+
Security scheme: "mtls" is never used.
4141

42-
177 | pathItems: {}
43-
178 | schemas:
44-
179 | Problem:
45-
| ^^^^^^^
46-
180 | id: https://tools.ietf.org/rfc/rfc7807.txt
47-
181 | $schema: 'http://json-schema.org/draft-06/schema#'
42+
173 | components:
43+
174 | securitySchemes:
44+
175 | mtls:
45+
| ^^^^
46+
176 | type: mutualTLS
47+
177 | pathItems: {}
4848

4949
Warning was generated by the no-unused-components rule.
5050

tests/e2e/lint/oas3.2/snapshot.txt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,23 @@ Operation must have at least one `4XX` response.
277277
Warning was generated by the operation-4xx-response rule.
278278

279279

280-
[21] openapi.yaml:125:5 at #/components/securitySchemes/petstore_auth
280+
[21] openapi.yaml:149:5 at #/components/schemas/MyResponseType
281+
282+
Component: "MyResponseType" is never used.
283+
284+
147 | tokenUrl: http://localhost
285+
148 | schemas:
286+
149 | MyResponseType:
287+
| ^^^^^^^^^^^^^^
288+
150 | oneOf:
289+
151 | - $ref: '#/components/schemas/Lizard'
290+
291+
Warning was generated by the no-unused-components rule.
292+
293+
Reference: https://redocly.com/docs/cli/rules/oas/no-unused-components
294+
295+
296+
[22] openapi.yaml:125:5 at #/components/securitySchemes/petstore_auth
281297

282298
Security scheme: "petstore_auth" is never used.
283299

@@ -293,7 +309,7 @@ Warning was generated by the no-unused-components rule.
293309
Reference: https://redocly.com/docs/cli/rules/oas/no-unused-components
294310

295311

296-
[22] openapi.yaml:136:5 at #/components/securitySchemes/api_key
312+
[23] openapi.yaml:136:5 at #/components/securitySchemes/api_key
297313

298314
Security scheme: "api_key" is never used.
299315

@@ -309,22 +325,6 @@ Warning was generated by the no-unused-components rule.
309325
Reference: https://redocly.com/docs/cli/rules/oas/no-unused-components
310326

311327

312-
[23] openapi.yaml:149:5 at #/components/schemas/MyResponseType
313-
314-
Component: "MyResponseType" is never used.
315-
316-
147 | tokenUrl: http://localhost
317-
148 | schemas:
318-
149 | MyResponseType:
319-
| ^^^^^^^^^^^^^^
320-
150 | oneOf:
321-
151 | - $ref: '#/components/schemas/Lizard'
322-
323-
Warning was generated by the no-unused-components rule.
324-
325-
Reference: https://redocly.com/docs/cli/rules/oas/no-unused-components
326-
327-
328328

329329
validating openapi.yaml using lint rules for api 'main'...
330330
openapi.yaml: validated in <test>ms

0 commit comments

Comments
 (0)