Skip to content

Commit

Permalink
feat(ls): add rules for OpenAPI 2.0 License Object
Browse files Browse the repository at this point in the history
Refs #3104
  • Loading branch information
char0n committed Dec 18, 2023
1 parent cf4fd26 commit db639e8
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
5 changes: 5 additions & 0 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions packages/apidom-ls/src/config/openapi/license/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
13 changes: 11 additions & 2 deletions packages/apidom-ls/src/config/openapi/license/documentation.ts
Original file line number Diff line number Diff line change
@@ -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.',
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ 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',
severity: DiagnosticSeverity.Error,
linterFunction: 'allowedFields',
linterParams: [['name', 'url'], 'x-'],
marker: 'key',
targetSpecs: OpenAPI30,
targetSpecs: [...OpenAPI2, ...OpenAPI30],
};

export default allowedFields3_0Lint;
export default allowedFields2_0__3_0Lint;
4 changes: 2 additions & 2 deletions packages/apidom-ls/src/config/openapi/license/lint/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,7 +12,7 @@ const lints = [
identifierTypeLint,
identifierMutuallyExclusiveLint,
urlFormatURILint,
allowedFields3_0Lint,
allowedFields2_0__3_0Lint,
allowedFields3_1Lint,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,7 +22,7 @@ const nameRequiredLint: LinterMeta = {
},
],
},
targetSpecs: OpenAPI3,
targetSpecs: OpenAPI,
};

export default nameRequiredLint;
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,7 +14,7 @@ const nameTypeLint: LinterMeta = {
marker: 'value',
target: 'name',
data: {},
targetSpecs: OpenAPI3,
targetSpecs: OpenAPI,
};

export default nameTypeLint;
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit db639e8

Please sign in to comment.