Skip to content

Commit

Permalink
fix: write detection from scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Dec 3, 2019
1 parent 59daebd commit ee47cc1
Show file tree
Hide file tree
Showing 9 changed files with 397 additions and 135 deletions.
8 changes: 8 additions & 0 deletions src/__tests__/__fixtures__/gh-658/URIError.yaml
Expand Up @@ -28,9 +28,17 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
description: No Bueno.
content:
application/json:
schema:
$ref: "./lib.yaml#/components/schemas/Error"
components:
schemas:
Error:
$ref: "#/components/schemas/Baz"
Baz:
$ref: ./lib.yaml#/components/schemas/Error
Foo:
type: string
4 changes: 4 additions & 0 deletions src/__tests__/__fixtures__/gh-658/lib.yaml
Expand Up @@ -14,7 +14,11 @@ components:
properties:
error:
type: string
maxLength: 1
error_description:
type: string
maxLength: 1
status_code:
type: string
test:
$ref: ./URIError.yaml#/components/schemas/Foo
266 changes: 196 additions & 70 deletions src/__tests__/spectral.jest.test.ts
Expand Up @@ -105,6 +105,7 @@ describe('Spectral', () => {
const parsed = {
parsed: parseWithPointers(fs.readFileSync(documentUri, 'utf8')),
getLocationForJsonPath,
source: documentUri,
};

const results = await spectral.run(parsed, {
Expand All @@ -128,7 +129,7 @@ describe('Spectral', () => {
line: 16,
},
},
source: undefined,
source: documentUri,
}),
expect.objectContaining({
code: 'oas2-schema',
Expand Down Expand Up @@ -158,7 +159,7 @@ describe('Spectral', () => {
line: 10,
},
},
source: undefined,
source: documentUri,
}),
expect.objectContaining({
code: 'info-contact',
Expand All @@ -173,7 +174,7 @@ describe('Spectral', () => {
line: 2,
},
},
source: undefined,
source: documentUri,
}),
expect.objectContaining({
code: 'operation-description',
Expand All @@ -188,13 +189,13 @@ describe('Spectral', () => {
line: 11,
},
},
source: undefined,
source: documentUri,
}),
]),
);
});

