Skip to content

Commit 7cd71f6

Browse files
authored
feat(ls): add rules for OpenAPI 2.0 Security Requirement Object (#3667)
Refs #3621
1 parent 1f539a3 commit 7cd71f6

File tree

5 files changed

+37
-8
lines changed

5 files changed

+37
-8
lines changed

packages/apidom-ls/src/config/codes.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,9 @@ enum ApilintCodes {
791791
OPENAPI2_XML_FIELD_ATTRIBUTE_TYPE = 3200400,
792792
OPENAPI2_XML_FIELD_WRAPPED_TYPE = 3200500,
793793

794+
OPENAPI2_SECURITY_REQUIREMENT = 3210000,
795+
OPENAPI2_SECURITY_REQUIREMENT_KEYS_DEFINED,
796+
794797
OPENAPI3_0 = 5000000,
795798

796799
OPENAPI3_0_OPENAPI_VALUE_PATTERN_3_0_0 = 5000100,
@@ -866,8 +869,8 @@ enum ApilintCodes {
866869
OPENAPI3_0_PATHS = 5090000,
867870
OPENAPI3_0_PATHS_VALUES_TYPE,
868871

869-
OPENAPI_3_0_SECURITY_REQUIREMENT = 5100000,
870-
OPENAPI_3_0_SECURITY_REQUIREMENT_KEYS_DEFINED,
872+
OPENAPI3_0_SECURITY_REQUIREMENT = 5100000,
873+
OPENAPI3_0_SECURITY_REQUIREMENT_KEYS_DEFINED,
871874

872875
OPENAPI3_0_TAG = 5110000,
873876
OPENAPI3_0_TAG_FIELD_NAME_TYPE = 5110100,

packages/apidom-ls/src/config/openapi/security-requirement/documentation.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { OpenAPI30, OpenAPI31 } from '../target-specs';
1+
import { OpenAPI2, OpenAPI30, OpenAPI31 } from '../target-specs';
22

33
const documentation = [
4+
{
5+
docs: '#### [Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#security-requirement-object)\n\nLists the required security schemes to execute this operation. The object can have multiple security schemes declared in it which are all required (that is, there is a logical AND between the schemes).\n\nThe name used for each property MUST correspond to a security scheme declared in the [Security Definitions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.m#securityDefinitionsObject).\n\n##### Patterned Fields\n\nField Pattern | Type | Description\n---|:---:|---\n{name} | [`string`] | Each name must correspond to a security scheme which is declared in the [Security Definitions](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.m#securityDefinitions). If the security scheme is of type `"oauth2"`, then the value is a list of scope names required for the execution. For other security scheme types, the array MUST be empty.\n\n##### Security Requirement Object Examples\n\n###### Non-OAuth2 Security Requirement\n\n```js\n{\n "api_key": []\n}\n```\n\n\n\\\nYAML\n```yaml\napi_key: []\n```\n\n###### OAuth2 Security Requirement\n\n```js\n{\n "petstore_auth": [\n "write:pets",\n "read:pets"\n ]\n}\n```\n\n```yaml\npetstore_auth:\n- write:pets\n- read:pets\n```',
6+
targetSpecs: OpenAPI2,
7+
},
48
{
59
docs: '#### [Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#securityRequirementObject)\n\nLists the required security schemes to execute this operation.\nThe name used for each property MUST correspond to a security scheme declared in the [Security Schemes](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#componentsSecuritySchemes) under the [Components Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#componentsObject).\n\nSecurity Requirement Objects that contain multiple schemes require that all schemes MUST be satisfied for a request to be authorized.\nThis enables support for scenarios where multiple query parameters or HTTP headers are required to convey security information.\n\nWhen a list of Security Requirement Objects is defined on the [OpenAPI Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#oasObject) or [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#operationObject), only one of the Security Requirement Objects in the list needs to be satisfied to authorize the request.\n\n##### Patterned Fields\n\nField Pattern | Type | Description\n---|:---:|---\n{name} | [`string`] | Each name MUST correspond to a security scheme which is declared in the [Security Schemes](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#componentsSecuritySchemes) under the [Components Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#componentsObject). If the security scheme is of type `"oauth2"` or `"openIdConnect"`, then the value is a list of scope names required for the execution, and the list MAY be empty if authorization does not require a specified scope. For other security scheme types, the array MUST be empty.\n\n##### Security Requirement Object Examples\n\n###### Non-OAuth2 Security Requirement\n\n\n\\\nJSON\n```json\n{\n "api_key": []\n}\n```\n\n\n\\\nYAML\n```yaml\napi_key: []\n```\n\n###### OAuth2 Security Requirement\n\n```json\n{\n "petstore_auth": [\n "write:pets",\n "read:pets"\n ]\n}\n```\n\n```yaml\npetstore_auth:\n- write:pets\n- read:pets\n```\n\n###### Optional OAuth2 Security\n\nOptional OAuth2 security as would be defined in an [OpenAPI Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#openapi-object) or an [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#operation-object):\n\n```json\n{\n "security": [\n {},\n {\n "petstore_auth": [\n "write:pets",\n "read:pets"\n ]\n }\n ]\n}\n```\n\n```yaml\nsecurity:\n - {}\n - petstore_auth:\n - write:pets\n - read:pets\n```',
610
targetSpecs: OpenAPI30,
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import keysDefinedLint from './keys--defined';
1+
import keysDefined2_0Lint from './keys--defined-2-0';
2+
import keysDefined3_0__3_1Lint from './keys--defined-3-0--3-1';
23

3-
const lints = [keysDefinedLint];
4+
const lints = [keysDefined2_0Lint, keysDefined3_0__3_1Lint];
45

56
export default lints;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { DiagnosticSeverity } from 'vscode-languageserver-types';
2+
3+
import ApilintCodes from '../../../codes';
4+
import { LinterMeta } from '../../../../apidom-language-types';
5+
import { OpenAPI2 } from '../../target-specs';
6+
7+
// eslint-disable-next-line @typescript-eslint/naming-convention
8+
const keysDefined2_0Lint: LinterMeta = {
9+
code: ApilintCodes.OPENAPI2_SECURITY_REQUIREMENT_KEYS_DEFINED,
10+
source: 'apilint',
11+
message: 'security keys must be included in defined security schemes',
12+
severity: DiagnosticSeverity.Error,
13+
linterFunction: 'apilintKeysIncluded',
14+
linterParams: ['root.securityDefinitions'],
15+
marker: 'key',
16+
data: {},
17+
targetSpecs: OpenAPI2,
18+
};
19+
20+
export default keysDefined2_0Lint;

packages/apidom-ls/src/config/openapi/security-requirement/lint/keys--defined.ts renamed to packages/apidom-ls/src/config/openapi/security-requirement/lint/keys--defined-3-0--3-1.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import ApilintCodes from '../../../codes';
44
import { LinterMeta } from '../../../../apidom-language-types';
55
import { OpenAPI3 } from '../../target-specs';
66

7-
const keysDefinedLint: LinterMeta = {
8-
code: ApilintCodes.OPENAPI_3_0_SECURITY_REQUIREMENT_KEYS_DEFINED,
7+
// eslint-disable-next-line @typescript-eslint/naming-convention
8+
const keysDefined3_0__3_1Lint: LinterMeta = {
9+
code: ApilintCodes.OPENAPI3_0_SECURITY_REQUIREMENT_KEYS_DEFINED,
910
source: 'apilint',
1011
message: 'security keys must be included in defined security schemes',
1112
severity: DiagnosticSeverity.Error,
@@ -16,4 +17,4 @@ const keysDefinedLint: LinterMeta = {
1617
targetSpecs: OpenAPI3,
1718
};
1819

19-
export default keysDefinedLint;
20+
export default keysDefined3_0__3_1Lint;

0 commit comments

Comments
 (0)