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

feat(oas): add oas3-unused-component rule which detects all orphaned components #1440

Merged
merged 4 commits into from
Jan 28, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/reference/openapi-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,9 @@ servers:
- url: https://example.com/api/
```

### oas3-unused-components-schema
### oas3-unused-component

Potential unused reusable `schema` entry has been detected.
Potential unused reusable `components` entry has been detected.

_Warning:_ This rule may identify false positives when linting a specification
that acts as a library (a container storing reusable objects, leveraged by other
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,13 +715,13 @@ responses:: !!foo
code: 'invalid-ref',
}),
expect.objectContaining({
code: 'oas3-unused-components-schema',
message: 'Potentially unused components schema has been detected.',
code: 'oas3-unused-component',
message: 'Potentially unused component has been detected.',
path: ['components', 'schemas', 'Pets'],
}),
expect.objectContaining({
code: 'oas3-unused-components-schema',
message: 'Potentially unused components schema has been detected.',
code: 'oas3-unused-component',
message: 'Potentially unused component has been detected.',
path: ['components', 'schemas', 'foo'],
}),
expect.objectContaining({
Expand Down
2 changes: 1 addition & 1 deletion src/cli/services/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ describe('Linter service', () => {
source: join(process.cwd(), 'src/__tests__/__fixtures__/petstore.invalid-schema.oas3.json'),
}),
expect.objectContaining({
code: 'oas3-unused-components-schema',
code: 'oas3-unused-component',
path: ['components', 'schemas', 'Pets'],
source: join(process.cwd(), 'src/__tests__/__fixtures__/petstore.invalid-schema.oas3.json'),
}),
Expand Down
142 changes: 142 additions & 0 deletions src/rulesets/__tests__/__fixtures__/unusedComponent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Used Components",
"description": "Negative test of unused components",
"contact": {
"email": "anywho@widgetco.com"
}
},
"servers": [
{
"url": "http://petstore.swagger.io/v2"
}
],
"tags": [
{
"name": "pets"
}
],
"paths": {
"/pet": {
"post": {
"description": "Add a new pet to the store",
"summary": "Add pet",
"operationId": "add_pet",
"tags": [
"pets"
],
"parameters": [
{
"name": "param1",
"in": "query",
"schema": {
"type": "string"
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
},
"responses": {
"200": {
"description": "Success"
},
"default": {
"description": "Not success"
}
}
}
}
},
"components": {
"schemas": {
"SomeSchema": {
"description": "Used schema",
"type": "string"
}
},
"parameters": {
"SomeParameter": {
"description": "Used parameter",
"name": "foo",
"in": "query",
"schema": {
"type": "string"
}
}
},
"requestBodies": {
"SomeBody": {
"description": "Used request body",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
},
"callbacks": {
"SomeCallback": {
"{$request.query.queryUrl}": {
"post": {
"requestBody": {
"description": "Callback payload",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
},
"examples": {
"SomeExample": {
"description": "Used example",
"value": "foo"
}
},
"headers": {
"SomeHeader": {
"description": "Used header",
"schema": {
"type": "string"
}
}
},
"links": {
"SomeLink": {
"description": "Used link",
"operationId": "add_pet",
"parameters": {
"pet_id": "$response.body#/id"
}
}
},
"responses": {
"SomeResponse": {
"description": "Used response"
}
}
}
}
162 changes: 162 additions & 0 deletions src/rulesets/__tests__/__fixtures__/unusedComponent.negative.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Used Components",
"description": "Negative test of unused components",
"contact": {
"email": "anywho@widgetco.com"
}
},
"servers": [
{
"url": "http://petstore.swagger.io/v2"
}
],
"tags": [
{
"name": "pets"
}
],
"paths": {
"/pet": {
"post": {
"description": "Add a new pet to the store",
"summary": "Add pet",
"operationId": "add_pet",
"tags": [
"pets"
],
"parameters": [
{
"name": "param1",
"in": "query",
"schema": {
"$ref": "#/components/schemas/SomeSchema"
}
},
{
"$ref": "#/components/parameters/SomeParameter"
}
],
"requestBody": {
"$ref": "#/components/requestBodies/SomeBody"
},
"responses": {
"200": {
"description": "Success",
"headers": {
"Some-Header": {
"$ref": "#/components/headers/SomeHeader"
}
},
"content": {
"application/json": {
"examples": {
"AnExample": {
"$ref": "#/components/examples/SomeExample"
}
}
}
},
"links": {
"TheLink": {
"$ref": "#/components/links/SomeLink"
}
}
},
"default": {
"$ref": "#/components/responses/SomeResponse"
}
},
"callbacks": {
"TheCallback": {
"$ref": "#/components/callbacks/SomeCallback"
}
}
}
}
},
"components": {
"schemas": {
"SomeSchema": {
"description": "Used schema",
"type": "string"
}
},
"parameters": {
"SomeParameter": {
"description": "Used parameter",
"name": "foo",
"in": "query",
"schema": {
"type": "string"
}
}
},
"requestBodies": {
"SomeBody": {
"description": "Used request body",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
},
"callbacks": {
"SomeCallback": {
"{$request.query.queryUrl}": {
"post": {
"requestBody": {
"description": "Callback payload",
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
}
},
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
},
"examples": {
"SomeExample": {
"description": "Used example",
"value": "foo"
}
},
"headers": {
"SomeHeader": {
"description": "Used header",
"schema": {
"type": "string"
}
}
},
"links": {
"SomeLink": {
"description": "Used link",
"operationId": "add_pet",
"parameters": {
"pet_id": "$response.body#/id"
}
}
},
"responses": {
"SomeResponse": {
"description": "Used response"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import * as nock from 'nock';

import { Document } from '../../../document';
import { readParsable } from '../../../fs/reader';
import type { Spectral } from '../../../index';
import { Spectral } from '../../../index';
import * as Parsers from '../../../parsers';
import { createWithRules } from './__helpers__/createWithRules';
import { httpAndFileResolver } from '../../../resolvers/http-and-file';

describe('unusedComponentsSchema - Http and fs remote references', () => {
describe('unusedComponent - Http and fs remote references', () => {
let s: Spectral;

beforeEach(async () => {
s = await createWithRules(['oas3-unused-components-schema'], { resolver: httpAndFileResolver });
s = await createWithRules(['oas3-unused-component'], { resolver: httpAndFileResolver });
});

describe('reports unreferenced components schemas', () => {
describe('reports unreferenced components', () => {
test('when analyzing an in-memory document', async () => {
nock('https://oas3.library.com')
.get('/defs.json')
Expand Down Expand Up @@ -84,8 +84,8 @@ describe('unusedComponentsSchema - Http and fs remote references', () => {

expect(results).toEqual([
{
code: 'oas3-unused-components-schema',
message: 'Potentially unused components schema has been detected.',
code: 'oas3-unused-component',
message: 'Potentially unused component has been detected.',
path: ['components', 'schemas', 'Unhooked'],
range: {
end: {
Expand Down Expand Up @@ -121,8 +121,8 @@ describe('unusedComponentsSchema - Http and fs remote references', () => {

expect(results).toEqual([
{
code: 'oas3-unused-components-schema',
message: 'Potentially unused components schema has been detected.',
code: 'oas3-unused-component',
message: 'Potentially unused component has been detected.',
path: ['components', 'schemas', 'Unhooked'],
range: {
end: {
Expand Down
Loading