Skip to content

Commit d16b5a6

Browse files
authored
fix(functions): handle percent encoded in unreferencedReusableObject(stoplightio#2212)
1 parent ca6b0c0 commit d16b5a6

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

packages/functions/src/unreferencedReusableObject.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createRulesetFunction } from '@stoplight/spectral-core';
22
import { safePointerToPath } from '@stoplight/spectral-runtime';
3+
import { decodePointer } from '@stoplight/json';
34

45
import { optionSchemas } from './optionSchemas';
56

@@ -24,7 +25,9 @@ export default createRulesetFunction<Record<string, unknown>, Options>(
2425

2526
const defined = Object.keys(data).map(name => `${normalizedSource}${opts.reusableObjectsLocation}/${name}`);
2627

27-
const orphans = defined.filter(defPath => !graph.hasNode(defPath));
28+
const decodedNodes = new Set(graph.overallOrder().map(n => decodePointer(n)));
29+
30+
const orphans = defined.filter(defPath => !decodedNodes.has(defPath));
2831

2932
return orphans.map(orphanPath => {
3033
return {

packages/rulesets/src/oas/__tests__/oas2-unused-definition.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,32 @@ testRule('oas2-unused-definition', [
201201
[indirect2Document.source!]: indirect2Document.data,
202202
},
203203
},
204+
205+
{
206+
name: 'all components are referenced with percent-encoded refs',
207+
document: {
208+
swagger: '2.0',
209+
paths: {
210+
'/path': {
211+
post: {
212+
parameters: [
213+
{
214+
name: '$body',
215+
in: 'body',
216+
schema: {
217+
$ref: '#/definitions/%24body',
218+
},
219+
},
220+
],
221+
},
222+
},
223+
},
224+
definitions: {
225+
$body: {
226+
type: 'string',
227+
},
228+
},
229+
},
230+
errors: [],
231+
},
204232
]);

packages/rulesets/src/oas/__tests__/oas3-unused-component.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,54 @@ testRule('oas3-unused-component', [
204204
[indirect2Document.source!]: indirect2Document.data,
205205
},
206206
},
207+
{
208+
name: 'all components are referenced with percent-encoded refs',
209+
document: {
210+
openapi: '3.0.0',
211+
paths: {
212+
'/path': {
213+
get: {
214+
parameters: [
215+
{
216+
$ref: '#/components/parameters/%24top',
217+
},
218+
],
219+
responses: {
220+
200: {
221+
description: 'Success',
222+
content: {
223+
'application/json': {
224+
$ref: '#/components/schemas/$resource',
225+
},
226+
},
227+
},
228+
default: {
229+
$ref: '#/components/responses/%24default',
230+
},
231+
},
232+
},
233+
},
234+
},
235+
components: {
236+
parameters: {
237+
$top: {
238+
name: '$top',
239+
in: 'query',
240+
type: 'integer',
241+
},
242+
},
243+
responses: {
244+
$default: {
245+
description: 'Ruh Roh',
246+
},
247+
},
248+
schemas: {
249+
$resource: {
250+
type: 'string',
251+
},
252+
},
253+
},
254+
},
255+
errors: [],
256+
},
207257
]);

0 commit comments

Comments
 (0)