Skip to content

Commit

Permalink
feat(ns-openapi-3-1): finish OpenAPI Object
Browse files Browse the repository at this point in the history
Closes #386
  • Loading branch information
char0n committed May 21, 2021
1 parent e840834 commit adeacff
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ns-openapi-3-1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ If multiple plugins with the same visitor method are defined, they run in parall

Only fully implemented specification objects should be checked here.

- [ ] [OpenAPI Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#oasObject) (partial)
- [x] [OpenAPI Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#oasObject)
- [x] [Info Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#infoObject)
- [x] [Contact Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#contactObject)
- [x] [License Object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md#licenseObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export const isSecuritySchemeLikeElement = <T extends Element>(element: T): bool
);
};

export const isTagLikeElement = <T extends Element>(element: T) => {
// @ts-ignore
return isObjectElement(element) && element.hasKey('name');
};

export const isOpenApiExtension = (element: MemberElement): boolean => {
// @ts-ignore
return isStringElement(element.key) && startsWith('x-', element.key.toValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ import OAuthFlowAuthorizationUrlVisitor from './visitors/open-api-3-1/oauth-flow
import OAuthFlowTokenUrlVisitor from './visitors/open-api-3-1/oauth-flow/TokenUrlVisitor';
import OAuthFlowRefreshUrlVisitor from './visitors/open-api-3-1/oauth-flow/RefreshUrlVisitor';
import OAuthFlowScopesVisitor from './visitors/open-api-3-1/oauth-flow/ScopesVisitor';
import WebhooksVisitor from './visitors/open-api-3-1/WebhooksVisitor';
import TagsVisitor from './visitors/open-api-3-1/TagsVisitor';

/**
* Specification object allows us to have complete control over visitors
Expand All @@ -211,10 +213,12 @@ const specification = {
paths: {
$ref: '#/visitors/document/objects/Paths',
},
webhooks: WebhooksVisitor,
components: {
$ref: '#/visitors/document/objects/Components',
},
security: SecurityVisitor,
tags: TagsVisitor,
externalDocs: {
$ref: '#/visitors/document/objects/ExternalDocumentation',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import stampit from 'stampit';
import { ArrayElement, Element, BREAK } from 'apidom';

import SpecificationVisitor from '../SpecificationVisitor';
import FallbackVisitor from '../FallbackVisitor';
import { isTagLikeElement } from '../../predicates';

const TagsVisitor = stampit(SpecificationVisitor, FallbackVisitor, {
init() {
this.element = new ArrayElement();
},
methods: {
ArrayElement(arrayElement: ArrayElement) {
arrayElement.forEach((item: Element) => {
const specPath = isTagLikeElement(item) ? ['document', 'objects', 'Tag'] : ['value'];
const element = this.toRefractedElement(specPath);

this.element.push(element);
});

this.copyMetaAndAttributes(arrayElement, this.element);
this.element.classes.push('tags');

return BREAK;
},
},
});

export default TagsVisitor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import stampit from 'stampit';
import { ObjectElement, Element } from 'apidom';

import ReferenceElement from '../../../elements/Reference';
import MapVisitor from '../generics/MapVisitor';
import FallbackVisitor from '../FallbackVisitor';
import { isPathItemLikeElement, isReferenceLikeElement } from '../../predicates';
import { isReferenceElement } from '../../../predicates';

const WebhooksVisitor = stampit(MapVisitor, FallbackVisitor, {
props: {
specPath: (element: Element) => {
// eslint-disable-next-line no-nested-ternary
return isReferenceLikeElement(element)
? ['document', 'objects', 'Reference']
: isPathItemLikeElement(element)
? ['document', 'objects', 'PathItem']
: ['value'];
},
},
init() {
this.element = new ObjectElement();
this.element.classes.push('webhooks');
},
methods: {
ObjectElement(objectElement: ObjectElement) {
// @ts-ignore
const result = MapVisitor.compose.methods.ObjectElement.call(this, objectElement);

this.element.filter(isReferenceElement).forEach((referenceElement: ReferenceElement) => {
referenceElement.setMetaProperty('referenced-element', 'pathItem');
});

return result;
},
},
});

export default WebhooksVisitor;

0 comments on commit adeacff

Please sign in to comment.