Skip to content

Commit dfa91c2

Browse files
authored
feat(ls): add rules for OpenAPI 2.0 Swagger Object (#3527)
These rules include: - Completion rules - Documentation rules - Linting rules Refs #3104
1 parent 08a0671 commit dfa91c2

24 files changed

+1448
-18
lines changed

packages/apidom-ls/src/config/codes.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,28 @@ enum ApilintCodes {
627627

628628
OPENAPI2 = 3000000,
629629

630-
OPENAPI2_CONTACT = 3010000,
631-
632-
OPENAPI2_INFO = 3020000,
630+
OPENAPI2_SWAGGER = 3010000,
631+
OPENAPI2_SWAGGER_FIELD_INFO_TYPE = 3010100,
632+
OPENAPI2_SWAGGER_FIELD_INFO_REQUIRED,
633+
OPENAPI2_SWAGGER_FIELD_HOST_PATTERN = 3010200,
634+
OPENAPI2_SWAGGER_FIELD_BASE_PATH_PATTERN = 3010300,
635+
OPENAPI2_SWAGGER_FIELD_SCHEMES_TYPE = 3010400,
636+
OPENAPI2_SWAGGER_FIELD_SCHEMES_EQUALS,
637+
OPENAPI2_SWAGGER_FIELD_CONSUMES_TYPE = 3010500,
638+
OPENAPI2_SWAGGER_FIELD_PRODUCES_TYPE = 3010600,
639+
OPENAPI2_SWAGGER_FIELD_PATHS_TYPE = 3010700,
640+
OPENAPI2_SWAGGER_FIELD_PATHS_REQUIRED,
641+
OPENAPI2_SWAGGER_FIELD_DEFINITIONS_TYPE = 3010800,
642+
OPENAPI2_SWAGGER_FIELD_PARAMETERS_TYPE = 3010900,
643+
OPENAPI2_SWAGGER_FIELD_RESPONSES_TYPE = 3011000,
644+
OPENAPI2_SWAGGER_FIELD_SECURITY_DEFINITIONS_TYPE = 3011100,
645+
OPENAPI2_SWAGGER_FIELD_SECURITY_TYPE = 3011200,
646+
OPENAPI2_SWAGGER_FIELD_TAGS_TYPE = 3011300,
647+
OPENAPI2_SWAGGER_FIELD_EXTERNAL_DOCS_TYPE = 3011400,
648+
649+
OPENAPI2_CONTACT = 3020000,
650+
651+
OPENAPI2_INFO = 3030000,
633652

634653
OPENAPI3_0 = 5000000,
635654

@@ -640,7 +659,7 @@ enum ApilintCodes {
640659

641660
OPENAPI3_0_OPEN_API = 5010000,
642661
OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE = 5010100,
643-
OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE_REQUIRED,
662+
OPENAPI3_0_OPEN_API_FIELD_INFO_REQUIRED,
644663
OPENAPI3_0_OPEN_API_FIELD_SERVERS_TYPE = 5010200,
645664
OPENAPI3_0_OPEN_API_FIELD_SERVERS_ITEMS_TYPE,
646665
OPENAPI3_0_OPEN_API_FIELD_PATHS_TYPE = 5010300,

packages/apidom-ls/src/config/openapi/openapi3_0/lint/info--required.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { LinterMeta } from '../../../../apidom-language-types';
55
import { OpenAPI30 } from '../../target-specs';
66

77
const infoRequiredLint: LinterMeta = {
8-
code: ApilintCodes.OPENAPI3_0_OPEN_API_FIELD_INFO_TYPE_REQUIRED,
8+
code: ApilintCodes.OPENAPI3_0_OPEN_API_FIELD_INFO_REQUIRED,
99
source: 'apilint',
1010
message: "should always have a 'info' section",
1111
severity: DiagnosticSeverity.Error,
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,221 @@
1-
import { ApidomCompletionItem } from '../../../apidom-language-types';
1+
import {
2+
ApidomCompletionItem,
3+
CompletionFormat,
4+
CompletionType,
5+
} from '../../../apidom-language-types';
6+
import { OpenAPI2 } from '../target-specs';
27

3-
const completion: ApidomCompletionItem[] = [];
8+
const completion: ApidomCompletionItem[] = [
9+
{
10+
label: 'swagger',
11+
insertText: 'swagger',
12+
kind: 14,
13+
format: CompletionFormat.QUOTED,
14+
type: CompletionType.PROPERTY,
15+
insertTextFormat: 2,
16+
documentation: {
17+
kind: 'markdown',
18+
value:
19+
'**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"`.',
20+
},
21+
targetSpecs: OpenAPI2,
22+
},
23+
{
24+
label: 'info',
25+
insertText: 'info',
26+
kind: 14,
27+
format: CompletionFormat.QUOTED,
28+
type: CompletionType.PROPERTY,
29+
insertTextFormat: 2,
30+
documentation: {
31+
kind: 'markdown',
32+
value:
33+
'[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.',
34+
},
35+
targetSpecs: OpenAPI2,
36+
},
37+
{
38+
label: 'host',
39+
insertText: 'host',
40+
kind: 14,
41+
format: CompletionFormat.QUOTED,
42+
type: CompletionType.PROPERTY,
43+
insertTextFormat: 2,
44+
documentation: {
45+
kind: 'markdown',
46+
value:
47+
'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).',
48+
},
49+
targetSpecs: OpenAPI2,
50+
},
51+
{
52+
label: 'basePath',
53+
insertText: 'basePath',
54+
kind: 14,
55+
format: CompletionFormat.QUOTED,
56+
type: CompletionType.PROPERTY,
57+
insertTextFormat: 2,
58+
documentation: {
59+
kind: 'markdown',
60+
value:
61+
'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).',
62+
},
63+
targetSpecs: OpenAPI2,
64+
},
65+
{
66+
label: 'schemes',
67+
insertText: 'schemes',
68+
kind: 14,
69+
format: CompletionFormat.QUOTED,
70+
type: CompletionType.PROPERTY,
71+
insertTextFormat: 2,
72+
documentation: {
73+
kind: 'markdown',
74+
value:
75+
'[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.',
76+
},
77+
targetSpecs: OpenAPI2,
78+
},
79+
{
80+
label: 'consumes',
81+
insertText: 'consumes',
82+
kind: 14,
83+
format: CompletionFormat.QUOTED,
84+
type: CompletionType.PROPERTY,
85+
insertTextFormat: 2,
86+
documentation: {
87+
kind: 'markdown',
88+
value:
89+
'[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).',
90+
},
91+
targetSpecs: OpenAPI2,
92+
},
93+
{
94+
label: 'produces',
95+
insertText: 'produces',
96+
kind: 14,
97+
format: CompletionFormat.QUOTED,
98+
type: CompletionType.PROPERTY,
99+
insertTextFormat: 2,
100+
documentation: {
101+
kind: 'markdown',
102+
value:
103+
'[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).',
104+
},
105+
targetSpecs: OpenAPI2,
106+
},
107+
{
108+
label: 'paths',
109+
insertText: 'paths',
110+
kind: 14,
111+
format: CompletionFormat.QUOTED,
112+
type: CompletionType.PROPERTY,
113+
insertTextFormat: 2,
114+
documentation: {
115+
kind: 'markdown',
116+
value:
117+
'[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.',
118+
},
119+
targetSpecs: OpenAPI2,
120+
},
121+
{
122+
label: 'definitions',
123+
insertText: 'definitions',
124+
kind: 14,
125+
format: CompletionFormat.QUOTED,
126+
type: CompletionType.PROPERTY,
127+
insertTextFormat: 2,
128+
documentation: {
129+
kind: 'markdown',
130+
value:
131+
'[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.',
132+
},
133+
targetSpecs: OpenAPI2,
134+
},
135+
{
136+
label: 'parameters',
137+
insertText: 'parameters',
138+
kind: 14,
139+
format: CompletionFormat.QUOTED,
140+
type: CompletionType.PROPERTY,
141+
insertTextFormat: 2,
142+
documentation: {
143+
kind: 'markdown',
144+
value:
145+
'[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.',
146+
},
147+
targetSpecs: OpenAPI2,
148+
},
149+
{
150+
label: 'responses',
151+
insertText: 'responses',
152+
kind: 14,
153+
format: CompletionFormat.QUOTED,
154+
type: CompletionType.PROPERTY,
155+
insertTextFormat: 2,
156+
documentation: {
157+
kind: 'markdown',
158+
value:
159+
'[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.',
160+
},
161+
targetSpecs: OpenAPI2,
162+
},
163+
{
164+
label: 'securityDefinitions',
165+
insertText: 'securityDefinitions',
166+
kind: 14,
167+
format: CompletionFormat.QUOTED,
168+
type: CompletionType.PROPERTY,
169+
insertTextFormat: 2,
170+
documentation: {
171+
kind: 'markdown',
172+
value:
173+
'[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.',
174+
},
175+
targetSpecs: OpenAPI2,
176+
},
177+
{
178+
label: 'security',
179+
insertText: 'security',
180+
kind: 14,
181+
format: CompletionFormat.QUOTED,
182+
type: CompletionType.PROPERTY,
183+
insertTextFormat: 2,
184+
documentation: {
185+
kind: 'markdown',
186+
value:
187+
'[[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.',
188+
},
189+
targetSpecs: OpenAPI2,
190+
},
191+
{
192+
label: 'tags',
193+
insertText: 'tags',
194+
kind: 14,
195+
format: CompletionFormat.QUOTED,
196+
type: CompletionType.PROPERTY,
197+
insertTextFormat: 2,
198+
documentation: {
199+
kind: 'markdown',
200+
value:
201+
"[[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.",
202+
},
203+
targetSpecs: OpenAPI2,
204+
},
205+
{
206+
label: 'externalDocs',
207+
insertText: 'externalDocs',
208+
kind: 14,
209+
format: CompletionFormat.QUOTED,
210+
type: CompletionType.PROPERTY,
211+
insertTextFormat: 2,
212+
documentation: {
213+
kind: 'markdown',
214+
value:
215+
'[External Documentation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#externalDocumentationObject)\n\\\n\\\nAdditional external documentation.',
216+
},
217+
targetSpecs: OpenAPI2,
218+
},
219+
];
4220

5221
export default completion;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,71 @@
11
import { DocumentationMeta } from '../../../apidom-language-types';
2+
import { OpenAPI2 } from '../target-specs';
23

3-
const documentation: DocumentationMeta[] = [];
4+
/**
5+
* Omitted fixed fields:
6+
* - definitions
7+
* - parameters
8+
* - responses
9+
* - securityDefinitions
10+
* - externalDocs
11+
*
12+
* Field omission reason: omitted fields do have a non-union type. Thus,
13+
* documentation for these fields doesn't need to be specified here and will
14+
* come directly from the type itself. Description of these fields doesn't
15+
* contain significant information.
16+
*/
17+
18+
const documentation: DocumentationMeta[] = [
19+
{
20+
target: 'swagger',
21+
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"`.',
22+
targetSpecs: OpenAPI2,
23+
},
24+
{
25+
target: 'info',
26+
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.',
27+
targetSpecs: OpenAPI2,
28+
},
29+
{
30+
target: 'host',
31+
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).',
32+
targetSpecs: OpenAPI2,
33+
},
34+
{
35+
target: 'basePath',
36+
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).',
37+
targetSpecs: OpenAPI2,
38+
},
39+
{
40+
target: 'schemes',
41+
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.',
42+
targetSpecs: OpenAPI2,
43+
},
44+
{
45+
target: 'consumes',
46+
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).',
47+
targetSpecs: OpenAPI2,
48+
},
49+
{
50+
target: 'produces',
51+
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).',
52+
targetSpecs: OpenAPI2,
53+
},
54+
{
55+
target: 'paths',
56+
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.',
57+
targetSpecs: OpenAPI2,
58+
},
59+
{
60+
target: 'security',
61+
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.',
62+
targetSpecs: OpenAPI2,
63+
},
64+
{
65+
target: 'tags',
66+
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.",
67+
targetSpecs: OpenAPI2,
68+
},
69+
];
470

