Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set proper document source if legacy parsed result is given #1040

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
}),
]);
});
});
});
4 changes: 2 additions & 2 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/spectral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@P0lip That changes looks strange, unrelated to the gist of this PR.

Considering the code of normalizeSource() is

const normalizeSource = (source: Optional<string>): string | null => {
  if (source === void 0) return null;
  return source && !startsWithProtocol(source) ? normalize(source) : source;
};

I wonder, are we also fixing an existing bug? Shouldn't this deserve a slight test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So actually it's not a bug. The typings are correct, but for whatever reason ts didn't complain about it.
Source can either be null or string, not undefined, so the condition was simply wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typings are correct, but for whatever reason ts didn't complain about it.

Weird... 👍

(document as Omit<Document, 'source'> & { source: string }).source = opts.resolve?.documentUri;
}

Expand Down