Skip to content

Commit

Permalink
feat(ns-openapi-3-1): add reference metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Aug 2, 2021
1 parent e66cdaa commit 5ab586e
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import stampit from 'stampit';
import { always } from 'ramda';
import { ObjectElement, isStringElement } from 'apidom';

import ExampleElement from '../../../../elements/Example';
import FallbackVisitor from '../../FallbackVisitor';
Expand All @@ -13,6 +14,19 @@ const ExampleVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, {
init() {
this.element = new ExampleElement();
},
methods: {
ObjectElement(objectElement: ObjectElement) {
// @ts-ignore
const result = FixedFieldsVisitor.compose.methods.ObjectElement.call(this, objectElement);

// mark this ExampleElement with reference metadata
if (isStringElement(this.element.externalValue)) {
this.element.classes.push('reference-element');
}

return result;
},
},
});

export default ExampleVisitor;
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import stampit from 'stampit';
import { StringElement, BREAK } from 'apidom';

import FallbackVisitor from '../../FallbackVisitor';

const OperationIdVisitor = FallbackVisitor;
const OperationIdVisitor = stampit(FallbackVisitor, {
methods: {
StringElement(stringElement: StringElement) {
this.element = stringElement.clone();
this.element.classes.push('reference-value');

return BREAK;
},
},
});

export default OperationIdVisitor;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import stampit from 'stampit';
import { always } from 'ramda';
import { isStringElement, ObjectElement } from 'apidom';

import LinkElement from '../../../../elements/Link';
import FallbackVisitor from '../../FallbackVisitor';
Expand All @@ -13,6 +14,19 @@ const LinkVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, {
init() {
this.element = new LinkElement();
},
methods: {
ObjectElement(objectElement: ObjectElement) {
// @ts-ignore
const result = FixedFieldsVisitor.compose.methods.ObjectElement.call(this, objectElement);

// mark this LinkElement with reference metadata
if (isStringElement(this.element.operationId) || isStringElement(this.element.operationRef)) {
this.element.classes.push('reference-element');
}

return result;
},
},
});

export default LinkVisitor;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stampit from 'stampit';
import { always } from 'ramda';
import { StringElement, ObjectElement } from 'apidom';
import { StringElement, ObjectElement, isStringElement } from 'apidom';

import PathItemElement from '../../../../elements/PathItem';
import OperationElement from '../../../../elements/Operation';
Expand Down Expand Up @@ -29,6 +29,11 @@ const PathItemVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, {
operationElement.setMetaProperty('httpMethod', httpMethodElementCS);
});

// mark this PathItemElement with reference metadata
if (isStringElement(this.element.$ref)) {
this.element.classes.push('reference-element');
}

return result;
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import stampit from 'stampit';
import { always } from 'ramda';
import { ObjectElement, isStringElement } from 'apidom';

import ReferenceElement from '../../../../elements/Reference';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
Expand All @@ -13,6 +14,19 @@ const ReferenceVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, {
init() {
this.element = new ReferenceElement();
},
methods: {
ObjectElement(objectElement: ObjectElement) {
// @ts-ignore
const result = FixedFieldsVisitor.compose.methods.ObjectElement.call(this, objectElement);

// mark this ReferenceElement with reference metadata
if (isStringElement(this.element.$ref)) {
this.element.classes.push('reference-element');
}

return result;
},
},
});

export default ReferenceVisitor;
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,25 @@ const SchemaVisitor = stampit(FixedFieldsVisitor, ParentSchemaAwareVisitor, Fall
this.element.setMetaProperty('inherited$id', inherited$id);
};

const appendReferenceMetadata = (objectElement: ObjectElement) => {
const $ref = objectElement.get('$ref')?.toValue();

if (isNonEmptyString($ref)) {
this.element.classes.push('reference-element');
}
};

/**
* Public Api.
*/
this.ObjectElement = function _ObjectElement(objectElement: ObjectElement) {
this.element = new SchemaElement();
handle$schema(objectElement);
handle$id(objectElement);
appendReferenceMetadata(objectElement);

// for further processing consider this Schema Element as parent for all embedded Schema Elements
this.parent = this.element;
// @ts-ignore
return FixedFieldsVisitor.compose.methods.ObjectElement.call(this, objectElement);
const result = FixedFieldsVisitor.compose.methods.ObjectElement.call(this, objectElement);

// mark this SchemaElement with reference metadata
if (isStringElement(this.element.$ref)) {
this.element.classes.push('reference-element');
}

return result;
};

this.BooleanElement = function _BooleanElement(booleanElement: BooleanElement) {
Expand Down
Loading

0 comments on commit 5ab586e

Please sign in to comment.