From db639e802ad29b696485f199245f61e26587a878 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Mon, 18 Dec 2023 17:06:09 +0100 Subject: [PATCH] feat(ls): add rules for OpenAPI 2.0 License Object Refs #3104 --- packages/apidom-ls/src/config/codes.ts | 5 +++++ .../src/config/openapi/license/completion.ts | 17 +++++++++++++++-- .../src/config/openapi/license/documentation.ts | 13 +++++++++++-- ...fields-3-0.ts => allowed-fields-2-0--3-0.ts} | 8 ++++---- .../src/config/openapi/license/lint/index.ts | 4 ++-- .../openapi/license/lint/name--required.ts | 6 +++--- .../config/openapi/license/lint/name--type.ts | 6 +++--- .../openapi/license/lint/url--format-uri.ts | 2 +- 8 files changed, 44 insertions(+), 17 deletions(-) rename packages/apidom-ls/src/config/openapi/license/lint/{allowed-fields-3-0.ts => allowed-fields-2-0--3-0.ts} (71%) diff --git a/packages/apidom-ls/src/config/codes.ts b/packages/apidom-ls/src/config/codes.ts index 2063e4940..31874340c 100644 --- a/packages/apidom-ls/src/config/codes.ts +++ b/packages/apidom-ls/src/config/codes.ts @@ -663,6 +663,11 @@ enum ApilintCodes { OPENAPI2_PATH_TEMPLATE_VALUE_WELL_FORMED = 3040100, OPENAPI2_PATH_TEMPLATE_VALUE_VALID, + OPENAPI2_LICENSE = 3050000, + OPENAPI2_LICENSE_FIELD_NAME_TYPE = 3050100, + OPENAPI2_LICENSE_FIELD_NAME_REQUIRED = 3050200, + OPENAPI2_LICENSE_FIELD_URL_FORMAT_URI = 3050300, + OPENAPI3_0 = 5000000, OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_0 = 5000100, diff --git a/packages/apidom-ls/src/config/openapi/license/completion.ts b/packages/apidom-ls/src/config/openapi/license/completion.ts index f17fb0de2..3de480994 100644 --- a/packages/apidom-ls/src/config/openapi/license/completion.ts +++ b/packages/apidom-ls/src/config/openapi/license/completion.ts @@ -3,9 +3,22 @@ import { CompletionFormat, CompletionType, } from '../../../apidom-language-types'; -import { OpenAPI30, OpenAPI31, OpenAPI3 } from '../target-specs'; +import { OpenAPI2, OpenAPI30, OpenAPI31, OpenAPI3 } from '../target-specs'; const completion: ApidomCompletionItem[] = [ + { + label: 'name', + insertText: 'name', + kind: 14, + format: CompletionFormat.QUOTED, + type: CompletionType.PROPERTY, + insertTextFormat: 2, + documentation: { + kind: 'markdown', + value: '**Required**. The license name used for the API.', + }, + targetSpecs: OpenAPI2, + }, { label: 'name', insertText: 'name', @@ -44,7 +57,7 @@ const completion: ApidomCompletionItem[] = [ kind: 'markdown', value: 'A URL to the license used for the API. MUST be in the format of a URL.', }, - targetSpecs: OpenAPI30, + targetSpecs: [...OpenAPI2, ...OpenAPI30], }, { label: 'url', diff --git a/packages/apidom-ls/src/config/openapi/license/documentation.ts b/packages/apidom-ls/src/config/openapi/license/documentation.ts index afc91c327..3971d4a09 100644 --- a/packages/apidom-ls/src/config/openapi/license/documentation.ts +++ b/packages/apidom-ls/src/config/openapi/license/documentation.ts @@ -1,6 +1,11 @@ -import { OpenAPI30, OpenAPI31, OpenAPI3 } from '../target-specs'; +import { OpenAPI2, OpenAPI30, OpenAPI31, OpenAPI3 } from '../target-specs'; const documentation = [ + { + target: 'name', + docs: '**Required.** The license name used for the API.', + targetSpecs: OpenAPI2, + }, { target: 'name', docs: '**REQUIRED**. The license name used for the API.', @@ -14,13 +19,17 @@ const documentation = [ { target: 'url', docs: 'A URL to the license used for the API. MUST be in the format of a URL.', - targetSpecs: OpenAPI30, + targetSpecs: [...OpenAPI2, ...OpenAPI30], }, { target: 'url', docs: 'A URL to the license used for the API. This MUST be in the form of a URL. The `url` field is mutually exclusive of the `identifier` field.', targetSpecs: OpenAPI31, }, + { + docs: '#### [License Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#license-object)\n\nLicense information for the exposed API.\n\n##### Fixed Fields\n\nField Name | Type | Description\n---|:---:|---\nname | `string` | **Required.** The license name used for the API.\nurl | `string` | A URL to the license used for the API. MUST be in the format of a URL.\n\n##### Patterned Objects\n\nField Pattern | Type | Description\n---|:---:|---\n^x- | Any | Allows extensions to the Swagger Schema. The field name MUST begin with `x-`, for example, `x-internal-id`. The value can be `null`, a primitive, an array or an object. See [Vendor Extensions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#vendorExtensions) for further details.\n\n##### License Object Example:\n\n```js\n{\n "name": "Apache 2.0",\n "url": "http://www.apache.org/licenses/LICENSE-2.0.html"\n}\n```\n\n\n\\\nYAML\n```yaml\nname: Apache 2.0\nurl: http://www.apache.org/licenses/LICENSE-2.0.html\n```', + targetSpecs: OpenAPI2, + }, { docs: '#### [License Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#licenseObject)\n\nLicense information for the exposed API.\n\n##### Fixed Fields\n\nField Name | Type | Description\n---|:---:|---\nname | `string` | **REQUIRED**. The license name used for the API.\nurl | `string` | A URL to the license used for the API. MUST be in the format of a URL.\n\nThis object MAY be extended with [Specification Extensions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#specificationExtensions).\n\n##### License Object Example\n\n\n\\\nJSON\n```json\n{\n "name": "Apache 2.0",\n "url": "https://www.apache.org/licenses/LICENSE-2.0.html"\n}\n```\n\n\n\\\nYAML\n```yaml\nname: Apache 2.0\nurl: https://www.apache.org/licenses/LICENSE-2.0.html\n```', targetSpecs: OpenAPI30, diff --git a/packages/apidom-ls/src/config/openapi/license/lint/allowed-fields-3-0.ts b/packages/apidom-ls/src/config/openapi/license/lint/allowed-fields-2-0--3-0.ts similarity index 71% rename from packages/apidom-ls/src/config/openapi/license/lint/allowed-fields-3-0.ts rename to packages/apidom-ls/src/config/openapi/license/lint/allowed-fields-2-0--3-0.ts index 5538ec1f2..e22138c6f 100644 --- a/packages/apidom-ls/src/config/openapi/license/lint/allowed-fields-3-0.ts +++ b/packages/apidom-ls/src/config/openapi/license/lint/allowed-fields-2-0--3-0.ts @@ -2,10 +2,10 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types'; import ApilintCodes from '../../../codes'; import { LinterMeta } from '../../../../apidom-language-types'; -import { OpenAPI30 } from '../../target-specs'; +import { OpenAPI2, OpenAPI30 } from '../../target-specs'; // eslint-disable-next-line @typescript-eslint/naming-convention -const allowedFields3_0Lint: LinterMeta = { +const allowedFields2_0__3_0Lint: LinterMeta = { code: ApilintCodes.NOT_ALLOWED_FIELDS, source: 'apilint', message: 'Object includes not allowed fields', @@ -13,7 +13,7 @@ const allowedFields3_0Lint: LinterMeta = { linterFunction: 'allowedFields', linterParams: [['name', 'url'], 'x-'], marker: 'key', - targetSpecs: OpenAPI30, + targetSpecs: [...OpenAPI2, ...OpenAPI30], }; -export default allowedFields3_0Lint; +export default allowedFields2_0__3_0Lint; diff --git a/packages/apidom-ls/src/config/openapi/license/lint/index.ts b/packages/apidom-ls/src/config/openapi/license/lint/index.ts index c896f897e..14d6a3afa 100644 --- a/packages/apidom-ls/src/config/openapi/license/lint/index.ts +++ b/packages/apidom-ls/src/config/openapi/license/lint/index.ts @@ -1,4 +1,4 @@ -import allowedFields3_0Lint from './allowed-fields-3-0'; +import allowedFields2_0__3_0Lint from './allowed-fields-2-0--3-0'; import allowedFields3_1Lint from './allowed-fields-3-1'; import nameTypeLint from './name--type'; import nameRequiredLint from './name--required'; @@ -12,7 +12,7 @@ const lints = [ identifierTypeLint, identifierMutuallyExclusiveLint, urlFormatURILint, - allowedFields3_0Lint, + allowedFields2_0__3_0Lint, allowedFields3_1Lint, ]; diff --git a/packages/apidom-ls/src/config/openapi/license/lint/name--required.ts b/packages/apidom-ls/src/config/openapi/license/lint/name--required.ts index db623f39d..6b16cd3e9 100644 --- a/packages/apidom-ls/src/config/openapi/license/lint/name--required.ts +++ b/packages/apidom-ls/src/config/openapi/license/lint/name--required.ts @@ -2,10 +2,10 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types'; import ApilintCodes from '../../../codes'; import { LinterMeta } from '../../../../apidom-language-types'; -import { OpenAPI3 } from '../../target-specs'; +import { OpenAPI } from '../../target-specs'; const nameRequiredLint: LinterMeta = { - code: ApilintCodes.OPENAPI3_0_LICENSE_FIELD_NAME_REQUIRED, + code: ApilintCodes.OPENAPI2_LICENSE_FIELD_NAME_REQUIRED, source: 'apilint', message: "should always have a 'name'", severity: DiagnosticSeverity.Error, @@ -22,7 +22,7 @@ const nameRequiredLint: LinterMeta = { }, ], }, - targetSpecs: OpenAPI3, + targetSpecs: OpenAPI, }; export default nameRequiredLint; diff --git a/packages/apidom-ls/src/config/openapi/license/lint/name--type.ts b/packages/apidom-ls/src/config/openapi/license/lint/name--type.ts index 4a2d1f176..c542d02e6 100644 --- a/packages/apidom-ls/src/config/openapi/license/lint/name--type.ts +++ b/packages/apidom-ls/src/config/openapi/license/lint/name--type.ts @@ -2,10 +2,10 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types'; import ApilintCodes from '../../../codes'; import { LinterMeta } from '../../../../apidom-language-types'; -import { OpenAPI3 } from '../../target-specs'; +import { OpenAPI } from '../../target-specs'; const nameTypeLint: LinterMeta = { - code: ApilintCodes.OPENAPI3_0_LICENSE_FIELD_NAME_TYPE, + code: ApilintCodes.OPENAPI2_LICENSE_FIELD_NAME_TYPE, source: 'apilint', message: "'identifier' must be a string", severity: DiagnosticSeverity.Error, @@ -14,7 +14,7 @@ const nameTypeLint: LinterMeta = { marker: 'value', target: 'name', data: {}, - targetSpecs: OpenAPI3, + targetSpecs: OpenAPI, }; export default nameTypeLint; diff --git a/packages/apidom-ls/src/config/openapi/license/lint/url--format-uri.ts b/packages/apidom-ls/src/config/openapi/license/lint/url--format-uri.ts index 11c926b1e..f9f6ed97a 100644 --- a/packages/apidom-ls/src/config/openapi/license/lint/url--format-uri.ts +++ b/packages/apidom-ls/src/config/openapi/license/lint/url--format-uri.ts @@ -5,7 +5,7 @@ import { LinterMeta } from '../../../../apidom-language-types'; import { OpenAPI3 } from '../../target-specs'; const urlFormatURILint: LinterMeta = { - code: ApilintCodes.OPENAPI3_0_LICENSE_FIELD_URL_FORMAT_URI, + code: ApilintCodes.OPENAPI2_LICENSE_FIELD_URL_FORMAT_URI, source: 'apilint', message: "'url' value must be a valid URL", severity: DiagnosticSeverity.Error,