Skip to content

Commit

Permalink
Spectral rules fix for changed | addedOrChanged (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
acunniffe authored Nov 15, 2023
1 parent 71dbc91 commit edbf288
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "openapi-workspaces",
"license": "MIT",
"private": true,
"version": "0.52.2",
"version": "0.52.3",
"workspaces": [
"projects/json-pointer-helpers",
"projects/openapi-io",
Expand Down
2 changes: 1 addition & 1 deletion projects/fastify-capture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/fastify-capture",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.52.2",
"version": "0.52.3",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/json-pointer-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/json-pointer-helpers",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.52.2",
"version": "0.52.3",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-io",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.52.2",
"version": "0.52.3",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-utilities",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.52.2",
"version": "0.52.3",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/optic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/optic",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.52.2",
"version": "0.52.3",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/rulesets-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/rulesets-base",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.52.2",
"version": "0.52.3",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,22 @@ exports[`spectral rule test runs spectral rules on added 1`] = `
},
]
`;

exports[`spectral rule test runs spectral rules on addedOrChanged 1`] = `
[
{
"docsLink": undefined,
"error": "Error code: this-rule: readOnly is not supported by API Gateway",
"exempted": false,
"location": {
"jsonPath": "/paths/~1api/get/responses/200/content/application~1json/schema/properties/example",
"spec": "after",
},
"name": "Spectral addedOrChanged rule",
"passed": false,
"severity": 2,
"type": "addedOrChanged",
"where": "addedOrChanged ",
},
]
`;
90 changes: 90 additions & 0 deletions projects/rulesets-base/src/__tests__/spectral-rule.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test, expect, describe } from '@jest/globals';
import { RuleRunner } from '../rule-runner';
import { SpectralRule } from '../extended-rules/spectral-rule';
import { undefined as SpectralUndefinedFunc } from '@stoplight/spectral-functions';
import {
Spectral,
RulesetDefinition as SpectralRulesetDefinition,
Expand Down Expand Up @@ -57,6 +58,95 @@ describe('spectral rule test', () => {
expect(resultsAgainstSelf.length === 0).toBe(true);
});

test('runs spectral rules on addedOrChanged', async () => {
const spectral = new Spectral();
spectral.setRuleset({
extends: [],
rules: {
'this-rule': {
description: 'readOnly is not supported by API Gateway',
severity: 'error',
given: '$..readOnly',
then: [
{
function: SpectralUndefinedFunc,
},
],
},
},
});

const ruleRunner = new RuleRunner([
new SpectralRule({
name: 'spectral-rules',
spectral,
applies: 'addedOrChanged',
}),
]);
const before: OpenAPIV3.Document = {
...defaultEmptySpec,
paths: {
'/api': {
get: {
responses: {
'200': {
description: '200',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
example: {
type: 'string',
},
},
},
},
},
},
},
},
},
},
};
const after: OpenAPIV3.Document = {
...defaultEmptySpec,
paths: {
'/api': {
get: {
responses: {
'200': {
description: '200',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
example: {
type: 'string',
readOnly: true,
},
},
},
},
},
},
},
},
},
},
};
const diffs = diff(before, after);
const resultsWithChanges = await ruleRunner.runRules({
diffs,
fromSpec: before,
toSpec: after,
context: {},
});

expect(resultsWithChanges).toMatchSnapshot();
});

test('can use matches', async () => {
const ruleRunner = new RuleRunner([
new SpectralRule({
Expand Down
14 changes: 12 additions & 2 deletions projects/rulesets-base/src/extended-rules/spectral-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,12 @@ export class SpectralRule extends ExternalRuleBase {
} else {
// find if there is an appropriate change
let maybeChange: ObjectDiff | undefined =
changesByJsonPath[fact.location.jsonPath];
changesByJsonPath[fact.location.jsonPath] ||
changesByJsonPath[
jsonPointerHelpers.compile(
spectralResult.path.map((i) => i.toString())
)
];

if (
!maybeChange &&
Expand Down Expand Up @@ -465,7 +470,12 @@ export class SpectralRule extends ExternalRuleBase {
} else {
// find if there is an appropriate change
const maybeChange: IChange | undefined =
changesByJsonPath[fact.location.jsonPath];
changesByJsonPath[fact.location.jsonPath] ||
changesByJsonPath[
jsonPointerHelpers.compile(
spectralResult.path.map((i) => i.toString())
)
];
if (maybeChange) {
if (this.lifecycle === 'added' && maybeChange.added) {
results.push(
Expand Down
2 changes: 1 addition & 1 deletion projects/standard-rulesets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/standard-rulesets",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.52.2",
"version": "0.52.3",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down

0 comments on commit edbf288

Please sign in to comment.