From 9fcf27d8758174e591ac94fb8675c261148a1259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Mon, 30 Mar 2020 13:08:08 +0200 Subject: [PATCH] fix: set proper document source if legacy parsed result is given (#1040) --- src/__tests__/linter.test.ts | 65 ++++++++++++++++++++++++++++++++++++ src/document.ts | 4 +-- src/spectral.ts | 2 +- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/__tests__/linter.test.ts b/src/__tests__/linter.test.ts index 5f1ee0212..e1fdc221b 100644 --- a/src/__tests__/linter.test.ts +++ b/src/__tests__/linter.test.ts @@ -1,6 +1,7 @@ import { Resolver } from '@stoplight/json-ref-resolver'; import { DiagnosticSeverity } from '@stoplight/types'; import { parse } from '@stoplight/yaml'; +import { IParsedResult } from '../document'; import { isOpenApiv2, isOpenApiv3 } from '../formats'; import { mergeRules, readRuleset } from '../rulesets'; import { RuleCollection, Spectral } from '../spectral'; @@ -1131,4 +1132,68 @@ responses:: !!foo expect(results).toEqual([expect.objectContaining({ code: 'no-info' })]); }); }); + + describe('legacy parsed document', () => { + beforeEach(() => { + spectral.setRules({ + 'falsy-document': { + // some dumb rule to have some error + given: '$', + then: { + function: 'falsy', + }, + }, + }); + }); + + test('should set parsed.source as the source of document', async () => { + const parsedResult: IParsedResult = { + parsed: { + data: {}, + diagnostics: [], + ast: {}, + lineMap: [], + }, + getLocationForJsonPath: jest.fn(), + source: 'foo', + }; + + const results = await spectral.run(parsedResult, { + ignoreUnknownFormat: true, + }); + + expect(results).toEqual([ + expect.objectContaining({ + code: 'falsy-document', + source: 'foo', + }), + ]); + }); + + test('given missing source on parsedResult, should try to set resolveUri as source of the document', async () => { + const parsedResult: IParsedResult = { + parsed: { + data: {}, + diagnostics: [], + ast: {}, + lineMap: [], + }, + getLocationForJsonPath: jest.fn(), + }; + + const results = await spectral.run(parsedResult, { + ignoreUnknownFormat: true, + resolve: { + documentUri: 'foo', + }, + }); + + expect(results).toEqual([ + expect.objectContaining({ + code: 'falsy-document', + source: 'foo', + }), + ]); + }); + }); }); diff --git a/src/document.ts b/src/document.ts index f705717b7..54ab4a196 100644 --- a/src/document.ts +++ b/src/document.ts @@ -66,9 +66,9 @@ export class ParsedDocument; public formats?: string[] | null; - constructor(protected readonly parserResult: R, source?: string) { + constructor(protected readonly parserResult: R) { // we need to normalize the path in case path with forward slashes is given - this.source = normalizeSource(source); + this.source = normalizeSource(parserResult.source); this.diagnostics = formatParserDiagnostics(this.parserResult.parsed.diagnostics, this.source); } diff --git a/src/spectral.ts b/src/spectral.ts index 4fb6f6811..68c161c1b 100644 --- a/src/spectral.ts +++ b/src/spectral.ts @@ -73,7 +73,7 @@ export class Spectral { opts.resolve?.documentUri, ); - if (document.source === void 0 && opts.resolve?.documentUri !== void 0) { + if (document.source === null && opts.resolve?.documentUri !== void 0) { (document as Omit & { source: string }).source = opts.resolve?.documentUri; }