From d16b5a65d1a144ff1752c6ced1af87a343cda068 Mon Sep 17 00:00:00 2001 From: Mike Kistler Date: Fri, 22 Jul 2022 01:53:03 -0700 Subject: [PATCH] fix(functions): handle percent encoded in unreferencedReusableObject(#2212) --- .../src/unreferencedReusableObject.ts | 5 +- .../__tests__/oas2-unused-definition.test.ts | 28 +++++++++++ .../__tests__/oas3-unused-component.test.ts | 50 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/packages/functions/src/unreferencedReusableObject.ts b/packages/functions/src/unreferencedReusableObject.ts index 05b147996..236d5626b 100644 --- a/packages/functions/src/unreferencedReusableObject.ts +++ b/packages/functions/src/unreferencedReusableObject.ts @@ -1,5 +1,6 @@ import { createRulesetFunction } from '@stoplight/spectral-core'; import { safePointerToPath } from '@stoplight/spectral-runtime'; +import { decodePointer } from '@stoplight/json'; import { optionSchemas } from './optionSchemas'; @@ -24,7 +25,9 @@ export default createRulesetFunction, Options>( const defined = Object.keys(data).map(name => `${normalizedSource}${opts.reusableObjectsLocation}/${name}`); - const orphans = defined.filter(defPath => !graph.hasNode(defPath)); + const decodedNodes = new Set(graph.overallOrder().map(n => decodePointer(n))); + + const orphans = defined.filter(defPath => !decodedNodes.has(defPath)); return orphans.map(orphanPath => { return { diff --git a/packages/rulesets/src/oas/__tests__/oas2-unused-definition.test.ts b/packages/rulesets/src/oas/__tests__/oas2-unused-definition.test.ts index 5c791f8bb..0607e76c1 100644 --- a/packages/rulesets/src/oas/__tests__/oas2-unused-definition.test.ts +++ b/packages/rulesets/src/oas/__tests__/oas2-unused-definition.test.ts @@ -201,4 +201,32 @@ testRule('oas2-unused-definition', [ [indirect2Document.source!]: indirect2Document.data, }, }, + + { + name: 'all components are referenced with percent-encoded refs', + document: { + swagger: '2.0', + paths: { + '/path': { + post: { + parameters: [ + { + name: '$body', + in: 'body', + schema: { + $ref: '#/definitions/%24body', + }, + }, + ], + }, + }, + }, + definitions: { + $body: { + type: 'string', + }, + }, + }, + errors: [], + }, ]); diff --git a/packages/rulesets/src/oas/__tests__/oas3-unused-component.test.ts b/packages/rulesets/src/oas/__tests__/oas3-unused-component.test.ts index 8eed2e5c8..f74df9d13 100644 --- a/packages/rulesets/src/oas/__tests__/oas3-unused-component.test.ts +++ b/packages/rulesets/src/oas/__tests__/oas3-unused-component.test.ts @@ -204,4 +204,54 @@ testRule('oas3-unused-component', [ [indirect2Document.source!]: indirect2Document.data, }, }, + { + name: 'all components are referenced with percent-encoded refs', + document: { + openapi: '3.0.0', + paths: { + '/path': { + get: { + parameters: [ + { + $ref: '#/components/parameters/%24top', + }, + ], + responses: { + 200: { + description: 'Success', + content: { + 'application/json': { + $ref: '#/components/schemas/$resource', + }, + }, + }, + default: { + $ref: '#/components/responses/%24default', + }, + }, + }, + }, + }, + components: { + parameters: { + $top: { + name: '$top', + in: 'query', + type: 'integer', + }, + }, + responses: { + $default: { + description: 'Ruh Roh', + }, + }, + schemas: { + $resource: { + type: 'string', + }, + }, + }, + }, + errors: [], + }, ]);