Skip to content

Commit

Permalink
feat(ls): add rules for OpenAPI 2.0 Swagger Object (#3527)
Browse files Browse the repository at this point in the history
These rules include:
- Completion rules
- Documentation rules
- Linting rules

Refs #3104
  • Loading branch information
char0n authored Dec 12, 2023
1 parent 08a0671 commit dfa91c2
Show file tree
Hide file tree
Showing 24 changed files with 1,448 additions and 18 deletions.
27 changes: 23 additions & 4 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,28 @@ enum ApilintCodes {

OPENAPI2 = 3000000,

OPENAPI2_CONTACT = 3010000,

OPENAPI2_INFO = 3020000,
OPENAPI2_SWAGGER = 3010000,
OPENAPI2_SWAGGER_FIELD_INFO_TYPE = 3010100,
OPENAPI2_SWAGGER_FIELD_INFO_REQUIRED,
OPENAPI2_SWAGGER_FIELD_HOST_PATTERN = 3010200,
OPENAPI2_SWAGGER_FIELD_BASE_PATH_PATTERN = 3010300,
OPENAPI2_SWAGGER_FIELD_SCHEMES_TYPE = 3010400,
OPENAPI2_SWAGGER_FIELD_SCHEMES_EQUALS,
OPENAPI2_SWAGGER_FIELD_CONSUMES_TYPE = 3010500,
OPENAPI2_SWAGGER_FIELD_PRODUCES_TYPE = 3010600,
OPENAPI2_SWAGGER_FIELD_PATHS_TYPE = 3010700,
OPENAPI2_SWAGGER_FIELD_PATHS_REQUIRED,
OPENAPI2_SWAGGER_FIELD_DEFINITIONS_TYPE = 3010800,
OPENAPI2_SWAGGER_FIELD_PARAMETERS_TYPE = 3010900,
OPENAPI2_SWAGGER_FIELD_RESPONSES_TYPE = 3011000,
OPENAPI2_SWAGGER_FIELD_SECURITY_DEFINITIONS_TYPE = 3011100,
OPENAPI2_SWAGGER_FIELD_SECURITY_TYPE = 3011200,
OPENAPI2_SWAGGER_FIELD_TAGS_TYPE = 3011300,
OPENAPI2_SWAGGER_FIELD_EXTERNAL_DOCS_TYPE = 3011400,

OPENAPI2_CONTACT = 3020000,

OPENAPI2_INFO = 3030000,

OPENAPI3_0 = 5000000,

Expand All @@ -640,7 +659,7 @@ enum ApilintCodes {

OPENAPI3_0_OPEN_API = 5010000,
OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE = 5010100,
OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE_REQUIRED,
OPENAPI3_0_OPEN_API_FIELD_INFO_REQUIRED,
OPENAPI3_0_OPEN_API_FIELD_SERVERS_TYPE = 5010200,
OPENAPI3_0_OPEN_API_FIELD_SERVERS_ITEMS_TYPE,
OPENAPI3_0_OPEN_API_FIELD_PATHS_TYPE = 5010300,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI30 } from '../../target-specs';

const infoRequiredLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE_REQUIRED,
code: ApilintCodes.OPENAPI3_0_OPEN_API_FIELD_INFO_REQUIRED,
source: 'apilint',
message: "should always have a 'info' section",
severity: DiagnosticSeverity.Error,
Expand Down
220 changes: 218 additions & 2 deletions packages/apidom-ls/src/config/openapi/swagger/completion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,221 @@
import { ApidomCompletionItem } from '../../../apidom-language-types';
import {
ApidomCompletionItem,
CompletionFormat,
CompletionType,
} from '../../../apidom-language-types';
import { OpenAPI2 } from '../target-specs';

const completion: ApidomCompletionItem[] = [];
const completion: ApidomCompletionItem[] = [
{
label: 'swagger',
insertText: 'swagger',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'**Required.** Specifies the Swagger Specification version being used. It can be used by the Swagger UI and other clients to interpret the API listing. The value MUST be `"2.0"`.',
},
targetSpecs: OpenAPI2,
},
{
label: 'info',
insertText: 'info',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#infoObject)\n\\\n\\\n**Required.** Provides metadata about the API. The metadata can be used by the clients if needed.',
},
targetSpecs: OpenAPI2,
},
{
label: 'host',
insertText: 'host',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'The host (name or ip) serving the API. This MUST be the host only and does not include the scheme nor sub-paths. It MAY include a port. If the `host` is not included, the host serving the documentation is to be used (including the port). The `host` does not support [path templating](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#pathTemplating).',
},
targetSpecs: OpenAPI2,
},
{
label: 'basePath',
insertText: 'basePath',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'The base path on which the API is served, which is relative to the [`host`](#swaggerHost). If it is not included, the API is served directly under the `host`. The value MUST start with a leading slash (`/`). The `basePath` does not support [path templating](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#pathTemplating).',
},
targetSpecs: OpenAPI2,
},
{
label: 'schemes',
insertText: 'schemes',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[string]\n\\\n\\\nThe transfer protocol of the API. Values MUST be from the list: `"http"`, `"https"`, `"ws"`, `"wss"`. If the `schemes` is not included, the default scheme to be used is the one used to access the Swagger definition itself.',
},
targetSpecs: OpenAPI2,
},
{
label: 'consumes',
insertText: 'consumes',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[string]\n\\\n\\\nA list of MIME types the APIs can consume. This is global to all APIs but can be overridden on specific API calls. Value MUST be as described under [Mime Types](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#mimeTypes).',
},
targetSpecs: OpenAPI2,
},
{
label: 'produces',
insertText: 'produces',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[string]\n\\\n\\\nA list of MIME types the APIs can produce. This is global to all APIs but can be overridden on specific API calls. Value MUST be as described under [Mime Types](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#mimeTypes).',
},
targetSpecs: OpenAPI2,
},
{
label: 'paths',
insertText: 'paths',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#pathsObject)\n\\\n\\\n**Required.** The available paths and operations for the API.',
},
targetSpecs: OpenAPI2,
},
{
label: 'definitions',
insertText: 'definitions',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#definitionsObject)\n\\\n\\\nAn object to hold data types produced and consumed by operations.',
},
targetSpecs: OpenAPI2,
},
{
label: 'parameters',
insertText: 'parameters',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Parameters Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#parametersDefinitionsObject)\n\\\n\\\nAn object to hold parameters that can be used across operations. This property does not define global parameters for all operations.',
},
targetSpecs: OpenAPI2,
},
{
label: 'responses',
insertText: 'responses',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Responses Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#responsesDefinitionsObject)\n\\\n\\\nAn object to hold responses that can be used across operations. This property does not define global responses for all operations.',
},
targetSpecs: OpenAPI2,
},
{
label: 'securityDefinitions',
insertText: 'securityDefinitions',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[Security Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#securityDefinitionsObject)\n\\\n\\\nSecurity scheme definitions that can be used across the specification.',
},
targetSpecs: OpenAPI2,
},
{
label: 'security',
insertText: 'security',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[[Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#securityRequirementObject)]\n\\\n\\\nA declaration of which security schemes are applied for the API as a whole. The list of values describes alternative security schemes that can be used (that is, there is a logical OR between the security requirements). Individual operations can override this definition.',
},
targetSpecs: OpenAPI2,
},
{
label: 'tags',
insertText: 'tags',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
"[[Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.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/2.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.",
},
targetSpecs: OpenAPI2,
},
{
label: 'externalDocs',
insertText: 'externalDocs',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'[External Documentation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#externalDocumentationObject)\n\\\n\\\nAdditional external documentation.',
},
targetSpecs: OpenAPI2,
},
];

