Skip to content

OpenAPI 3.1.0 lint and completion rules for openapi object #2117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
23 changes: 21 additions & 2 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,27 @@ enum ApilintCodes {

OPENAPI3_1_OPENAPI_VALUE_PATTERN_3_1_0 = 7000100,

OPENAPI3_1_INFO = 7010000,
OPENAPI3_1_INFO_FIELD_SUMMARY_TYPE = 7010100,
OPENAPI3_1_OPEN_API = 7010000,
OPENAPI3_1_OPEN_API_REQUIRED_FIELDS = 7010010,
OPENAPI3_1_OPEN_API_FIELD_INFO_TYPE = 7010100,
OPENAPI3_1_OPEN_API_FIELD_INFO_TYPE_REQUIRED,
OPENAPI3_1_OPEN_API_FIELD_SERVERS_TYPE = 7010200,
OPENAPI3_1_OPEN_API_FIELD_SERVERS_ITEMS_TYPE,
OPENAPI3_1_OPEN_API_FIELD_PATHS_TYPE = 7010300,
OPENAPI3_1_OPEN_API_FIELD_PATHS_REQUIRED,
OPENAPI3_1_OPEN_API_FIELD_COMPONENTS_TYPE = 7010400,
OPENAPI3_1_OPEN_API_FIELD_COMPONENTS_REQUIRED,
OPENAPI3_1_OPEN_API_FIELD_SECURITY_TYPE = 7010500,
OPENAPI3_1_OPEN_API_FIELD_SECURITY_ITEMS_TYPE,
OPENAPI3_1_OPEN_API_FIELD_TAGS_TYPE = 7010600,
OPENAPI3_1_OPEN_API_FIELD_TAGS_ITEMS_TYPE,
OPENAPI3_1_OPEN_API_FIELD_EXTERNAL_DOCS_TYPE = 7010700,
OPENAPI3_1_OPEN_API_FIELD_WEBHOOKS_TYPE = 7010800,
OPENAPI3_1_OPEN_API_FIELD_WEBHOOKS_REQUIRED,
OPENAPI3_1_OPEN_API_FIELD_JSON_SCHEMA_FORMAT_URI = 7010900,

OPENAPI3_1_INFO = 7020000,
OPENAPI3_1_INFO_FIELD_SUMMARY_TYPE = 7020100,

ADS = 8000000,
ADS_INFO = 8010000,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Omitted fixed fields:
* - context
* - contact
* - license
*
* Field omission reason: omitted fields do have a non-union type. Thus,
Expand Down
140 changes: 140 additions & 0 deletions packages/apidom-ls/src/config/openapi/openapi3_1/completion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import {
ApidomCompletionItem,
CompletionFormat,
CompletionType,
} from '../../../apidom-language-types';

const completion: ApidomCompletionItem[] = [
{
label: 'openapi',
insertText: 'openapi',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'**REQUIRED**. This string MUST be the [semantic version number](https://semver.org/spec/v2.0.0.html) of the [OpenAPI Specification version](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#versions) that the OpenAPI document uses. The `openapi` field SHOULD be used by tooling specifications and clients to interpret the OpenAPI document. This is *not* related to the API [`info.version`](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#infoVersion) string',
},
},
{
label: 'info',
insertText: 'info',
kind: 14,
format: CompletionFormat.OBJECT,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#infoObject)\n\\\n\\\n**REQUIRED**. Provides metadata about the API. The metadata MAY be used by tooling as required.',
},
},
{
label: 'servers',
insertText: 'servers',
kind: 14,
format: CompletionFormat.ARRAY,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[[Server Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#serverObject)]\n\\\n\\\nAn array of Server Objects, which provide connectivity information to a target server. If the `servers` property is not provided, or is an empty array, the default value would be a [Server Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#serverObject) with a [url](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#serverUrl) value of `/`.',
},
},
{
label: 'paths',
insertText: 'paths',
kind: 14,
format: CompletionFormat.OBJECT,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathsObject)\n\\\n\\\n**REQUIRED**. The available paths and operations for the API.',
},
},
{
label: 'components',
insertText: 'components',
kind: 14,
format: CompletionFormat.OBJECT,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Components Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#componentsObject)\n\\\n\\\nAn element to hold various schemas for the specification.',
},
},
{
label: 'security',
insertText: 'security',
kind: 14,
format: CompletionFormat.ARRAY,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[[Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#securityRequirementObject)]\n\\\n\\\nA declaration of which security mechanisms can be used across the API. The list of values includes alternative security requirement objects that can be used. Only one of the security requirement objects need to be satisfied to authorize a request. Individual operations can override this definition. To make security optional, an empty security requirement (`{}`) can be included in the array.',
},
},
{
label: 'tags',
insertText: 'tags',
kind: 14,
format: CompletionFormat.ARRAY,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
"[[Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#tagObject)]\n\\\n\\\nA list of tags used by the specification with additional metadata. The order of the tags can be used to reflect on their order by the parsing tools. Not all tags that are used by the [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#operationObject) must be declared. The tags that are not declared MAY be organized randomly or based on the tools' logic. Each tag name in the list MUST be unique.",
},
},
{
label: 'externalDocs',
insertText: 'externalDocs',
kind: 14,
format: CompletionFormat.OBJECT,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[External Documentation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#externalDocumentationObject)\n\\\n\\\nAdditional external documentation.',
},
},
{
label: 'webhooks',
insertText: 'webhooks',
kind: 14,
format: CompletionFormat.OBJECT,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'Map[`string`, [Path Item Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathItemObject) | [Reference Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#referenceObject)]\n\\\n\\\nThe incoming webhooks that MAY be received as part of this API and that the API consumer MAY choose to implement. Closely related to the `callbacks` feature, this section describes requests initiated other than by an API call, for example by an out of band registration. The key name is a unique string to refer to each webhook, while the (optionally referenced) Path Item Object describes a request that may be initiated by the API provider and the expected responses. An [example](https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.1/webhook-example.yaml) is available.',
},
},
{
label: 'jsonSchemaDialect',
insertText: 'jsonSchemaDialect',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'The default value for the `$schema` keyword within [Schema Objects](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#schemaObject) contained within this OAS document. This MUST be in the form of a URI.',
},
},
];

