diff --git a/apidom/packages/apidom-parser-adapter-openapi3-1-json/src/parser/specification.ts b/apidom/packages/apidom-parser-adapter-openapi3-1-json/src/parser/specification.ts index 941fc80374..8f100fcb59 100644 --- a/apidom/packages/apidom-parser-adapter-openapi3-1-json/src/parser/specification.ts +++ b/apidom/packages/apidom-parser-adapter-openapi3-1-json/src/parser/specification.ts @@ -85,6 +85,7 @@ const specification = { components: { $ref: '#/visitors/document/objects/Components', }, + security: SecurityVisitor, paths: { $ref: '#/visitors/document/objects/Paths', }, diff --git a/apidom/packages/apidom-parser-adapter-openapi3-1-json/test/fixtures/sample-api.json b/apidom/packages/apidom-parser-adapter-openapi3-1-json/test/fixtures/sample-api.json index 7f8f4ae901..1d60a719df 100644 --- a/apidom/packages/apidom-parser-adapter-openapi3-1-json/test/fixtures/sample-api.json +++ b/apidom/packages/apidom-parser-adapter-openapi3-1-json/test/fixtures/sample-api.json @@ -58,6 +58,15 @@ } } }, + "security": [ + {}, + { + "petstore_auth": [ + "write:pets", + "read:pets" + ] + } + ], "servers": [ { "url": "http://api.example.com/v1", diff --git a/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/specification.ts b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/specification.ts index 92887fedd2..6f15344bc2 100644 --- a/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/specification.ts +++ b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/specification.ts @@ -29,6 +29,8 @@ import ServerVariableVisitor from './visitors/open-api-3-1/server-variable'; import ServerVariableEnumVisitor from './visitors/open-api-3-1/server-variable/EnumVisitor'; import ServerVariableDefaultVisitor from './visitors/open-api-3-1/server-variable/DefaultVisitor'; import ServerVariableDescriptionVisitor from './visitors/open-api-3-1/server-variable/DescriptionVisitor'; +import SecurityVisitor from './visitors/open-api-3-1/SecurityVisitor'; +import SecurityRequirementVisitor from './visitors/open-api-3-1/security-requirement'; /** * Specification object allows us to have complete control over visitors @@ -59,6 +61,7 @@ const specification = { $ref: '#/visitors/document/objects/Info', }, servers: ServersVisitor, + security: SecurityVisitor, }, }, Info: { @@ -109,6 +112,9 @@ const specification = { description: ServerVariableDescriptionVisitor, }, }, + SecurityRequirement: { + $visitor: SecurityRequirementVisitor, + }, }, extension: SpecificationExtensionVisitor, }, diff --git a/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/generics/PatternedFieldsYamlMappingVisitor.ts b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/generics/PatternedFieldsYamlMappingVisitor.ts index b0d07b4f30..d51c7ae203 100644 --- a/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/generics/PatternedFieldsYamlMappingVisitor.ts +++ b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/generics/PatternedFieldsYamlMappingVisitor.ts @@ -49,7 +49,7 @@ const PatternedFieldsYamlMappingVisitor = stampit(SpecificationVisitor, { const visitor = this.retrieveVisitorInstance(specPath); const keyElement = new this.namespace.elements.String(keyName); - visit(keyValuePairNode, visitor); + visit(valueNode, visitor); const memberElement = this.maybeAddSourceMap( keyValuePairNode, diff --git a/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/SecurityVisitor.ts b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/SecurityVisitor.ts new file mode 100644 index 0000000000..c7a5d3c597 --- /dev/null +++ b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/SecurityVisitor.ts @@ -0,0 +1,32 @@ +import stampit from 'stampit'; +import { isYamlMapping, YamlSequence } from 'apidom-ast'; + +import { BREAK } from '..'; +import SpecificationVisitor from '../SpecificationVisitor'; +import { KindVisitor } from '../generics'; + +const SecurityVisitor = stampit(KindVisitor, SpecificationVisitor, { + init() { + this.element = new this.namespace.elements.Array(); + this.element.classes.push('security'); + }, + methods: { + sequence(sequenceNode: YamlSequence) { + sequenceNode.content.forEach((item) => { + if (isYamlMapping(item)) { + const element = this.nodeToElement(['document', 'objects', 'SecurityRequirement'], item); + this.element.push(element); + } else { + const element = this.nodeToElement(['kind'], item); + this.element.push(element); + } + }); + + this.maybeAddSourceMap(sequenceNode, this.element); + + return BREAK; + }, + }, +}); + +export default SecurityVisitor; diff --git a/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/security-requirement/index.ts b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/security-requirement/index.ts new file mode 100644 index 0000000000..9bf4d42f79 --- /dev/null +++ b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/src/parser/visitors/open-api-3-1/security-requirement/index.ts @@ -0,0 +1,16 @@ +import stampit from 'stampit'; +import { always } from 'ramda'; + +import MapYamlMappingVisitor from '../../generics/MapYamlMappingVisitor'; +import { KindVisitor } from '../../generics'; + +const SecurityRequirementVisitor = stampit(KindVisitor, MapYamlMappingVisitor, { + props: { + specPath: always(['kind']), + }, + init() { + this.element = new this.namespace.elements.SecurityRequirement(); + }, +}); + +export default SecurityRequirementVisitor; diff --git a/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/test/fixtures/sample-api.yaml b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/test/fixtures/sample-api.yaml index d624401186..52369cd39f 100644 --- a/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/test/fixtures/sample-api.yaml +++ b/apidom/packages/apidom-parser-adapter-openapi3-1-yaml/test/fixtures/sample-api.yaml @@ -29,9 +29,14 @@ servers: variables: port: enum: - - '8443' - - '443' - default: '8443' + - 8443 + - 443 + default: 8443 description: Port description +security: + - {} + - petstore_auth: + - write:pets + - read:pets ... prop: value