Skip to content

OpenAPI 3.0.x language service rules #2139

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

Merged
merged 6 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LinterMeta } from '../../../../apidom-language-types';
const inEqualsApiKeyLint: LinterMeta = {
code: ApilintCodes.ASYNCAPI2_SECURITY_SCHEME_FIELD_IN_EQUALS_API_KEY,
source: 'apilint',
message: 'type must be one of allowed values',
message: 'in must be one of allowed values',
severity: 1,
linterFunction: 'apilintValueOrArray',
linterParams: [['user', 'password']],
Expand Down
20 changes: 20 additions & 0 deletions packages/apidom-ls/src/config/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,26 @@ enum ApilintCodes {
OPENAPI3_0_XML_FIELD_ATTRIBUTE_TYPE = 5230400,
OPENAPI3_0_XML_FIELD_WRAPPED_TYPE = 5230500,

OPENAPI3_SECURITY_SCHEME = 5240000,
OPENAPI3_SECURITY_SCHEME_FIELD_TYPE_EQUALS = 5240100,
OPENAPI3_SECURITY_SCHEME_FIELD_DESCRIPTION_TYPE = 5240200,
OPENAPI3_SECURITY_SCHEME_FIELD_NAME_TYPE = 5240300,
OPENAPI3_SECURITY_SCHEME_FIELD_NAME_REQUIRED,
OPENAPI3_SECURITY_SCHEME_FIELD_IN_EQUALS = 5240400,
OPENAPI3_SECURITY_SCHEME_FIELD_IN_REQUIRED,
OPENAPI3_SECURITY_SCHEME_FIELD_SCHEME_TYPE = 5240500,
OPENAPI3_SECURITY_SCHEME_FIELD_SCHEME_REQUIRED,
OPENAPI3_SECURITY_SCHEME_FIELD_BEARER_FORMAT_TYPE = 5240600,
OPENAPI3_SECURITY_SCHEME_FIELD_FLOWS_TYPE = 5240700,
OPENAPI3_SECURITY_SCHEME_FIELD_FLOWS_REQUIRED,
OPENAPI3_SECURITY_SCHEME_FIELD_OPEN_ID_CONNECT_URL_FORMAT_URI = 5240800,
OPENAPI3_SECURITY_SCHEME_FIELD_OPEN_ID_CONNECT_URL_REQUIRED,

OPENAPI3_DISCRIMINATOR = 5250000,
OPENAPI3_DISCRIMINATOR_FIELD_PROPERTY_NAME_TYPE = 5250100,
OPENAPI3_DISCRIMINATOR_FIELD_PROPERTY_NAME_REQUIRED,
OPENAPI3_DISCRIMINATOR_FIELD_MAPPING_TYPE = 5250200,

OPENAPI3_1 = 7000000,

OPENAPI3_1_OPENAPI_VALUE_PATTERN_3_1_0 = 7000100,
Expand Down
36 changes: 36 additions & 0 deletions packages/apidom-ls/src/config/openapi/discriminator/completion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
ApidomCompletionItem,
CompletionFormat,
CompletionType,
} from '../../../apidom-language-types';

const completion: ApidomCompletionItem[] = [
{
label: 'propertyName',
insertText: 'propertyName',
kind: 14,
format: CompletionFormat.QUOTED,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'**REQUIRED**. The name of the property in the payload that will hold the discriminator value.',
},
},
{
label: 'mapping',
insertText: 'mapping',
kind: 14,
format: CompletionFormat.OBJECT,
type: CompletionType.PROPERTY,
insertTextFormat: 2,
documentation: {
kind: 'markdown',
value:
'Map[string, string]\n\\\n\\\nAn object to hold mappings between payload values and schema names or references.',
},
},
];

