Skip to content

Commit

Permalink
fix(oas): path-params reports actual endpoint path (#1135)
Browse files Browse the repository at this point in the history
* fix(oas): path-params reports actual endpoint path

* test: use Document
  • Loading branch information
P0lip authored May 5, 2020
1 parent 23655a5 commit 0c910db
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 81 deletions.
18 changes: 11 additions & 7 deletions src/rulesets/oas/functions/__tests__/oasOp2xxResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { rules } from '../../index.json';
import oasOp2xxResponse from '../oasOp2xxResponse';

describe('oasOp2xxResponse', () => {
const s = new Spectral();
s.setFunctions({ oasOp2xxResponse });
s.setRules({
'operation-2xx-response': Object.assign(rules['operation-2xx-response'], {
recommended: true,
type: RuleType[rules['operation-2xx-response'].type],
}),
let s: Spectral;

beforeEach(() => {
s = new Spectral();
s.setFunctions({ oasOp2xxResponse });
s.setRules({
'operation-2xx-response': Object.assign(rules['operation-2xx-response'], {
recommended: true,
type: RuleType[rules['operation-2xx-response'].type],
}),
});
});

test('validate a correct object', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import { rules } from '../../index.json';
import oasOpFormDataConsumeCheck from '../oasOpFormDataConsumeCheck';

describe('oasOpFormDataConsumeCheck', () => {
const s = new Spectral();
s.registerFormat('oas2', () => true);
s.setFunctions({ oasOpFormDataConsumeCheck });
s.setRules({
'oas2-operation-formData-consume-check': Object.assign(rules['oas2-operation-formData-consume-check'], {
recommended: true,
type: RuleType[rules['oas2-operation-formData-consume-check'].type],
}),
let s: Spectral;

beforeEach(() => {
s = new Spectral();
s.registerFormat('oas2', () => true);
s.setFunctions({ oasOpFormDataConsumeCheck });
s.setRules({
'oas2-operation-formData-consume-check': Object.assign(rules['oas2-operation-formData-consume-check'], {
recommended: true,
type: RuleType[rules['oas2-operation-formData-consume-check'].type],
}),
});
});

test('validate a correct object', async () => {
Expand Down
17 changes: 10 additions & 7 deletions src/rulesets/oas/functions/__tests__/oasOpIdUnique.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { rules } from '../../index.json';
import oasOpIdUnique from '../oasOpIdUnique';

describe('oasOpIdUnique', () => {
const s = new Spectral();
let s: Spectral;

s.setFunctions({ oasOpIdUnique });
s.setRules({
'operation-operationId-unique': Object.assign(rules['operation-operationId-unique'], {
recommended: true,
type: RuleType[rules['operation-operationId-unique'].type],
}),
beforeEach(() => {
s = new Spectral();
s.setFunctions({ oasOpIdUnique });
s.setRules({
'operation-operationId-unique': Object.assign(rules['operation-operationId-unique'], {
recommended: true,
type: RuleType[rules['operation-operationId-unique'].type],
}),
});
});

test('validate a correct object', async () => {
Expand Down
18 changes: 11 additions & 7 deletions src/rulesets/oas/functions/__tests__/oasOpParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { rules } from '../../index.json';
import oasOpParams from '../oasOpParams';

describe('oasOpParams', () => {
const s = new Spectral();
s.setFunctions({ oasOpParams });
s.setRules({
'operation-parameters': Object.assign(rules['operation-parameters'], {
recommended: true,
type: RuleType[rules['operation-parameters'].type],
}),
let s: Spectral;

beforeEach(() => {
s = new Spectral();
s.setFunctions({ oasOpParams });
s.setRules({
'operation-parameters': Object.assign(rules['operation-parameters'], {
recommended: true,
type: RuleType[rules['operation-parameters'].type],
}),
});
});

test('No error if no params', async () => {
Expand Down
20 changes: 12 additions & 8 deletions src/rulesets/oas/functions/__tests__/oasOpSecurityDefined.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import oasOpSecurityDefined from '../oasOpSecurityDefined';

describe('oasOpSecurityDefined', () => {
describe('oas2', () => {
const s = new Spectral();
s.registerFormat('oas2', () => true);
s.setFunctions({ oasOpSecurityDefined });
s.setRules({
'oas2-operation-security-defined': Object.assign(oasRules['oas2-operation-security-defined'], {
recommended: true,
type: RuleType[oasRules['oas2-operation-security-defined'].type],
}),
let s: Spectral;

beforeEach(() => {
s = new Spectral();
s.registerFormat('oas2', () => true);
s.setFunctions({ oasOpSecurityDefined });
s.setRules({
'oas2-operation-security-defined': Object.assign(oasRules['oas2-operation-security-defined'], {
recommended: true,
type: RuleType[oasRules['oas2-operation-security-defined'].type],
}),
});
});

test('validate a correct object (just in body)', async () => {
Expand Down
83 changes: 46 additions & 37 deletions src/rulesets/oas/functions/__tests__/oasPathParam.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { RuleType, Spectral } from '../../../../index';
import { Document, Parsers, RuleType, Spectral } from '../../../../index';
import { rules } from '../../index.json';
import oasPathParam from '../oasPathParam';

describe('oasPathParam', () => {
const s = new Spectral();
s.setFunctions({ oasPathParam });
s.setRules({
'path-params': Object.assign(rules['path-params'], {
recommended: true,
type: RuleType[rules['path-params'].type],
}),
let s: Spectral;

beforeEach(() => {
s = new Spectral();
s.setFunctions({ oasPathParam });
s.setRules({
'path-params': Object.assign(rules['path-params'], {
recommended: true,
type: RuleType[rules['path-params'].type],
}),
});
});

test('No error if templated path is not used', async () => {
Expand Down Expand Up @@ -255,44 +259,49 @@ describe('oasPathParam', () => {
});

test('Error if paths are functionally equivalent', async () => {
const results = await s.run({
paths: {
'/foo/{boo}': {
parameters: [
{
name: 'boo',
in: 'path',
required: true,
},
],
get: {},
},
'/foo/{bar}': {
parameters: [
{
name: 'bar',
in: 'path',
required: true,
},
],
get: {},
},
},
});
const results = await s.run(
new Document(
`{
"paths": {
"/foo/{boo}": {
"parameters": [
{
"name": "boo",
"in": "path",
"required": true
}
],
"get": {}
},
"/foo/{bar}": {
"parameters": [
{
"name": "bar",
"in": "path",
"required": true
}
],
"get": {}
}
}
}`,
Parsers.Json,
),
);

expect(results).toEqual([
{
code: 'path-params',
message: `The paths \`/foo/{boo}\` and \`/foo/{bar}\` are equivalent.`,
path: ['paths'],
path: ['paths', '/foo/{bar}'],
range: {
end: {
character: 15,
line: 20,
character: 5,
line: 21,
},
start: {
character: 10,
line: 1,
character: 18,
line: 12,
},
},
severity: DiagnosticSeverity.Error,
Expand Down
17 changes: 10 additions & 7 deletions src/rulesets/oas/functions/__tests__/oasTagDefined.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { rules } from '../../index.json';
import oasTagDefined from '../oasTagDefined';

describe('oasTagDefined', () => {
const s = new Spectral();
let s: Spectral;

s.setFunctions({ oasTagDefined });
s.setRules({
'operation-tag-defined': Object.assign(rules['operation-tag-defined'], {
recommended: true,
type: RuleType[rules['operation-tag-defined'].type],
}),
beforeEach(() => {
s = new Spectral();
s.setFunctions({ oasTagDefined });
s.setRules({
'operation-tag-defined': Object.assign(rules['operation-tag-defined'], {
recommended: true,
type: RuleType[rules['operation-tag-defined'].type],
}),
});
});

test('validate a correct object', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/rulesets/oas/functions/oasPathParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const oasPathParam: IFunction = (targetVal, _options, paths, vals) => {
generateResult(`The paths \`${uniquePaths[normalized]}\` and \`${path}\` are equivalent.`, [
...paths.given,
'paths',
path,
]),
);
} else {
Expand Down

0 comments on commit 0c910db

Please sign in to comment.