export default completion;
68 changes: 67 additions & 1 deletion packages/apidom-ls/src/config/openapi/swagger/documentation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
import { DocumentationMeta } from '../../../apidom-language-types';
import { OpenAPI2 } from '../target-specs';

const documentation: DocumentationMeta[] = [];
/**
* Omitted fixed fields:
* - definitions
* - parameters
* - responses
* - securityDefinitions
* - externalDocs
*
* Field omission reason: omitted fields do have a non-union type. Thus,
* documentation for these fields doesn't need to be specified here and will
* come directly from the type itself. Description of these fields doesn't
* contain significant information.
*/

const documentation: DocumentationMeta[] = [
{
target: 'swagger',
docs: '**Required.** Specifies the Swagger Specification version being used. It can be used by the Swagger UI and other clients to interpret the API listing. The value MUST be `"2.0"`.',
targetSpecs: OpenAPI2,
},
{
target: 'info',
docs: '[Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#infoObject)\n\\\n\\\n**Required.** Provides metadata about the API. The metadata can be used by the clients if needed.',
targetSpecs: OpenAPI2,
},
{
target: 'host',
docs: 'The host (name or ip) serving the API. This MUST be the host only and does not include the scheme nor sub-paths. It MAY include a port. If the `host` is not included, the host serving the documentation is to be used (including the port). The `host` does not support [path templating](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#pathTemplating).',
targetSpecs: OpenAPI2,
},
{
target: 'basePath',
docs: 'The base path on which the API is served, which is relative to the [`host`](#swaggerHost). If it is not included, the API is served directly under the `host`. The value MUST start with a leading slash (`/`). The `basePath` does not support [path templating](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#pathTemplating).',
targetSpecs: OpenAPI2,
},
{
target: 'schemes',
docs: '[string]\n\\\n\\\nThe transfer protocol of the API. Values MUST be from the list: `"http"`, `"https"`, `"ws"`, `"wss"`. If the `schemes` is not included, the default scheme to be used is the one used to access the Swagger definition itself.',
targetSpecs: OpenAPI2,
},
{
target: 'consumes',
docs: '[string]\n\\\n\\\nA list of MIME types the APIs can consume. This is global to all APIs but can be overridden on specific API calls. Value MUST be as described under [Mime Types](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#mimeTypes).',
targetSpecs: OpenAPI2,
},
{
target: 'produces',
docs: '[string]\n\\\n\\\nA list of MIME types the APIs can produce. This is global to all APIs but can be overridden on specific API calls. Value MUST be as described under [Mime Types](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#mimeTypes).',
targetSpecs: OpenAPI2,
},
{
target: 'paths',
docs: '[Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#pathsObject)\n\\\n\\\n**Required.** The available paths and operations for the API.',
targetSpecs: OpenAPI2,
},
{
target: 'security',
docs: '[[Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#securityRequirementObject)]\n\\\n\\\nA declaration of which security schemes are applied for the API as a whole. The list of values describes alternative security schemes that can be used (that is, there is a logical OR between the security requirements). Individual operations can override this definition.',
targetSpecs: OpenAPI2,
},
{
target: 'tags',
docs: "[[Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.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/2.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.",
targetSpecs: OpenAPI2,
},
];

export default documentation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

const basePathPatternLint: LinterMeta = {
code: ApilintCodes.OPENAPI2_SWAGGER_FIELD_BASE_PATH_PATTERN,
source: 'apilint',
message: '"basePath" value MUST be a relative URI Referencing starting with a leading slash (/).',
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintValueRegex',
linterParams: ['^/(?:[^/s][^s]*)?$'],
target: 'basePath',
marker: 'value',
data: {},
targetSpecs: OpenAPI2,
};

export default basePathPatternLint;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DiagnosticSeverity } from 'vscode-languageserver-types';

import ApilintCodes from '../../../codes';
import { LinterMeta } from '../../../../apidom-language-types';
import { OpenAPI2 } from '../../target-specs';

const consumesTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI2_SWAGGER_FIELD_CONSUMES_TYPE,
source: 'apilint',
message: "'consumes' should be an array of strings",
severity: DiagnosticSeverity.Error,
linterFunction: 'apilintArrayOfType',
linterParams: ['string'],
marker: 'key',
target: 'consumes',
data: {},
targetSpecs: OpenAPI2,
};

export default consumesTypeLint;
Loading

0 comments on commit dfa91c2

Please sign in to comment.