Skip to content

Commit

Permalink
feat(OpenApi3.1-Yaml): add support for OpenApi.security field
Browse files Browse the repository at this point in the history
Refs #1
  • Loading branch information
char0n committed Oct 13, 2020
1 parent 51de6e4 commit 7065807
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const specification = {
components: {
$ref: '#/visitors/document/objects/Components',
},
security: SecurityVisitor,
paths: {
$ref: '#/visitors/document/objects/Paths',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
}
}
},
"security": [
{},
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
],
"servers": [
{
"url": "http://api.example.com/v1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,6 +61,7 @@ const specification = {
$ref: '#/visitors/document/objects/Info',
},
servers: ServersVisitor,
security: SecurityVisitor,
},
},
Info: {
Expand Down Expand Up @@ -109,6 +112,9 @@ const specification = {
description: ServerVariableDescriptionVisitor,
},
},
SecurityRequirement: {
$visitor: SecurityRequirementVisitor,
},
},
extension: SpecificationExtensionVisitor,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7065807

Please sign in to comment.