Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unused percent encoded #2212

Merged
merged 2 commits into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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: [],
},
]);
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: [],
},
]);