Skip to content
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

OpenAPI 3.1.0 lint and completion rules for openapi object #2106

Merged
merged 31 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
7ff19e4
fix(ls): oas31 docs formatting
tim-lai Oct 4, 2022
32d2b20
feat(ls): oas31 completion rules for top level objects
tim-lai Oct 4, 2022
9a54801
fix(ls): update comment for info object docs
tim-lai Oct 4, 2022
0af1d23
feat(ls): oas31 top level lint rules
tim-lai Oct 4, 2022
e4a91d7
feat(ls): oas31 lint add info object required
tim-lai Oct 4, 2022
766f74a
fix(ls): oas31 lint and completion rules for
tim-lai Oct 5, 2022
f758bca
feat(ls): additional oas31 lint rules
tim-lai Oct 5, 2022
c599710
feat(ls): oas31 lint required for paths, components, webhooks
tim-lai Oct 6, 2022
6d2d7c3
fix(ls): oas31 lint rules require oneOf paths, components, webhooks
tim-lai Oct 7, 2022
90d9ed2
Merge branch 'main' into feat/oas31-openapi-lint-completion
tim-lai Oct 10, 2022
55dec1f
fix(ls): oas31 docs formatting
tim-lai Oct 4, 2022
f4344cb
feat(ls): oas31 completion rules for top level objects
tim-lai Oct 4, 2022
32f8f35
fix(ls): update comment for info object docs
tim-lai Oct 4, 2022
9c8d05f
feat(ls): oas31 top level lint rules
tim-lai Oct 4, 2022
e7d02c0
feat(ls): oas31 lint add info object required
tim-lai Oct 4, 2022
f48edad
fix(ls): oas31 lint and completion rules for
tim-lai Oct 5, 2022
a98ca84
feat(ls): additional oas31 lint rules
tim-lai Oct 5, 2022
fe3c144
feat(ls): oas31 lint required for paths, components, webhooks
tim-lai Oct 6, 2022
6f7c148
fix(ls): oas31 lint rules require oneOf paths, components, webhooks
tim-lai Oct 7, 2022
956262f
Merge branch 'feat/oas31-openapi-lint-completion' of github.com:swagg…
tim-lai Oct 10, 2022
b029d73
Merge branch 'main' of github.com:swagger-api/apidom into feat/oas31-…
tim-lai Oct 10, 2022
07ec7b1
fix(ls): oas31 lint feedback changes
tim-lai Oct 11, 2022
255e5b9
test(ls): align tests with updated documentation rules
tim-lai Oct 11, 2022
64b8f3d
feat(ls): OpenAPI 3.1.0 lint rules for components object (#2118)
tim-lai Oct 10, 2022
3c54663
fix(ls): oas31 lint feedback changes
tim-lai Oct 11, 2022
5e4ad6d
test(ls): align tests with updated completion rules
tim-lai Oct 11, 2022
be4458f
Merge branch 'feat/oas31-openapi-lint-completion' of github.com:swagg…
tim-lai Oct 11, 2022
78f41ba
test(ls): align tests with updated oas31 lint rules
tim-lai Oct 12, 2022
eb13b1a
test(ls): apply proper expected values for oas31 assertions
tim-lai Oct 12, 2022
75b2c5c
test(ls): cleanup comments
tim-lai Oct 13, 2022
1e1cf2c
Merge branch 'main' of github.com:swagger-api/apidom into feat/oas31-…
tim-lai Oct 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -768,8 +768,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,
tim-lai marked this conversation as resolved.
Show resolved Hide resolved
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_VALUES_TYPE = 7010800,
OPENAPI3_1_OPEN_API_FIELD_WEBHOOKS_REQUIRED,
OPENAPI3_1_OPEN_API_FIELD_JSON_SCHEMA_DIALECT_FORMAT_URI = 7010900,

OPENAPI3_1_INFO = 7020000,
OPENAPI3_1_INFO_FIELD_SUMMARY_TYPE = 7020100,

OPENAPI3_1_COMPONENTS = 7020000,
OPENAPI3_1_COMPONENTS_FIELD_PATH_ITEMS_VALUES_TYPE = 7020100,
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[] = [
tim-lai marked this conversation as resolved.
Show resolved Hide resolved
{
label: 'openapi',
insertText: 'openapi',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'**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.',
},
},
{
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: '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.',
},
},
{
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: '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: '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.',
},
},
];

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 './json-schema-dialect--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 webhooksValuesTypeLint from './webhooks--values--type';

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

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_DIALECT_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