export default completion;
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ const documentation = [
target: 'mapping',
docs: 'Map[`string`, `string`]\n\\\n\\\nAn object to hold mappings between payload values and schema names or references.',
},
/**
* The original documentation has been trimmed in this implementation for readability purposes
* A new custom section `Additional documentation topics` has been added,
* which refers the reader to the source documentation for additional explanations
* Also note, we are cherry-picking the documentation section about
* `Composition and Inheritance (Polymorphism)`from outside the Discriminator Object
* source documentation.
*/
{
/**
* The original documentation has been trimmed in this implementation for readability purposes
* A new custom section `Additional documentation topics` has been added,
* which refers the reader to the source documentation for additional explanations
* Also note, we are cherry picking the documentation section about
* `Composition and Inheritance (Polymorphism)`from outside of the Discriminator Object
* source documentation.
*/
docs: '#### [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#discriminator-object)\n\nWhen request bodies or response payloads may be one of a number of different schemas, a `discriminator` object can be used to aid in serialization, deserialization, and validation. The discriminator is a specific object in a schema which is used to inform the consumer of the document of an alternative schema based on the value associated with it.\n\n\\\nWhen using the discriminator, _inline_ schemas will not be considered.\n\n##### Fixed Fields\nField Name | Type | Description\n---|:---:|---\npropertyName | `string` | **REQUIRED**. The name of the property in the payload that will hold the discriminator value.\nmapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or references.\n\n\\\nThis object MAY be extended with [Specification Extensions](#specificationExtensihttps://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specificationExtensions).\n\n\\\nThe discriminator object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`.\n\n##### [Composition and Inheritance (Polymorphism)](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#composition-and-inheritance-polymorphism)\n\nThe OpenAPI Specification allows combining and extending model definitions using the `allOf` property of JSON Schema, in effect offering model composition.\n`allOf` takes an array of object definitions that are validated *independently* but together compose a single object.\n\n\\\nWhile composition offers model extensibility, it does not imply a hierarchy between the models.\nTo support polymorphism, the OpenAPI Specification adds the `discriminator` field.\nWhen used, the `discriminator` will be the name of the property that decides which schema definition validates the structure of the model.\nAs such, the `discriminator` field MUST be a required field.\nThere are two ways to define the value of a discriminator for an inheriting instance.\n- Use the schema name.\n- Override the schema name by overriding the property with a new value. If a new value exists, this takes precedence over the schema name.\nAs such, inline schema definitions, which do not have a given id, *cannot* be used in polymorphism.\n\n#### Additional documentation topics\n\nFurther explanations of usage can be found in the source documentation for the [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#discriminator-object)\n',
docs: '#### [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#discriminatorObject)\n\nWhen request bodies or response payloads may be one of a number of different schemas, a `discriminator` object can be used to aid in serialization, deserialization, and validation. The discriminator is a specific object in a schema which is used to inform the consumer of the specification of an alternative schema based on the value associated with it.\n\nWhen using the discriminator, _inline_ schemas will not be considered.\n\n##### Fixed Fields\nField Name | Type | Description\n---|:---:|---\npropertyName | `string` | **REQUIRED**. The name of the property in the payload that will hold the discriminator value.\nmapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or references.\n\nThe discriminator object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`.\n\n##### [Composition and Inheritance (Polymorphism)](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#composition-and-inheritance-polymorphism)\n\nThe OpenAPI Specification allows combining and extending model definitions using the `allOf` property of JSON Schema, in effect offering model composition.\n`allOf` takes an array of object definitions that are validated *independently* but together compose a single object.\n\n\\\nWhile composition offers model extensibility, it does not imply a hierarchy between the models.\nTo support polymorphism, the OpenAPI Specification adds the `discriminator` field.\nWhen used, the `discriminator` will be the name of the property that decides which schema definition validates the structure of the model.\nAs such, the `discriminator` field MUST be a required field.\nThere are two ways to define the value of a discriminator for an inheriting instance.\n- Use the schema name.\n- Override the schema name by overriding the property with a new value. If a new value exists, this takes precedence over the schema name.\nAs such, inline schema definitions, which do not have a given id, *cannot* be used in polymorphism.\n\n#### Additional documentation topics\n\nFurther explanations of usage can be found in the source documentation for the [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#discriminatorObject)',
targetSpecs: [
{ namespace: 'openapi', version: '3.0.0' },
{ namespace: 'openapi', version: '3.0.1' },
{ namespace: 'openapi', version: '3.0.2' },
{ namespace: 'openapi', version: '3.0.3' },
],
},
{
docs: '#### [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#discriminator-object)\n\nWhen request bodies or response payloads may be one of a number of different schemas, a `discriminator` object can be used to aid in serialization, deserialization, and validation. The discriminator is a specific object in a schema which is used to inform the consumer of the document of an alternative schema based on the value associated with it.\n\n\\\nWhen using the discriminator, _inline_ schemas will not be considered.\n\n##### Fixed Fields\nField Name | Type | Description\n---|:---:|---\npropertyName | `string` | **REQUIRED**. The name of the property in the payload that will hold the discriminator value.\nmapping | Map[`string`, `string`] | An object to hold mappings between payload values and schema names or references.\n\n\\\nThis object MAY be extended with [Specification Extensions](#specificationExtensihttps://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#specificationExtensions).\n\n\\\nThe discriminator object is legal only when using one of the composite keywords `oneOf`, `anyOf`, `allOf`.\n\n##### [Composition and Inheritance (Polymorphism)](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#composition-and-inheritance-polymorphism)\n\nThe OpenAPI Specification allows combining and extending model definitions using the `allOf` property of JSON Schema, in effect offering model composition.\n`allOf` takes an array of object definitions that are validated *independently* but together compose a single object.\n\n\\\nWhile composition offers model extensibility, it does not imply a hierarchy between the models.\nTo support polymorphism, the OpenAPI Specification adds the `discriminator` field.\nWhen used, the `discriminator` will be the name of the property that decides which schema definition validates the structure of the model.\nAs such, the `discriminator` field MUST be a required field.\nThere are two ways to define the value of a discriminator for an inheriting instance.\n- Use the schema name.\n- Override the schema name by overriding the property with a new value. If a new value exists, this takes precedence over the schema name.\nAs such, inline schema definitions, which do not have a given id, *cannot* be used in polymorphism.\n\n#### Additional documentation topics\n\nFurther explanations of usage can be found in the source documentation for the [Discriminator Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#discriminator-object)\n.',
targetSpecs: [{ namespace: 'openapi', version: '3.1.0' }],
},
];

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

