From 4df43a40f8beffe7b5665a343ec44a0a9856876d Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Fri, 12 Jan 2024 09:22:10 +0100 Subject: [PATCH] feat(ls): add rules for OpenAPI 2.0 Security Definitions Object Refs #3618 --- packages/apidom-ls/src/config/codes.ts | 3 +++ .../apidom-ls/src/config/openapi/config.ts | 2 ++ .../security-definitions/documentation.ts | 10 ++++++++++ .../security-definitions/lint/index.ts | 5 +++++ .../security-definitions/lint/values--type.ts | 19 +++++++++++++++++++ .../openapi/security-definitions/meta.ts | 10 ++++++++++ 6 files changed, 49 insertions(+) create mode 100644 packages/apidom-ls/src/config/openapi/security-definitions/documentation.ts create mode 100644 packages/apidom-ls/src/config/openapi/security-definitions/lint/index.ts create mode 100644 packages/apidom-ls/src/config/openapi/security-definitions/lint/values--type.ts create mode 100644 packages/apidom-ls/src/config/openapi/security-definitions/meta.ts diff --git a/packages/apidom-ls/src/config/codes.ts b/packages/apidom-ls/src/config/codes.ts index b1bcbd524..03c9e36ce 100644 --- a/packages/apidom-ls/src/config/codes.ts +++ b/packages/apidom-ls/src/config/codes.ts @@ -772,6 +772,9 @@ enum ApilintCodes { OPENAPI2_RESPONSES_DEFINITIONS = 3160000, OPENAPI2_RESPONSES_DEFINITIONS_VALUES_TYPE = 3160100, + OPENAPI2_SECURITY_DEFINITIONS = 3170000, + OPENAPI2_SECURITY_DEFINITIONS_VALUES_TYPE = 3170100, + OPENAPI3_0 = 5000000, OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_0 = 5000100, diff --git a/packages/apidom-ls/src/config/openapi/config.ts b/packages/apidom-ls/src/config/openapi/config.ts index 9ad24873b..b6e38ab53 100644 --- a/packages/apidom-ls/src/config/openapi/config.ts +++ b/packages/apidom-ls/src/config/openapi/config.ts @@ -32,6 +32,7 @@ import responsesMeta from './responses/meta'; import responsesDefinitionsMeta from './responses-definitions/meta'; import securityRequirementMeta from './security-requirement/meta'; import securitySchemeMeta from './security-scheme/meta'; +import securityDefinitionsMeta from './security-definitions/meta'; import serverMeta from './server/meta'; import serverVariableMeta from './server-variable/meta'; import swaggerMeta from './swagger/meta'; @@ -87,6 +88,7 @@ export default { responsesDefinitions: responsesDefinitionsMeta, securityRequirement: securityRequirementMeta, securityScheme: securitySchemeMeta, + securityDefinitions: securityDefinitionsMeta, server: serverMeta, serverVariable: serverVariableMeta, swagger: swaggerMeta, diff --git a/packages/apidom-ls/src/config/openapi/security-definitions/documentation.ts b/packages/apidom-ls/src/config/openapi/security-definitions/documentation.ts new file mode 100644 index 000000000..f51781438 --- /dev/null +++ b/packages/apidom-ls/src/config/openapi/security-definitions/documentation.ts @@ -0,0 +1,10 @@ +import { OpenAPI2 } from '../target-specs'; + +const documentation = [ + { + docs: '#### [Security Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#securityDefinitionsObject)\n\nA declaration of the security schemes available to be used in the specification. This does not enforce the security schemes on the operations and only serves to provide the relevant details for each scheme.\n\n##### Patterned Fields\nField Pattern | Type | Description\n---|:---:|---\n{name} | [Security Scheme Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#securitySchemeObject) | A single security scheme definition, mapping a "name" to the scheme it defines.\n\n##### Security Definitions Object Example\n\n```js\n{\n "api_key": {\n "type": "apiKey",\n "name": "api_key",\n "in": "header"\n },\n "petstore_auth": {\n "type": "oauth2",\n "authorizationUrl": "http://swagger.io/api/oauth/dialog",\n "flow": "implicit",\n "scopes": {\n "write:pets": "modify pets in your account",\n "read:pets": "read your pets"\n }\n }\n}\n```\n\n\n\\\nYAML\n```yaml\napi_key:\n type: apiKey\n name: api_key\n in: header\npetstore_auth:\n type: oauth2\n authorizationUrl: http://swagger.io/api/oauth/dialog\n flow: implicit\n scopes:\n write:pets: modify pets in your account\n read:pets: read your pets\n```', + targetSpecs: OpenAPI2, + }, +]; + +export default documentation; diff --git a/packages/apidom-ls/src/config/openapi/security-definitions/lint/index.ts b/packages/apidom-ls/src/config/openapi/security-definitions/lint/index.ts new file mode 100644 index 000000000..f49a5fe79 --- /dev/null +++ b/packages/apidom-ls/src/config/openapi/security-definitions/lint/index.ts @@ -0,0 +1,5 @@ +import valuesTypeLint from './values--type'; + +const lints = [valuesTypeLint]; + +export default lints; diff --git a/packages/apidom-ls/src/config/openapi/security-definitions/lint/values--type.ts b/packages/apidom-ls/src/config/openapi/security-definitions/lint/values--type.ts new file mode 100644 index 000000000..7410c417b --- /dev/null +++ b/packages/apidom-ls/src/config/openapi/security-definitions/lint/values--type.ts @@ -0,0 +1,19 @@ +import { DiagnosticSeverity } from 'vscode-languageserver-types'; + +import ApilintCodes from '../../../codes'; +import { LinterMeta } from '../../../../apidom-language-types'; +import { OpenAPI2 } from '../../target-specs'; + +const valuesTypeLint: LinterMeta = { + code: ApilintCodes.OPENAPI2_SECURITY_DEFINITIONS_VALUES_TYPE, + source: 'apilint', + message: 'Security Definitions Object values must be of Security Scheme Object shape', + severity: DiagnosticSeverity.Error, + linterFunction: 'apilintChildrenOfElementsOrClasses', + linterParams: [['securityScheme']], + marker: 'key', + data: {}, + targetSpecs: OpenAPI2, +}; + +export default valuesTypeLint; diff --git a/packages/apidom-ls/src/config/openapi/security-definitions/meta.ts b/packages/apidom-ls/src/config/openapi/security-definitions/meta.ts new file mode 100644 index 000000000..687725779 --- /dev/null +++ b/packages/apidom-ls/src/config/openapi/security-definitions/meta.ts @@ -0,0 +1,10 @@ +import lint from './lint'; +import documentation from './documentation'; +import { FormatMeta } from '../../../apidom-language-types'; + +const meta: FormatMeta = { + lint, + documentation, +}; + +export default meta;