Repository: stoplightio/spectral
Pull request #1040 (2020-03-29 15:09:58)

fix: set proper document source if legacy parsed result is given
Needed by Studio :grin:
A tiny bug, shouldn't really affect anyone besides Studio.

**Checklist**

- [x] Tests added / updated
- [x] Docs added / updated

**Does this PR introduce a breaking change?**

- [ ] Yes
- [x] No





Patch:
diff --git a/src/document.ts b/src/document.ts
index f705717b..54ab4a19 100644
--- a/src/document.ts
+++ b/src/document.ts
@@ -66,9 +66,9 @@ export class ParsedDocument<D = unknown, R extends IParsedResult = IParsedResult
   public readonly diagnostics: ReadonlyArray<IRuleResult>;
   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 4fb6f681..68c161c1 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<Document, 'source'> & { source: string }).source = opts.resolve?.documentUri;
     }
 


Test patch:
diff --git a/src/__tests__/linter.test.ts b/src/__tests__/linter.test.ts
index 5f1ee021..e1fdc221 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',
+        }),
+      ]);
+    });
+  });
 });