-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ls): add rules for OpenAPI 2.0 Swagger Object (#3527)
These rules include: - Completion rules - Documentation rules - Linting rules Refs #3104
- Loading branch information
Showing
24 changed files
with
1,448 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 218 additions & 2 deletions
220
packages/apidom-ls/src/config/openapi/swagger/completion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
packages/apidom-ls/src/config/openapi/swagger/documentation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
20 changes: 20 additions & 0 deletions
20
packages/apidom-ls/src/config/openapi/swagger/lint/base-path--pattern.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
20 changes: 20 additions & 0 deletions
20
packages/apidom-ls/src/config/openapi/swagger/lint/consumes--type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.