Skip to content

Commit

Permalink
feat(no-$ref-siblings): modify rule to also run on OpenAPI 3.1.x docu…
Browse files Browse the repository at this point in the history
…ments

This commit modifies the 'no-$ref-siblings' rule which is inherited
from Spectral's oas ruleset.  Specifically, we modify the rule so that
it is run on OpenAPI 3.1.x documents instead of only 3.0.x documents.

Signed-off-by: Phil Adams <[email protected]>
  • Loading branch information
padamstx committed Sep 27, 2023
1 parent c1fffb4 commit 1ccfd3d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/ruleset/src/ibm-oas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
const { oas3 } = require('@stoplight/spectral-formats');
const { oas } = require('@stoplight/spectral-rulesets');
const ibmRules = require('./rules');

// Spectral's "no-$ref-siblings" rule is configured to run on
// OpenAPI 3.0.x documents (ref sibling attributes are allowed in OpenAPI 3.1.x).
// However, we want to enable this rule also for OpenAPI 3.1.x documents,
// so we'll just tweak Spectral's rule definition here.
oas.rules['no-$ref-siblings'].formats = [oas3];

module.exports = {
extends: oas,
documentationUrl:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ components:
description: string
category:
$ref: "#/components/schemas/Category"
description: 'This ref sibling should cause an error'
name:
type: string
example: doggie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ components:
description: string
category:
$ref: "#/components/schemas/Category"
description: 'This ref sibling should cause an error'
name:
type: string
example: doggie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,23 @@ describe('Expected output tests', function () {
expect(capturedText[errorStart + 10].match(/\S+/g)[2]).toEqual('96');
expect(capturedText[errorStart + 15].match(/\S+/g)[2]).toEqual('103');

// Specifically verify that the no-$ref-siblings error occurred.
// We do this because this rule is inherited from Spectral's oas ruleset,
// but we modify the rule definition in ibmoas.js so that it is run
// against both OpenAPI 3.0 and 3.1 documents.
expect(capturedText[errorStart + 17].split(':')[1].trim()).toEqual(
'$ref must not be placed next to any other properties'
);
expect(capturedText[errorStart + 18].split(':')[1].trim()).toEqual(
'no-$ref-siblings'
);
expect(capturedText[errorStart + 19].split(':')[1].trim()).toEqual(
'components.schemas.Pet.properties.category.description'
);
expect(capturedText[errorStart + 20].match(/\S+/g)[2]).toEqual('176');

// warnings
const warningStart = 20;
const warningStart = 25;
expect(capturedText[warningStart + 5].match(/\S+/g)[2]).toEqual('22');
expect(capturedText[warningStart + 10].match(/\S+/g)[2]).toEqual('24');
expect(capturedText[warningStart + 15].match(/\S+/g)[2]).toEqual('40');
Expand All @@ -101,7 +116,7 @@ describe('Expected output tests', function () {
expect(capturedText[warningStart + 50].match(/\S+/g)[2]).toEqual('96');
// Skip a few, then verify the last one.
expect(capturedText[warningStart + 145].match(/\S+/g)[2]).toEqual(
'201'
'202'
);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('cli tool - test option handling', function () {
const capturedText = getCapturedText(consoleSpy.mock.calls);
// This can be uncommented to display the output when adjustments to
// the expect statements below are needed.
// let textOutput = "";
// let textOutput = '';
// capturedText.forEach((elem, index) => {
// textOutput += `[${index}]: ${elem}\n`;
// });
Expand All @@ -113,19 +113,19 @@ describe('cli tool - test option handling', function () {
expect(sumSection).toBe(3);

// totals
expect(capturedText[sumSection + 2].match(/\S+/g)[5]).toEqual('3');
expect(capturedText[sumSection + 2].match(/\S+/g)[5]).toEqual('4');
expect(capturedText[sumSection + 3].match(/\S+/g)[5]).toEqual('29');

// errors
const errorSection = 8;
expect(capturedText[errorSection + 1].match(/\S+/g)[0]).toEqual('1');
expect(capturedText[errorSection + 1].match(/\S+/g)[1]).toEqual('(33%)');
expect(capturedText[errorSection + 1].match(/\S+/g)[1]).toEqual('(25%)');

expect(capturedText[errorSection + 2].match(/\S+/g)[0]).toEqual('2');
expect(capturedText[errorSection + 2].match(/\S+/g)[1]).toEqual('(67%)');
expect(capturedText[errorSection + 2].match(/\S+/g)[1]).toEqual('(50%)');

// warnings
const warningSection = 12;
const warningSection = 13;
expect(capturedText[warningSection + 1].match(/\S+/g)[0]).toEqual('2');
expect(capturedText[warningSection + 1].match(/\S+/g)[1]).toEqual('(7%)');

Expand Down

0 comments on commit 1ccfd3d

Please sign in to comment.