Skip to content

Commit

Permalink
fix(functions): handle percent encoded in unreferencedReusableObject(#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mkistler committed Jul 22, 2022
1 parent ca6b0c0 commit d16b5a6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 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';

Expand All @@ -24,7 +25,9 @@ export default createRulesetFunction<Record<string, unknown>, 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 {
Expand Down
28 changes: 28 additions & 0 deletions packages/rulesets/src/oas/__tests__/oas2-unused-definition.test.ts
Expand Up @@ -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: [],
},
]);
50 changes: 50 additions & 0 deletions packages/rulesets/src/oas/__tests__/oas3-unused-component.test.ts
Expand Up @@ -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: [],
},
]);

0 comments on commit d16b5a6

Please sign in to comment.