From 65c57abe19336145fe247b78bc91151e17142d5c Mon Sep 17 00:00:00 2001 From: nulltoken Date: Mon, 24 Feb 2020 08:58:04 +0100 Subject: [PATCH] fixup! WIP --- src/cli/services/linter/utils/getRuleset.ts | 1 + src/formats/openapi.ts | 2 +- .../aas/__tests__/streetlights.jest.test.ts | 127 +----------------- src/rulesets/aas/index.json | 2 +- 4 files changed, 8 insertions(+), 124 deletions(-) diff --git a/src/cli/services/linter/utils/getRuleset.ts b/src/cli/services/linter/utils/getRuleset.ts index 223bdd53e1..ffde38e5de 100644 --- a/src/cli/services/linter/utils/getRuleset.ts +++ b/src/cli/services/linter/utils/getRuleset.ts @@ -21,4 +21,5 @@ export async function getRuleset(rulesetFile: Optional) { return await (rulesetFiles ? loadRulesets(process.cwd(), Array.isArray(rulesetFiles) ? rulesetFiles : [rulesetFiles]) : readRuleset('spectral:oas')); + // TODO: Should there be a default, now? } diff --git a/src/formats/openapi.ts b/src/formats/openapi.ts index f3ff4acb86..5d2e2b85d4 100644 --- a/src/formats/openapi.ts +++ b/src/formats/openapi.ts @@ -1,4 +1,4 @@ -import { bearsAStringPropertyNamed } from './bearsAStringPropertyNamed'; +import { isObject } from 'lodash'; type MaybeOAS2 = Partial<{ swagger: unknown }>; type MaybeOAS3 = Partial<{ openapi: unknown }>; diff --git a/src/rulesets/aas/__tests__/streetlights.jest.test.ts b/src/rulesets/aas/__tests__/streetlights.jest.test.ts index f4cd5a903c..b3457bbb02 100644 --- a/src/rulesets/aas/__tests__/streetlights.jest.test.ts +++ b/src/rulesets/aas/__tests__/streetlights.jest.test.ts @@ -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$'), - }, - ]); - }); }); }); diff --git a/src/rulesets/aas/index.json b/src/rulesets/aas/index.json index 0af7d7ca35..a9357e275c 100644 --- a/src/rulesets/aas/index.json +++ b/src/rulesets/aas/index.json @@ -274,7 +274,7 @@ "function": "schema", "functionOptions": { "schema": { - "$ref": "./schemas/asyncapi-2.0.0.json" + "$ref": "./schemas/schema.aas2.json" } } }