// eslint-disable-next-line @typescript-eslint/naming-convention
const allowedFields3_0Lint: LinterMeta = {
code: ApilintCodes.NOT_ALLOWED_FIELDS,
source: 'apilint',
message: 'Object includes not allowed fields',
severity: 1,
linterFunction: 'allowedFields',
linterParams: [['propertyName', 'mapping']],
marker: 'key',
};

export default allowedFields3_0Lint;
13 changes: 13 additions & 0 deletions packages/apidom-ls/src/config/openapi/discriminator/lint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import allowedFields3_0Lint from './allowed-fields-3-0';
import propertyNameTypeLint from './property-name--type';
import propertyNameRequiredLint from './property-name--required';
import mappingTypeLint from './mapping--type';

const lints = [
propertyNameTypeLint,
propertyNameRequiredLint,
mappingTypeLint,
allowedFields3_0Lint,
];

export default lints;
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 mappingTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_DISCRIMINATOR_FIELD_MAPPING_TYPE,
source: 'apilint',
message: "'mapping' must be an object",
severity: 1,
linterFunction: 'apilintElementOrClass',
linterParams: ['discriminator-mapping'],
marker: 'value',
target: 'mapping',
data: {},
};

export default mappingTypeLint;
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 propertyNameRequiredLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_DISCRIMINATOR_FIELD_PROPERTY_NAME_REQUIRED,
source: 'apilint',
message: "should always have a 'propertyName'",
severity: 1,
linterFunction: 'hasRequiredField',
linterParams: ['propertyName'],
marker: 'key',
data: {
quickFix: [
{
message: "add 'propertyName' field",
action: 'addChild',
snippetYaml: 'propertyName: \n ',
snippetJson: '"propertyName": "",\n ',
},
],
},
};

export default propertyNameRequiredLint;
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 propertyNameTypeLint: LinterMeta = {
code: ApilintCodes.OPENAPI3_DISCRIMINATOR_FIELD_PROPERTY_NAME_TYPE,
source: 'apilint',
message: "'propertyName' must be a string",
severity: 1,
linterFunction: 'apilintType',
linterParams: ['string'],
marker: 'value',
target: 'propertyName',
data: {},
};

export default propertyNameTypeLint;
4 changes: 4 additions & 0 deletions packages/apidom-ls/src/config/openapi/discriminator/meta.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import lint from './lint';
import completion from './completion';
import documentation from './documentation';
import { FormatMeta } from '../../../apidom-language-types';

const meta: FormatMeta = {
lint,
completion,
documentation,
};

Expand Down
Loading