test('should recognize the source of remote $refs', () => {
test('should recognize the source of remote $refs', async () => {
const s = new Spectral({ resolver: httpAndFileResolver });
const documentUri = path.join(__dirname, './__fixtures__/gh-658/URIError.yaml');

Expand All @@ -203,81 +204,206 @@ describe('Spectral', () => {
severity: DiagnosticSeverity.Warning,
recommended: true,
message: "String typed properties MUST be further described using 'maxLength'. Error: {{error}}",
given: "$..*[?(@.type === 'string')]",
given: "$..[?(@.type === 'string')]",
then: {
field: 'maxLength',
function: 'truthy',
},
},
});

return expect(s.run(fs.readFileSync(documentUri, 'utf8'), { resolve: { documentUri } })).resolves.toEqual(
expect.arrayContaining([
expect.objectContaining({
code: 'schema-strings-maxLength',
path: ['paths', '/test', 'get', 'responses', '200', 'content', 'application/json', 'schema'],
range: {
end: {
character: 28,
line: 23,
},
start: {
character: 21,
line: 22,
},
const results = await s.run(fs.readFileSync(documentUri, 'utf8'), { resolve: { documentUri } });

return expect(results).toEqual([
expect.objectContaining({
path: ['paths', '/test', 'get', 'responses', '200', 'content', 'application/json', 'schema'],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/URIError.yaml'),
range: {
end: {
character: 28,
line: 23,
},
severity: DiagnosticSeverity.Warning,
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/URIError.yaml'),
}),
expect.objectContaining({
code: 'schema-strings-maxLength',
path: ['components', 'schemas', 'Baz', 'properties', 'error'],
range: {
end: {
character: 22,
line: 15,
},
start: {
character: 14,
line: 14,
},
start: {
character: 21,
line: 22,
},
severity: DiagnosticSeverity.Warning,
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/lib.yaml'),
}),
expect.objectContaining({
code: 'schema-strings-maxLength',
path: ['components', 'schemas', 'Baz', 'properties', 'error_description'],
range: {
end: {
character: 22,
line: 17,
},
start: {
character: 26,
line: 16,
},
},
}),

expect.objectContaining({
path: [
'paths',
'/test',
'get',
'responses',
'400',
'content',
'application/json',
'schema',
'properties',
'status_code',
],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/lib.yaml'),
range: {
end: {
character: 22,
line: 21,
},
severity: DiagnosticSeverity.Warning,
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/lib.yaml'),
}),
expect.objectContaining({
code: 'schema-strings-maxLength',
path: ['components', 'schemas', 'Baz', 'properties', 'status_code'],
range: {
end: {
character: 22,
line: 19,
},
start: {
character: 20,
line: 18,
},
start: {
character: 20,
line: 20,
},
severity: DiagnosticSeverity.Warning,
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/lib.yaml'),
}),
]),
);
},
}),
expect.objectContaining({
path: [
'paths',
'/test',
'get',
'responses',
'400',
'content',
'application/json',
'schema',
'properties',
'test',
],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/URIError.yaml'),
range: {
end: {
character: 18,
line: 43,
},
start: {
character: 8,
line: 42,
},
},
}),

expect.objectContaining({
path: [
'paths',
'/test',
'get',
'responses',
'500',
'content',
'application/json',
'schema',
'properties',
'status_code',
],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/lib.yaml'),
range: {
end: {
character: 22,
line: 21,
},
start: {
character: 20,
line: 20,
},
},
}),
expect.objectContaining({
path: [
'paths',
'/test',
'get',
'responses',
'500',
'content',
'application/json',
'schema',
'properties',
'test',
],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/URIError.yaml'),
range: {
end: {
character: 18,
line: 43,
},
start: {
character: 8,
line: 42,
},
},
}),

expect.objectContaining({
path: ['components', 'schemas', 'Foo'],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/URIError.yaml'),
range: {
end: {
character: 18,
line: 43,
},
start: {
character: 8,
line: 42,
},
},
}),

expect.objectContaining({
path: ['components', 'schemas', 'Error', 'properties', 'status_code'],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/lib.yaml'),
range: {
end: {
character: 22,
line: 21,
},
start: {
character: 20,
line: 20,
},
},
}),
expect.objectContaining({
path: ['components', 'schemas', 'Error', 'properties', 'test'],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/URIError.yaml'),
range: {
end: {
character: 18,
line: 43,
},
start: {
character: 8,
line: 42,
},
},
}),

expect.objectContaining({
path: ['components', 'schemas', 'Baz', 'properties', 'status_code'],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/lib.yaml'),
range: {
end: {
character: 22,
line: 21,
},
start: {
character: 20,
line: 20,
},
},
}),
expect.objectContaining({
path: ['components', 'schemas', 'Baz', 'properties', 'test'],
source: expect.stringContaining('/src/__tests__/__fixtures__/gh-658/URIError.yaml'),
range: {
end: {
character: 18,
line: 43,
},
start: {
character: 8,
line: 42,
},
},
}),
]);
});
});
7 changes: 7 additions & 0 deletions src/cli/services/__tests__/linter.test.ts
Expand Up @@ -55,6 +55,7 @@ describe('Linter service', () => {
expect.arrayContaining([
expect.objectContaining({
code: 'oas3-schema',
path: ['info', 'contact', 'name'],
range: {
end: {
character: 14,
Expand Down Expand Up @@ -709,6 +710,12 @@ describe('Linter service', () => {
describe('--resolver', () => {
it('uses provided resolver for $ref resolving', async () => {
expect(await run(`lint --resolver ${fooResolver} ${fooDocument}`)).toEqual([
expect.objectContaining({
code: 'info-contact',
}),
expect.objectContaining({
code: 'info-description',
}),
expect.objectContaining({
code: 'oas3-api-servers',
}),
Expand Down

0 comments on commit ee47cc1

Please sign in to comment.