Skip to content

Commit

Permalink
fix(ls): temporary quick fix false positive for schema reference
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Jul 24, 2023
1 parent 70a359f commit 18b02fb
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ enum ApilintCodes {
SCHEMA_EXAMPLES,
SCHEMA_EXTERNAL_DOCS,
SCHEMA_EXAMPLE_DEPRECATED,
SCHEMA_ALLOF_OPENAPI_3_0,
SCHEMA_ANYOF_OPENAPI_3_0,
SCHEMA_ONEOF_OPENAPI_3_0,

DUPLICATE_KEYS = 14999,
NOT_ALLOWED_FIELDS = 15000,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

/*
TODO remove specific rule fo OAS 3.0 when underlying issue has been fixed (missing referenced-element in ref)
see
https://github.com/swagger-api/swagger-editor/issues/3722
https://github.com/swagger-api/swagger-editor/issues/4026
*/
const allOfTypeLint: LinterMeta = {
code: ApilintCodes.SCHEMA_ALLOF_OPENAPI_3_0,
source: 'apilint',
message: 'allOf must be a non-empty array of schemas',
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintArrayOfElementsOrClasses',
linterParams: [['schema', 'reference'], true],
marker: 'key',
target: 'allOf',
data: {},
targetSpecs: [
{ namespace: 'openapi', version: '3.0.0' },
{ namespace: 'openapi', version: '3.0.1' },
{ namespace: 'openapi', version: '3.0.2' },
{ namespace: 'openapi', version: '3.0.3' },
],
};

export default allOfTypeLint;
16 changes: 16 additions & 0 deletions packages/apidom-ls/src/config/common/schema/lint/all-of--type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types';
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

/*
TODO remove targetSpecs when underlying issue has been fixed (missing referenced-element in ref)
see
https://github.com/swagger-api/swagger-editor/issues/3722
https://github.com/swagger-api/swagger-editor/issues/4026
*/
const allOfTypeLint: LinterMeta = {
code: ApilintCodes.SCHEMA_ALLOF,
source: 'apilint',
Expand All @@ -13,6 +19,16 @@ const allOfTypeLint: LinterMeta = {
marker: 'key',
target: 'allOf',
data: {},
targetSpecs: [
{ namespace: 'asyncapi', version: '2.0.0' },
{ namespace: 'asyncapi', version: '2.1.0' },
{ namespace: 'asyncapi', version: '2.2.0' },
{ namespace: 'asyncapi', version: '2.3.0' },
{ namespace: 'asyncapi', version: '2.4.0' },
{ namespace: 'asyncapi', version: '2.5.0' },
{ namespace: 'asyncapi', version: '2.6.0' },
{ namespace: 'openapi', version: '3.1.0' },
],
};

export default allOfTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

/*
TODO remove specific rule fo OAS 3.0 when underlying issue has been fixed (missing referenced-element in ref)
see
https://github.com/swagger-api/swagger-editor/issues/3722
https://github.com/swagger-api/swagger-editor/issues/4026
*/
const allOfTypeLint: LinterMeta = {
code: ApilintCodes.SCHEMA_ANYOF_OPENAPI_3_0,
source: 'apilint',
message: 'anyOf must be a non-empty array of schemas',
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintArrayOfElementsOrClasses',
linterParams: [['schema', 'reference'], true],
marker: 'key',
target: 'anyOf',
data: {},
targetSpecs: [
{ namespace: 'openapi', version: '3.0.0' },
{ namespace: 'openapi', version: '3.0.1' },
{ namespace: 'openapi', version: '3.0.2' },
{ namespace: 'openapi', version: '3.0.3' },
],
};

export default allOfTypeLint;
16 changes: 16 additions & 0 deletions packages/apidom-ls/src/config/common/schema/lint/any-of--type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types';
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

/*
TODO remove targetSpecs when underlying issue has been fixed (missing referenced-element in ref)
see
https://github.com/swagger-api/swagger-editor/issues/3722
https://github.com/swagger-api/swagger-editor/issues/4026
*/
const anyOfTypeLint: LinterMeta = {
code: ApilintCodes.SCHEMA_ANYOF,
source: 'apilint',
Expand All @@ -13,6 +19,16 @@ const anyOfTypeLint: LinterMeta = {
marker: 'key',
target: 'anyOf',
data: {},
targetSpecs: [
{ namespace: 'asyncapi', version: '2.0.0' },
{ namespace: 'asyncapi', version: '2.1.0' },
{ namespace: 'asyncapi', version: '2.2.0' },
{ namespace: 'asyncapi', version: '2.3.0' },
{ namespace: 'asyncapi', version: '2.4.0' },
{ namespace: 'asyncapi', version: '2.5.0' },
{ namespace: 'asyncapi', version: '2.6.0' },
{ namespace: 'openapi', version: '3.1.0' },
],
};

export default anyOfTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

/*
TODO remove specific rule fo OAS 3.0 when underlying issue has been fixed (missing referenced-element in ref)
see
https://github.com/swagger-api/swagger-editor/issues/3722
https://github.com/swagger-api/swagger-editor/issues/4026
*/
const allOfTypeLint: LinterMeta = {
code: ApilintCodes.SCHEMA_ONEOF_OPENAPI_3_0,
source: 'apilint',
message: 'oneOf must be a non-empty array of schemas',
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintArrayOfElementsOrClasses',
linterParams: [['schema', 'reference'], true],
marker: 'key',
target: 'oneOf',
data: {},
targetSpecs: [
{ namespace: 'openapi', version: '3.0.0' },
{ namespace: 'openapi', version: '3.0.1' },
{ namespace: 'openapi', version: '3.0.2' },
{ namespace: 'openapi', version: '3.0.3' },
],
};

export default allOfTypeLint;
16 changes: 16 additions & 0 deletions packages/apidom-ls/src/config/common/schema/lint/one-of--type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

/*
TODO remove targetSpecs when underlying issue has been fixed (missing referenced-element in ref)
see
https://github.com/swagger-api/swagger-editor/issues/3722
https://github.com/swagger-api/swagger-editor/issues/4026
*/
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

Expand All @@ -13,6 +19,16 @@ const oneOfTypeLint: LinterMeta = {
marker: 'key',
target: 'oneOf',
data: {},
targetSpecs: [
{ namespace: 'asyncapi', version: '2.0.0' },
{ namespace: 'asyncapi', version: '2.1.0' },
{ namespace: 'asyncapi', version: '2.2.0' },
{ namespace: 'asyncapi', version: '2.3.0' },
{ namespace: 'asyncapi', version: '2.4.0' },
{ namespace: 'asyncapi', version: '2.5.0' },
{ namespace: 'asyncapi', version: '2.6.0' },
{ namespace: 'openapi', version: '3.1.0' },
],
};

export default oneOfTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
openapi: 3.0.0
info:
title: allOf with one subschema
version: 0.0.1
paths: {}
components:
schemas:
Foo:
type: object
BarAll:
allOf:
- $ref: '#/components/schemas/Foo'
description: something something
BarAny:
anyOf:
- $ref: '#/components/schemas/Foo'
description: something something
BarOne:
oneOf:
- $ref: '#/components/schemas/Foo'
description: something something

26 changes: 26 additions & 0 deletions packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3136,4 +3136,30 @@ describe('apidom-ls-validate', function () {

languageService.terminate();
});

it('oas / yaml - test editor issue 3722 / false positive', async function () {
const validationContext: ValidationContext = {
comments: DiagnosticSeverity.Error,
maxNumberOfProblems: 100,
relatedInformation: false,
};

const spec = fs
.readFileSync(path.join(__dirname, 'fixtures', 'validation', 'oas', 'issue-editor-3722.yaml'))
.toString();
const doc: TextDocument = TextDocument.create(
'foo://bar/issue-editor-3722.yaml',
'yaml',
0,
spec,
);

const languageService: LanguageService = getLanguageService(contextNoSchema);

const result = await languageService.doValidation(doc, validationContext);
const expected: Diagnostic[] = [];
assert.deepEqual(result, expected as Diagnostic[]);

languageService.terminate();
});
});

0 comments on commit 18b02fb

Please sign in to comment.