Skip to content

Commit

Permalink
fixup! WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Feb 25, 2020
1 parent dfb78da commit 65c57ab
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 124 deletions.
1 change: 1 addition & 0 deletions src/cli/services/linter/utils/getRuleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export async function getRuleset(rulesetFile: Optional<string[]>) {
return await (rulesetFiles
? loadRulesets(process.cwd(), Array.isArray(rulesetFiles) ? rulesetFiles : [rulesetFiles])
: readRuleset('spectral:oas'));
// TODO: Should there be a default, now?
}
2 changes: 1 addition & 1 deletion src/formats/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bearsAStringPropertyNamed } from './bearsAStringPropertyNamed';
import { isObject } from 'lodash';

type MaybeOAS2 = Partial<{ swagger: unknown }>;
type MaybeOAS3 = Partial<{ openapi: unknown }>;
Expand Down
127 changes: 5 additions & 122 deletions src/rulesets/aas/__tests__/streetlights.jest.test.ts
Original file line number Diff line number Diff line change
@@ -1,142 +1,25 @@
import * as path from '@stoplight/path';
import { DiagnosticSeverity } from '@stoplight/types';
import * as nock from 'nock';

import { Document } from '../../../document';
import { isAsyncApiv2 } from '../../../formats';
import { readParsable } from '../../../fs/reader';
import { unreferencedReusableObject } from '../../../functions/unreferencedReusableObject';
import { RuleType, Spectral } from '../../../index';
import { Spectral } from '../../../index';
import * as Parsers from '../../../parsers';
import { httpAndFileResolver } from '../../../resolvers/http-and-file';
import { rules } from '../index.json';

describe('unusedComponentsSchema - Http and fs remote references', () => {
const streetlights = path.join(__dirname, './__fixtures__/streetlights.yaml');
const s = new Spectral({ resolver: httpAndFileResolver });
s.registerFormat('aas2', isAsyncApiv2);

describe('reports unreferenced components schemas', () => {
test('when analyzing an in-memory document', async () => {
nock('https://oas3.library.com')
.get('/defs.json')
.reply(
200,
JSON.stringify({
components: {
schemas: {
ExternalHttp: {
type: 'number',
},
},
},
}),
);

const remoteFsRefeferencePath = path.join(
__dirname,
'../../__tests__/__fixtures__/unusedComponentsSchema.definition.json#/components/schemas/ExternalFs',
);

const doc = `{
"openapi": "3.0.0",
"x-hook": {
"$ref": "#/components/schemas/Hooked"
},
"x-also-hook": {
"$ref": "#/components/schemas/Hooked"
},
"paths": {
"/path": {
"post": {
"parameters": [
{
"$ref": "#/components/schemas/HookedAsWell"
},
{
"$ref": "${remoteFsRefeferencePath}"
},
{
"$ref": "https://oas3.library.com/defs.json#/components/schemas/ExternalHttp"
}
]
}
}
},
"components": {
"schemas": {
"Hooked": {
"type": "object"
},
"HookedAsWell": {
"name": "value",
"in": "query",
"type": "number"
},
"Unhooked": {
"type": "object"
}
}
}
}`;
await s.loadRuleset('spectral:aas');

const results = await s.run(new Document(doc, Parsers.Json));

expect(results).toEqual([
{
code: 'oas3-unused-components-schema',
message: 'Potentially unused components schema has been detected.',
path: ['components', 'schemas', 'Unhooked'],
range: {
end: {
character: 11,
line: 37,
},
start: {
character: 22,
line: 35,
},
},
severity: DiagnosticSeverity.Warning,
},
]);

nock.cleanAll();
});
const document = new Document(await readParsable(streetlights, { encoding: 'utf8' }), Parsers.Yaml, streetlights);

test('when analyzing a directly self-referencing document from the filesystem', async () => {
const fixturePath = path.join(__dirname, '../../__tests__/__fixtures__/unusedComponentsSchema.remoteLocal.json');

const spec = await readParsable(fixturePath, { encoding: 'utf8' });
const results = await s.run(new Document(spec, Parsers.Json, fixturePath));
const results = await s.run(document);

expect(results).toEqual([]);
});

test('when analyzing an indirectly self-referencing document from the filesystem', async () => {
const fixturePath = path.join(__dirname, '../../__tests__/__fixtures__/unusedComponentsSchema.indirect.1.json');

const spec = await readParsable(fixturePath, { encoding: 'utf8' });
const results = await s.run(new Document(spec, Parsers.Json, fixturePath));

expect(results).toEqual([
{
code: 'oas3-unused-components-schema',
message: 'Potentially unused components schema has been detected.',
path: ['components', 'schemas', 'Unhooked'],
range: {
end: {
character: 7,
line: 12,
},
start: {
character: 18,
line: 10,
},
},
severity: DiagnosticSeverity.Warning,
source: expect.stringMatching('/__tests__/__fixtures__/unusedComponentsSchema.indirect.1.json$'),
},
]);
});
});
});
2 changes: 1 addition & 1 deletion src/rulesets/aas/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
"function": "schema",
"functionOptions": {
"schema": {
"$ref": "./schemas/asyncapi-2.0.0.json"
"$ref": "./schemas/schema.aas2.json"
}
}
}
Expand Down

0 comments on commit 65c57ab

Please sign in to comment.