diff --git a/packages/apidom-ls/src/config/codes.ts b/packages/apidom-ls/src/config/codes.ts index 1455c7424..601fe751e 100644 --- a/packages/apidom-ls/src/config/codes.ts +++ b/packages/apidom-ls/src/config/codes.ts @@ -740,6 +740,8 @@ enum ApilintCodes { OPENAPI3_0_OPERATION_FIELD_PARAMETERS_TYPE = 5130600, OPENAPI3_0_OPERATION_FIELD_PARAMETERS_ITEMS_TYPE, OPENAPI3_0_OPERATION_FIELD_REQUEST_BODY_TYPE = 5130700, + OPENAPI3_0_OPERATION_FIELD_REQUEST_BODY_ALLOWED, + OPENAPI3_0_OPERATION_FIELD_REQUEST_BODY_TENTATIVELY_ALLOWED, OPENAPI3_0_OPERATION_FIELD_RESPONSES_TYPE = 5130800, OPENAPI3_0_OPERATION_FIELD_RESPONSES_REQUIRED, OPENAPI3_0_OPERATION_FIELD_CALLBACKS_VALUES_TYPE = 5130900, @@ -780,8 +782,6 @@ enum ApilintCodes { OPENAPI3_0_REQUEST_BODY_FIELD_CONTENT_VALUES_TYPE = 5160300, OPENAPI3_0_REQUEST_BODY_FIELD_CONTENT_REQUIRED, OPENAPI3_0_REQUEST_BODY_FIELD_REQUIRED_TYPE = 5160400, - OPENAPI3_0_REQUEST_BODY_GET_HEAD_DELETE = 5160500, - OPENAPI3_0_REQUEST_BODY_OPTIONS_TRACE, OPENAPI3_0_MEDIA_TYPE = 5170000, OPENAPI3_0_MEDIA_TYPE_FIELD_SCHEMA_VALUES_TYPE = 5170100, diff --git a/packages/apidom-ls/src/config/openapi/operation/lint/index.ts b/packages/apidom-ls/src/config/openapi/operation/lint/index.ts index be727f86e..f5013643b 100644 --- a/packages/apidom-ls/src/config/openapi/operation/lint/index.ts +++ b/packages/apidom-ls/src/config/openapi/operation/lint/index.ts @@ -16,8 +16,8 @@ import securityTypeLint from './security--type'; import securityItemsTypeLint from './security--items-type'; import serversTypeLint from './servers--type'; import serversItemsTypeLint from '../../path-item/lint/servers--items-type'; -import requestBodyGetHeadDeleteLint from './request-body--get-head-delete'; -import requestBodyOptionsTraceLint from './request-body--options-trace'; +import requestBodyAllowedLint from './request-body--allowed'; +import requestBodyTentativelyAllowed from './request-body--tentatively-allowed'; const lints = [ tagsTypeLint, @@ -38,8 +38,8 @@ const lints = [ serversTypeLint, serversItemsTypeLint, allowedFieldsLint, - requestBodyOptionsTraceLint, - requestBodyGetHeadDeleteLint, + requestBodyAllowedLint, + requestBodyTentativelyAllowed, ]; export default lints; diff --git a/packages/apidom-ls/src/config/openapi/operation/lint/request-body--options-trace.ts b/packages/apidom-ls/src/config/openapi/operation/lint/request-body--allowed.ts similarity index 58% rename from packages/apidom-ls/src/config/openapi/operation/lint/request-body--options-trace.ts rename to packages/apidom-ls/src/config/openapi/operation/lint/request-body--allowed.ts index b9f3eef5d..b09b543cc 100644 --- a/packages/apidom-ls/src/config/openapi/operation/lint/request-body--options-trace.ts +++ b/packages/apidom-ls/src/config/openapi/operation/lint/request-body--allowed.ts @@ -3,16 +3,17 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types'; import ApilintCodes from '../../../codes'; import { LinterMeta } from '../../../../apidom-language-types'; -const requestBodyOptionsTraceLint: LinterMeta = { - code: ApilintCodes.OPENAPI3_0_REQUEST_BODY_OPTIONS_TRACE, +const requestBodyAllowedLint: LinterMeta = { + code: ApilintCodes.OPENAPI3_0_OPERATION_FIELD_REQUEST_BODY_ALLOWED, source: 'apilint', message: 'requestBody is not allowed for OPTIONS and TRACE operations', severity: DiagnosticSeverity.Error, - linterFunction: 'apilintOperationRequestBody_OPTIONS_TRACE', + linterFunction: 'apilintOperationRequestBodyAllowed', + linterParams: [['GET', 'HEAD', 'DELETE', 'PUT', 'POST', 'PATCH']], marker: 'key', markerTarget: 'requestBody', target: 'requestBody', data: {}, }; -export default requestBodyOptionsTraceLint; +export default requestBodyAllowedLint; diff --git a/packages/apidom-ls/src/config/openapi/operation/lint/request-body--get-head-delete.ts b/packages/apidom-ls/src/config/openapi/operation/lint/request-body--tentatively-allowed.ts similarity index 57% rename from packages/apidom-ls/src/config/openapi/operation/lint/request-body--get-head-delete.ts rename to packages/apidom-ls/src/config/openapi/operation/lint/request-body--tentatively-allowed.ts index 0b39f94f6..a7d2759c3 100644 --- a/packages/apidom-ls/src/config/openapi/operation/lint/request-body--get-head-delete.ts +++ b/packages/apidom-ls/src/config/openapi/operation/lint/request-body--tentatively-allowed.ts @@ -3,16 +3,17 @@ import { DiagnosticSeverity } from 'vscode-languageserver-types'; import ApilintCodes from '../../../codes'; import { LinterMeta } from '../../../../apidom-language-types'; -const requestBodyGetHeadDeleteLint: LinterMeta = { - code: ApilintCodes.OPENAPI3_0_REQUEST_BODY_GET_HEAD_DELETE, +const requestBodyTentativelyAllowedLint: LinterMeta = { + code: ApilintCodes.OPENAPI3_0_OPERATION_FIELD_REQUEST_BODY_TENTATIVELY_ALLOWED, source: 'apilint', message: 'requestBody does not have well-defined semantics for GET, HEAD and DELETE operations', severity: DiagnosticSeverity.Warning, - linterFunction: 'apilintOperationRequestBody_GET_HEAD_DELETE', + linterFunction: 'apilintOperationRequestBodyAllowed', + linterParams: [['PUT', 'POST', 'PATCH', 'OPTIONS', 'TRACE']], marker: 'key', markerTarget: 'requestBody', target: 'requestBody', data: {}, }; -export default requestBodyGetHeadDeleteLint; +export default requestBodyTentativelyAllowedLint; diff --git a/packages/apidom-ls/src/services/validation/linter-functions.ts b/packages/apidom-ls/src/services/validation/linter-functions.ts index f70e0c441..4dbfa1e79 100644 --- a/packages/apidom-ls/src/services/validation/linter-functions.ts +++ b/packages/apidom-ls/src/services/validation/linter-functions.ts @@ -958,39 +958,17 @@ export const standardLinterfunctions: FunctionItem[] = [ }, }, { - functionName: 'apilintOperationRequestBody_GET_HEAD_DELETE', - function: (element: Element): boolean => { - const operationNode = element?.parent?.parent; - if (!operationNode || operationNode.element !== 'operation') { - return true; - } - const httpMethod = operationNode.getMetaProperty('http-method', '').toValue(); - switch (httpMethod) { - case 'GET': - case 'HEAD': - case 'DELETE': - return false; - default: - return true; - } - return true; - }, - }, - { - functionName: 'apilintOperationRequestBody_OPTIONS_TRACE', - function: (element: Element): boolean => { + functionName: 'apilintOperationRequestBodyAllowed', + function: (element: Element, allowedHttpMethods: string[]): boolean => { const operationNode = element?.parent?.parent; if (!operationNode || operationNode.element !== 'operation') { return true; } const httpMethod = operationNode.getMetaProperty('http-method', '').toValue(); - switch (httpMethod) { - case 'OPTIONS': - case 'TRACE': - return false; - default: - return true; + if (httpMethod && !allowedHttpMethods.includes(httpMethod)) { + return false; } + return true; }, }, diff --git a/packages/apidom-ls/test/openapi-json.ts b/packages/apidom-ls/test/openapi-json.ts index 8a9457f81..c3549f6ae 100644 --- a/packages/apidom-ls/test/openapi-json.ts +++ b/packages/apidom-ls/test/openapi-json.ts @@ -519,7 +519,7 @@ describe('apidom-ls', function () { message: 'requestBody does not have well-defined semantics for GET, HEAD and DELETE operations', severity: 2, - code: 5160500, + code: 5130702, source: 'apilint', data: {}, }, diff --git a/packages/apidom-ls/test/openapi-yaml.ts b/packages/apidom-ls/test/openapi-yaml.ts index af4e0afaa..ac6cd936e 100644 --- a/packages/apidom-ls/test/openapi-yaml.ts +++ b/packages/apidom-ls/test/openapi-yaml.ts @@ -548,7 +548,7 @@ describe('apidom-ls-yaml', function () { message: 'requestBody does not have well-defined semantics for GET, HEAD and DELETE operations', severity: 2, - code: 5160500, + code: 5130702, source: 'apilint', data: {}, }, diff --git a/packages/apidom-ls/test/validate.ts b/packages/apidom-ls/test/validate.ts index e13c4ef05..a664d13dd 100644 --- a/packages/apidom-ls/test/validate.ts +++ b/packages/apidom-ls/test/validate.ts @@ -3076,7 +3076,7 @@ describe('apidom-ls-validate', function () { message: 'requestBody does not have well-defined semantics for GET, HEAD and DELETE operations', severity: 2, - code: 5160500, + code: 5130702, source: 'apilint', data: {}, }, @@ -3085,7 +3085,7 @@ describe('apidom-ls-validate', function () { message: 'requestBody does not have well-defined semantics for GET, HEAD and DELETE operations', severity: 2, - code: 5160500, + code: 5130702, source: 'apilint', data: {}, }, @@ -3094,7 +3094,7 @@ describe('apidom-ls-validate', function () { message: 'requestBody does not have well-defined semantics for GET, HEAD and DELETE operations', severity: 2, - code: 5160500, + code: 5130702, source: 'apilint', data: {}, }, @@ -3102,7 +3102,7 @@ describe('apidom-ls-validate', function () { range: { start: { line: 43, character: 6 }, end: { line: 43, character: 17 } }, message: 'requestBody is not allowed for OPTIONS and TRACE operations', severity: 1, - code: 5160501, + code: 5130701, source: 'apilint', data: {}, }, @@ -3110,7 +3110,7 @@ describe('apidom-ls-validate', function () { range: { start: { line: 50, character: 6 }, end: { line: 50, character: 17 } }, message: 'requestBody is not allowed for OPTIONS and TRACE operations', severity: 1, - code: 5160501, + code: 5130701, source: 'apilint', data: {}, }, @@ -3127,7 +3127,7 @@ describe('apidom-ls-validate', function () { message: 'requestBody does not have well-defined semantics for GET, HEAD and DELETE operations', severity: 2, - code: 5160500, + code: 5130702, source: 'apilint', data: {}, },