export default completion;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
const documentation = [
{
target: 'openapi',
docs: '`**REQUIRED**. This string MUST be the [version number](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#versions) of the OpenAPI Specification that the OpenAPI document uses. The `openapi` field SHOULD be used by tooling to interpret the OpenAPI document. This is *not* related to the API [`info.version`](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#infoVersion) string.',
docs: '**REQUIRED**. This string MUST be the [version number](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#versions) of the OpenAPI Specification that the OpenAPI document uses. The `openapi` field SHOULD be used by tooling to interpret the OpenAPI document. This is *not* related to the API [`info.version`](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#infoVersion) string.',
},
{
target: 'jsonSchemaDialect',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const allowedFieldsLint: LinterMeta = {
code: ApilintCodes.NOT_ALLOWED_FIELDS,
source: 'apilint',
message: 'Object includes not allowed fields',
severity: 1,
linterFunction: 'allowedFields',
linterParams: [
[
'openapi',
'info',
'servers',
'paths',
'components',
'security',
'tags',
'externalDocs',
'jsonSchemaDialect',
'webhooks',
],
'x-',
],
marker: 'key',
};

export default allowedFieldsLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const componentsTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_OPEN_API_FIELD_COMPONENTS_TYPE,
source: 'apilint',
message: 'components must be an object',
severity: 1,
linterFunction: 'apilintElementOrClass',
linterParams: ['components'],
marker: 'value',
target: 'components',
data: {},
};

export default componentsTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const externalDocsTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_OPEN_API_FIELD_EXTERNAL_DOCS_TYPE,
source: 'apilint',
message: 'externalDocs must be an object',
severity: 1,
linterFunction: 'apilintElementOrClass',
linterParams: ['externalDocumentation'],
marker: 'value',
target: 'externalDocs',
data: {},
};

export default externalDocsTypeLint;
35 changes: 35 additions & 0 deletions packages/apidom-ls/src/config/openapi/openapi3_1/lint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import allowedFieldsLint from './allowed-fields';
import requiredFieldsLint from './required-fields';
import componentsTypeLint from './components--type';
import externalDocsTypeLint from './external-docs--type';
import infoRequiredLint from './info--required';
import infoTypeLint from './info--type';
import jsonSchemaDialectFormatURILint from './jsonSchemaDialect--format-uri';
import pathsTypeLint from './paths--type';
import securityItemsTypeLint from './security--items-type';
import securityTypeLint from './security--type';
import serversItemsTypeLint from './servers--items-type';
import serversTypeLint from './servers--type';
import tagsItemsTypeLint from './tags--items-type';
import tagsTypeLint from './tags--type';
import webhooksLint from './webhooks--type';

const lints = [
allowedFieldsLint,
requiredFieldsLint,
componentsTypeLint,
externalDocsTypeLint,
infoRequiredLint,
infoTypeLint,
jsonSchemaDialectFormatURILint,
pathsTypeLint,
securityItemsTypeLint,
securityTypeLint,
serversItemsTypeLint,
serversTypeLint,
tagsItemsTypeLint,
tagsTypeLint,
webhooksLint,
];

export default lints;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const infoRequiredLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_OPEN_API_FIELD_INFO_TYPE_REQUIRED,
source: 'apilint',
message: "should always have a 'info' section",
severity: 1,
linterFunction: 'hasRequiredField',
linterParams: ['info'],
marker: 'key',
data: {
quickFix: [
{
message: "add 'info' section",
action: 'addChild',
snippetYaml: 'info: \n \n',
snippetJson: '"info": {\n \n },\n',
},
],
},
};

export default infoRequiredLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const infoTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_OPEN_API_FIELD_INFO_TYPE,
source: 'apilint',
message: 'info must be an object',
severity: 1,
linterFunction: 'apilintElementOrClass',
linterParams: ['info'],
marker: 'value',
target: 'info',
data: {},
};

export default infoTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const jsonSchemaDialectFormatURILint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_OPEN_API_FIELD_JSON_SCHEMA_FORMAT_URI,
source: 'apilint',
message: "'jsonSchemaDialect' value MUST be in the form of a URI.",
severity: 1,
linterFunction: 'apilintValidURI',
marker: 'value',
target: 'jsonSchemaDialect',
data: {},
};

export default jsonSchemaDialectFormatURILint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const pathsTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_OPEN_API_FIELD_PATHS_TYPE,
source: 'apilint',
message: 'paths must be an object',
severity: 1,
linterFunction: 'apilintElementOrClass',
linterParams: ['paths'],
marker: 'value',
target: 'paths',
data: {},
};

export default pathsTypeLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';

const requiredFieldsLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_1_OPEN_API_REQUIRED_FIELDS,
source: 'apilint',
message: 'OpenAPI Object must contain one the following fields: paths, components, webhooks',
severity: 1,
linterFunction: 'hasRequiredField',
linterParams: ['paths'],
marker: 'key',
conditions: [
{
targets: [{ path: 'root' }],
function: 'missingFields',
params: [['paths', 'components', 'webhooks']],
},
],
data: {
quickFix: [
{
message: "add 'paths' section",
action: 'addChild',
snippetYaml: 'paths: \n \n',
snippetJson: '"paths": {\n \n },\n',
},
],
},
};

export default requiredFieldsLint;
Loading