571
export default documentation;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { DiagnosticSeverity } from 'vscode-languageserver-types';
2+
3+
import ApilintCodes from '../../../codes';
4+
import { LinterMeta } from '../../../../apidom-language-types';
5+
import { OpenAPI2 } from '../../target-specs';
6+
7+
const basePathPatternLint: LinterMeta = {
8+
code: ApilintCodes.OPENAPI2_SWAGGER_FIELD_BASE_PATH_PATTERN,
9+
source: 'apilint',
10+
message: '"basePath" value MUST be a relative URI Referencing starting with a leading slash (/).',
11+
severity: DiagnosticSeverity.Error,
12+
linterFunction: 'apilintValueRegex',
13+
linterParams: ['^/(?:[^/s][^s]*)?$'],
14+
target: 'basePath',
15+
marker: 'value',
16+
data: {},
17+
targetSpecs: OpenAPI2,
18+
};
19+
20+
export default basePathPatternLint;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { DiagnosticSeverity } from 'vscode-languageserver-types';
2+
3+
import ApilintCodes from '../../../codes';
4+
import { LinterMeta } from '../../../../apidom-language-types';
5+
import { OpenAPI2 } from '../../target-specs';
6+
7+
const consumesTypeLint: LinterMeta = {
8+
code: ApilintCodes.OPENAPI2_SWAGGER_FIELD_CONSUMES_TYPE,
9+
source: 'apilint',
10+
message: "'consumes' should be an array of strings",
11+
severity: DiagnosticSeverity.Error,
12+
linterFunction: 'apilintArrayOfType',
13+
linterParams: ['string'],
14+
marker: 'key',
15+
target: 'consumes',
16+
data: {},
17+
targetSpecs: OpenAPI2,
18+
};
19+
20+
export default consumesTypeLint;

0 commit comments

Comments
 (0)