Skip to content

Commit b4c3c11

Browse files
authored
feat(cli): use Content-Type header to detect ruleset format (#2272)
1 parent f3fb425 commit b4c3c11

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"@stoplight/spectral-parsers": "^1.0.1",
4141
"@stoplight/spectral-ref-resolver": "1.0.2",
4242
"@stoplight/spectral-ruleset-bundler": "^1.0.0",
43-
"@stoplight/spectral-ruleset-migrator": "^1.5.0",
43+
"@stoplight/spectral-ruleset-migrator": "^1.9.0",
4444
"@stoplight/spectral-rulesets": ">=1",
4545
"@stoplight/spectral-runtime": "^1.1.0",
4646
"@stoplight/types": "12.3.0",

packages/cli/src/services/__tests__/linter.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ describe('Linter service', () => {
313313
.persist()
314314
.get('/ruleset.json')
315315
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
316-
'Content-Type': 'application/yaml',
316+
'Content-Type': 'application/json',
317317
});
318318

319319
const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset.json`);
@@ -326,6 +326,45 @@ describe('Linter service', () => {
326326
]),
327327
);
328328
});
329+
330+
it('fallbacks to Content-Type', async () => {
331+
nock('http://foo.local')
332+
.persist()
333+
.get('/ruleset')
334+
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
335+
'Content-Type': 'application/json',
336+
});
337+
338+
const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset`);
339+
expect(output).toEqual(expect.arrayContaining([expect.objectContaining({ code: 'info-matches-stoplight' })]));
340+
expect(output).toEqual(
341+
expect.not.arrayContaining([
342+
expect.objectContaining({
343+
message: 'Info object should contain `contact` object',
344+
}),
345+
]),
346+
);
347+
});
348+
349+
it('ignores query parameters', async () => {
350+
nock('http://foo.local')
351+
.persist()
352+
.get('/ruleset.json')
353+
.query({ token: 'bar' })
354+
.replyWithFile(200, join(__dirname, '__fixtures__/ruleset.json'), {
355+
'Content-Type': 'text/plain', // GitHub raw like
356+
});
357+
358+
const output = await run(`lint ${validOas3SpecPath} -r http://foo.local/ruleset.json?token=bar`);
359+
expect(output).toEqual(expect.arrayContaining([expect.objectContaining({ code: 'info-matches-stoplight' })]));
360+
expect(output).toEqual(
361+
expect.not.arrayContaining([
362+
expect.objectContaining({
363+
message: 'Info object should contain `contact` object',
364+
}),
365+
]),
366+
);
367+
});
329368
});
330369
});
331370

packages/cli/src/services/linter/utils/getRuleset.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from '@stoplight/path';
55
import * as process from 'process';
66
import { createRequire } from 'module';
77
import { fetch } from '@stoplight/spectral-runtime';
8-
import { migrateRuleset } from '@stoplight/spectral-ruleset-migrator';
8+
import { migrateRuleset, isBasicRuleset } from '@stoplight/spectral-ruleset-migrator';
99
import { bundleRuleset } from '@stoplight/spectral-ruleset-bundler';
1010
import { node } from '@stoplight/spectral-ruleset-bundler/presets/node';
1111
import { stdin } from '@stoplight/spectral-ruleset-bundler/plugins/stdin';
@@ -25,10 +25,6 @@ async function getDefaultRulesetFile(): Promise<Optional<string>> {
2525
return;
2626
}
2727

28-
function isBasicRuleset(filepath: string): boolean {
29-
return /\.(json|ya?ml)$/.test(path.extname(filepath));
30-
}
31-
3228
function isErrorWithCode(error: Error | (Error & { code: unknown })): error is Error & { code: string } {
3329
return 'code' in error && typeof error.code === 'string';
3430
}
@@ -49,7 +45,7 @@ export async function getRuleset(rulesetFile: Optional<string>): Promise<Ruleset
4945
let ruleset: string;
5046

5147
try {
52-
if (isBasicRuleset(rulesetFile)) {
48+
if (await isBasicRuleset(rulesetFile)) {
5349
const migratedRuleset = await migrateRuleset(rulesetFile, {
5450
format: 'esm',
5551
fs,

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,7 +2514,7 @@ __metadata:
25142514
"@stoplight/spectral-parsers": ^1.0.1
25152515
"@stoplight/spectral-ref-resolver": 1.0.2
25162516
"@stoplight/spectral-ruleset-bundler": ^1.0.0
2517-
"@stoplight/spectral-ruleset-migrator": ^1.5.0
2517+
"@stoplight/spectral-ruleset-migrator": ^1.9.0
25182518
"@stoplight/spectral-rulesets": ">=1"
25192519
"@stoplight/spectral-runtime": ^1.1.0
25202520
"@stoplight/types": 12.3.0
@@ -2660,7 +2660,7 @@ __metadata:
26602660
languageName: unknown
26612661
linkType: soft
26622662

2663-
"@stoplight/spectral-ruleset-migrator@^1.5.0, @stoplight/spectral-ruleset-migrator@^1.7.4, @stoplight/spectral-ruleset-migrator@workspace:packages/ruleset-migrator":
2663+
"@stoplight/spectral-ruleset-migrator@^1.7.4, @stoplight/spectral-ruleset-migrator@^1.9.0, @stoplight/spectral-ruleset-migrator@workspace:packages/ruleset-migrator":
26642664
version: 0.0.0-use.local
26652665
resolution: "@stoplight/spectral-ruleset-migrator@workspace:packages/ruleset-migrator"
26662666
dependencies:

0 commit comments

Comments
 (0)