Skip to content

Commit

Permalink
feat(ns-workflows-1): add support for Components Object (#3456)
Browse files Browse the repository at this point in the history
Refs #3392
  • Loading branch information
frankkilcommins committed Nov 24, 2023
1 parent bac4850 commit 14dd260
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/apidom-ns-workflows-1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Only fully implemented specification objects should be checked here.
- [x] [Parameter Object](https://github.com/OAI/sig-workflows/blob/main/versions/1.0.0.md#parameter-object)
- [x] [Success Action Object](https://github.com/OAI/sig-workflows/blob/main/versions/1.0.0.md#success-action-object)
- [x] [Failure Action Object](https://github.com/OAI/sig-workflows/blob/main/versions/1.0.0.md#failure-action-object)
- [ ] [Component Object](https://github.com/OAI/sig-workflows/blob/main/versions/1.0.0.md#component-object)
- [x] [Component Object](https://github.com/OAI/sig-workflows/blob/main/versions/1.0.0.md#component-object)
- [x] [Criterion Object](https://github.com/OAI/sig-workflows/blob/main/versions/1.0.0.md#criterion-object)
- [x] [Reference Object](https://github.com/OAI/sig-workflows/blob/main/versions/1.0.0.md#reference-object)
- [x] [JSON Schema](https://json-schema.org/specification-links#2020-12)
Expand Down
26 changes: 26 additions & 0 deletions packages/apidom-ns-workflows-1/src/elements/Components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core';

class Components extends ObjectElement {
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'components';
}

get inputs(): ObjectElement | undefined {
return this.get('inputs');
}

set inputs(inputs: ObjectElement | undefined) {
this.set('inputs', inputs);
}

get parameters(): ObjectElement | undefined {
return this.get('parameters');
}

set parameters(parameters: ObjectElement | undefined) {
this.set('parameters', parameters);
}
}

export default Components;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core';

class ComponentsInputs extends ObjectElement {
static primaryClass = 'components-inputs';

constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.classes.push(ComponentsInputs.primaryClass);
}
}

export default ComponentsInputs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core';

class ComponentsParameters extends ObjectElement {
static primaryClass = 'components-parameters';

constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.classes.push(ComponentsParameters.primaryClass);
}
}

export default ComponentsParameters;
4 changes: 4 additions & 0 deletions packages/apidom-ns-workflows-1/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
isSuccessActionCriteriaElement,
isFailureActionElement,
isFailureActionCriteriaElement,
isComponentsElement,
isCriterionElement,
isReferenceElement,
isJSONSchemaElement,
Expand All @@ -57,6 +58,7 @@ export {
ParameterElement,
SuccessActionElement,
FailureActionElement,
ComponentsElement,
CriterionElement,
ReferenceElement,
JSONSchemaElement,
Expand All @@ -65,3 +67,5 @@ export {
export { default as SourceDescriptionsElement } from './elements/nces/SourceDescriptions';
export { default as SuccessActionCriteriaElement } from './elements/nces/SuccessActionCriteria';
export { default as FailureActionCriteriaElement } from './elements/nces/FailureActionCriteria';
export { default as ComponentsSchemas } from './elements/nces/ComponentsInputs';
export { default as ComponentsParameters } from './elements/nces/ComponentsParameters';
2 changes: 2 additions & 0 deletions packages/apidom-ns-workflows-1/src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SourceDescriptionElement from './elements/SourceDescription';
import ParameterElement from './elements/Parameter';
import SuccessActionElement from './elements/SuccessAction';
import FailureActionElement from './elements/FailureAction';
import ComponentsElement from './elements/Components';
import CriterionElement from './elements/Criterion';
import ReferenceElement from './elements/Reference';
import JSONSchemaElement from './elements/JSONSchema';
Expand All @@ -22,6 +23,7 @@ const workflows1 = {
base.register('parameter', ParameterElement);
base.register('successAction', SuccessActionElement);
base.register('failureAction', FailureActionElement);
base.register('components', ComponentsElement);
base.register('criterion', CriterionElement);
base.register('reference', ReferenceElement);
base.register('jSONSchemaDraft202012', JSONSchemaElement);
Expand Down
11 changes: 11 additions & 0 deletions packages/apidom-ns-workflows-1/src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SourceDescriptionElement from './elements/SourceDescription';
import ParameterElement from './elements/Parameter';
import SuccessActionElement from './elements/SuccessAction';
import FailureActionElement from './elements/FailureAction';
import ComponentsElement from './elements/Components';
import CriterionElement from './elements/Criterion';
import ReferenceElement from './elements/Reference';
import JSONSchemaElement from './elements/JSONSchema';
Expand Down Expand Up @@ -89,6 +90,16 @@ export const isSuccessActionElement = createPredicate(
},
);

export const isComponentsElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is ComponentsElement =>
element instanceof ComponentsElement ||
(hasBasicElementProps(element) &&
isElementType('components', element) &&
primitiveEq('object', element));
},
);

export const isCriterionElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is CriterionElement =>
Expand Down
9 changes: 9 additions & 0 deletions packages/apidom-ns-workflows-1/src/refractor/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SourceDescriptionElement from '../elements/SourceDescription';
import ParameterElement from '../elements/Parameter';
import SuccessActionElement from '../elements/SuccessAction';
import FailureActionElement from '../elements/FailureAction';
import ComponentsElement from '../elements/Components';
import CriterionElement from '../elements/Criterion';
import ReferenceElement from '../elements/Reference';
import JSONSchemaElement from '../elements/JSONSchema';
Expand Down Expand Up @@ -54,6 +55,13 @@ FailureActionElement.refract = createRefractor([
'FailureAction',
'$visitor',
]);
ComponentsElement.refract = createRefractor([
'visitors',
'document',
'objects',
'Components',
'$visitor',
]);
CriterionElement.refract = createRefractor([
'visitors',
'document',
Expand Down Expand Up @@ -84,6 +92,7 @@ export {
ParameterElement,
SuccessActionElement,
FailureActionElement,
ComponentsElement,
CriterionElement,
ReferenceElement,
JSONSchemaElement,
Expand Down
10 changes: 10 additions & 0 deletions packages/apidom-ns-workflows-1/src/refractor/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import SuccessActionVisitor from './visitors/workflows-1/success-action';
import SuccessActionCriteriaVisitor from './visitors/workflows-1/SuccessActionCriteriaVisitor';
import FailureActionVisitor from './visitors/workflows-1/failure-action';
import FailureActionCriteriaVisitor from './visitors/workflows-1/FailureActionCriteriaVisitor';
import ComponentsVisitor from './visitors/workflows-1/components';
import ComponentsInputsVisitor from './visitors/workflows-1/components/InputsVisitor';
import ComponentsParametersVisitor from './visitors/workflows-1/components/ParametersVisitor';
import CriterionVisitor from './visitors/workflows-1/criterion';
import ReferenceVisitor from './visitors/workflows-1/reference';
import Reference$RefVisitor from './visitors/workflows-1/reference/$RefVisitor';
Expand Down Expand Up @@ -97,6 +100,13 @@ const specification = {
criteria: FailureActionCriteriaVisitor,
},
},
Components: {
$visitor: ComponentsVisitor,
fixedFields: {
inputs: ComponentsInputsVisitor,
parameters: ComponentsParametersVisitor,
},
},
Criterion: {
$visitor: CriterionVisitor,
fixedFields: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import stampit from 'stampit';
import { always } from 'ramda';

import ComponentsInputsElement from '../../../../elements/nces/ComponentsInputs';
import MapVisitor from '../../generics/MapVisitor';
import FallbackVisitor from '../../FallbackVisitor';

const InputsVisitor = stampit(MapVisitor, FallbackVisitor, {
props: {
specPath: always(['document', 'objects', 'JSONSchema']),
},
init() {
this.element = new ComponentsInputsElement();
},
});

export default InputsVisitor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import stampit from 'stampit';
import { always } from 'ramda';

import ComponentsParametersElement from '../../../../elements/nces/ComponentsParameters';
import FallbackVisitor from '../../FallbackVisitor';
import MapVisitor from '../../generics/MapVisitor';

const ParametersVisitor = stampit(MapVisitor, FallbackVisitor, {
props: {
specPath: always(['document', 'objects', 'Parameter']),
},
init() {
this.element = new ComponentsParametersElement();
},
});

export default ParametersVisitor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import stampit from 'stampit';
import { always } from 'ramda';

import ComponentsElement from '../../../../elements/Components';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
import FallbackVisitor from '../../FallbackVisitor';

const ComponentsVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, {
props: {
specPath: always(['document', 'objects', 'Components']),
canSupportSpecificationExtensions: true,
},
init() {
this.element = new ComponentsElement();
},
});

export default ComponentsVisitor;
3 changes: 2 additions & 1 deletion packages/apidom-ns-workflows-1/src/traversal/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export const keyMap = {
ParameterElement: ['content'],
SuccessActionElement: ['content'],
FailureActionElement: ['content'],
ComponentsElement: ['content'],
CriterionElement: ['content'],
ReferenceElement: ['content'],
JSONSSchemaDraft202012Element: ['content'],
JSONSchemaDraft202012Element: ['content'],
...keyMapBase,
};
55 changes: 55 additions & 0 deletions packages/apidom-ns-workflows-1/test/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isSuccessActionCriteriaElement,
isFailureActionElement,
isFailureActionCriteriaElement,
isComponentsElement,
isCriterionElement,
isReferenceElement,
WorkflowsSpecification1Element,
Expand All @@ -24,6 +25,7 @@ import {
SuccessActionCriteriaElement,
FailureActionElement,
FailureActionCriteriaElement,
ComponentsElement,
CriterionElement,
ReferenceElement,
} from '../src';
Expand Down Expand Up @@ -302,6 +304,59 @@ describe('predicates', function () {
});
});

context('isComponentsElement', function () {
context('given ComponentsElement instance value', function () {
specify('should return true', function () {
const element = new ComponentsElement();

assert.isTrue(isComponentsElement(element));
});
});

context('given subtype instance value', function () {
specify('should return true', function () {
class ComponentsSubElement extends ComponentsElement {}

assert.isTrue(isComponentsElement(new ComponentsSubElement()));
});
});

context('given non ComponentsElement instance value', function () {
specify('should return false', function () {
assert.isFalse(isComponentsElement(1));
assert.isFalse(isComponentsElement(null));
assert.isFalse(isComponentsElement(undefined));
assert.isFalse(isComponentsElement({}));
assert.isFalse(isComponentsElement([]));
assert.isFalse(isComponentsElement('string'));
});
});

specify('should support duck-typing', function () {
const componentsElementDuck = {
_storedElement: 'components',
_content: [],
primitive() {
return 'object';
},
get element() {
return this._storedElement;
},
};

const componentsElementSwan = {
_storedElement: undefined,
_content: undefined,
primitive() {
return 'swan';
},
};

assert.isTrue(isComponentsElement(componentsElementDuck));
assert.isFalse(isComponentsElement(componentsElementSwan));
});
});

context('isCriterionElement', function () {
context('given CriterionElement instance value', function () {
specify('should return true', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`refractor elements ComponentsElement should refract to semantic ApiDOM tree 1`] = `
(ComponentsElement
(MemberElement
(StringElement)
(ObjectElement
(MemberElement
(StringElement)
(JSONSchemaDraft202012Element))))
(MemberElement
(StringElement)
(ObjectElement
(MemberElement
(StringElement)
(ParameterElement)))))
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { expect } from 'chai';
import { sexprs } from '@swagger-api/apidom-core';

import { ComponentsElement } from '../../../../src';

describe('refractor', function () {
context('elements', function () {
context('ComponentsElement', function () {
specify('should refract to semantic ApiDOM tree', function () {
const componentsElement = ComponentsElement.refract({
inputs: {
Schema1: {},
},
parameters: {
Parameter1: {},
},
});

expect(sexprs(componentsElement)).toMatchSnapshot();
});
});
});
});

0 comments on commit 14dd260

Please sign in to comment.