Skip to content

Commit

Permalink
refactor(ls): align linting Operation.requestBody with existing rules (
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Jul 25, 2023
1 parent 73c07af commit 6f8174c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 49 deletions.
4 changes: 2 additions & 2 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions packages/apidom-ls/src/config/openapi/operation/lint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,8 +38,8 @@ const lints = [
serversTypeLint,
serversItemsTypeLint,
allowedFieldsLint,
requestBodyOptionsTraceLint,
requestBodyGetHeadDeleteLint,
requestBodyAllowedLint,
requestBodyTentativelyAllowed,
];

export default lints;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
32 changes: 5 additions & 27 deletions packages/apidom-ls/src/services/validation/linter-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-ls/test/openapi-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/apidom-ls/test/openapi-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
},
Expand Down
12 changes: 6 additions & 6 deletions packages/apidom-ls/test/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
},
Expand All @@ -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: {},
},
Expand All @@ -3094,23 +3094,23 @@ 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: {},
},
{
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: {},
},
{
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: {},
},
Expand All @@ -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: {},
},
Expand Down

0 comments on commit 6f8174c

Please sign in to comment.