Skip to content

Commit

Permalink
feat(ns-openapi-2): add support for Tag Object (#3246)
Browse files Browse the repository at this point in the history
Refs #3097
  • Loading branch information
char0n committed Oct 10, 2023
1 parent 219f96d commit 7adf623
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/apidom-ns-openapi-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Only fully implemented specification objects should be checked here.
- [ ] [Headers Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-headers-object)
- [ ] [Example Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-example-object)
- [ ] [Header Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-header-object)
- [ ] [Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-tag-object)
- [x] [Tag Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-tag-object)
- [ ] [Reference Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-reference-object)
- [ ] [Schema Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-schema-object)
- [x] [XML Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-xml-object)
Expand Down
36 changes: 36 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/Tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ObjectElement, StringElement, Attributes, Meta } from '@swagger-api/apidom-core';

import ExternalDocumentationElement from './ExternalDocumentation';

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

get name(): StringElement | undefined {
return this.get('name');
}

set name(name: StringElement | undefined) {
this.set('name', name);
}

get description(): StringElement | undefined {
return this.get('description');
}

set description(description: StringElement | undefined) {
this.set('description', description);
}

get externalDocs(): ExternalDocumentationElement | undefined {
return this.get('externalDocs');
}

set externalDocs(externalDocs: ExternalDocumentationElement | undefined) {
this.set('externalDocs', externalDocs);
}
}

export default Tag;
2 changes: 2 additions & 0 deletions packages/apidom-ns-openapi-2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {
isLicenseElement,
isContactElement,
isExternalDocumentationElement,
isTagElement,
isXmlElement,
isSecurityDefinitionsElement,
isSecuritySchemeElement,
Expand All @@ -39,6 +40,7 @@ export {
LicenseElement,
ContactElement,
ExternalDocumentationElement,
TagElement,
XmlElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
Expand Down
2 changes: 2 additions & 0 deletions packages/apidom-ns-openapi-2/src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import InfoElement from './elements/Info';
import LicenseElement from './elements/License';
import ContactElement from './elements/Contact';
import ExternalDocumentation from './elements/ExternalDocumentation';
import TagElement from './elements/Tag';
import XmlElement from './elements/Xml';
import SecurityDefinitionsElement from './elements/SecurityDefinitions';
import SecuritySchemeElement from './elements/SecurityScheme';
Expand All @@ -18,6 +19,7 @@ const openApi2 = {
base.register('license', LicenseElement);
base.register('contact', ContactElement);
base.register('externalDocumentation', ExternalDocumentation);
base.register('tag', TagElement);
base.register('xml', XmlElement);
base.register('securityDefinitions', SecurityDefinitionsElement);
base.register('securityScheme', SecuritySchemeElement);
Expand Down
11 changes: 11 additions & 0 deletions packages/apidom-ns-openapi-2/src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ import InfoElement from './elements/Info';
import LicenseElement from './elements/License';
import ContactElement from './elements/Contact';
import ExternalDocumentation from './elements/ExternalDocumentation';
import TagElement from './elements/Tag';
import XmlElement from './elements/Xml';
import SecurityDefinitionsElement from './elements/SecurityDefinitions';
import SecuritySchemeElement from './elements/SecurityScheme';
import SecurityRequirementElement from './elements/SecurityRequirement';
import ScopesElement from './elements/Scopes';

export const isTagElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: any) =>
element instanceof TagElement ||
(hasBasicElementProps(element) &&
isElementType('tag', element) &&
primitiveEq('object', element));
},
);

export const isInfoElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: any) =>
Expand Down
3 changes: 3 additions & 0 deletions packages/apidom-ns-openapi-2/src/refractor/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import InfoElement from '../elements/Info';
import LicenseElement from '../elements/License';
import ContactElement from '../elements/Contact';
import ExternalDocumentationElement from '../elements/ExternalDocumentation';
import TagElement from '../elements/Tag';
import XmlElement from '../elements/Xml';
import SecurityDefinitionsElement from '../elements/SecurityDefinitions';
import SecuritySchemeElement from '../elements/SecurityScheme';
Expand Down Expand Up @@ -32,6 +33,7 @@ ExternalDocumentationElement.refract = createRefractor([
'ExternalDocumentation',
'$visitor',
]);
TagElement.refract = createRefractor(['visitors', 'document', 'objects', 'Tag', '$visitor']);
XmlElement.refract = createRefractor(['visitors', 'document', 'objects', 'XML', '$visitor']);
SecurityDefinitionsElement.refract = createRefractor([
'visitors',
Expand Down Expand Up @@ -61,6 +63,7 @@ export {
LicenseElement,
ContactElement,
ExternalDocumentationElement,
TagElement,
XmlElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
Expand Down
11 changes: 11 additions & 0 deletions packages/apidom-ns-openapi-2/src/refractor/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import InfoVersionVisitor from './visitors/open-api-2/info/VersionVisitor';
import LicenseVisitor from './visitors/open-api-2/license';
import ContactVisitor from './visitors/open-api-2/contact';
import ExternalDocumentationElement from './visitors/open-api-2/external-documentation';
import TagVisitor from './visitors/open-api-2/tag';
import XmlVisitor from './visitors/open-api-2/xml';
import SecurityDefinitionsVisitor from './visitors/open-api-2/security-definitions';
import SecuritySchemeVisitor from './visitors/open-api-2/security-scheme';
Expand Down Expand Up @@ -62,6 +63,16 @@ const specification = {
url: FallbackVisitor,
},
},
Tag: {
$visitor: TagVisitor,
fixedFields: {
name: FallbackVisitor,
description: FallbackVisitor,
externalDocs: {
$ref: '#/visitors/document/objects/ExternalDocumentation',
},
},
},
XML: {
$visitor: XmlVisitor,
fixedFields: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import stampit from 'stampit';
import { always } from 'ramda';

import TagElement from '../../../../elements/Tag';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';

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

export default TagVisitor;
1 change: 1 addition & 0 deletions packages/apidom-ns-openapi-2/src/traversal/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const keyMap = {
LicenseElement: ['content'],
ContactElement: ['content'],
ExternalDocumentationElement: ['content'],
TagElement: ['content'],
XmlElement: ['content'],
SecurityDefinitionsElement: ['content'],
SecuritySchemeElement: ['content'],
Expand Down
59 changes: 59 additions & 0 deletions packages/apidom-ns-openapi-2/test/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LicenseElement,
ContactElement,
ExternalDocumentationElement,
TagElement,
XmlElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
Expand All @@ -14,6 +15,7 @@ import {
isLicenseElement,
isContactElement,
isExternalDocumentationElement,
isTagElement,
isXmlElement,
isSecurityDefinitionsElement,
isSecuritySchemeElement,
Expand All @@ -22,6 +24,63 @@ import {
} from '../src';

describe('predicates', function () {
context('isTagElement', function () {
context('given TagElement instance value', function () {
specify('should return true', function () {
const element = new TagElement();

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

context('given subtype instance value', function () {
specify('should return true', function () {
// eslint-disable-next-line @typescript-eslint/naming-convention
class TagSubElement extends TagElement {}

assert.isTrue(isTagElement(new TagSubElement()));
});
});

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

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

const tagElementSwan = {
_storedElement: undefined,
_content: undefined,
primitive() {
return 'swan';
},
get length() {
return 0;
},
};

assert.isTrue(isTagElement(tagElementDuck));
assert.isFalse(isTagElement(tagElementSwan));
});
});

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

exports[`refractor elements TagElement should refract to semantic ApiDOM tree 1`] = `
(TagElement
(MemberElement
(StringElement)
(StringElement))
(MemberElement
(StringElement)
(StringElement))
(MemberElement
(StringElement)
(ExternalDocumentationElement)))
`;
20 changes: 20 additions & 0 deletions packages/apidom-ns-openapi-2/test/refractor/elements/Tag/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect } from 'chai';
import { sexprs } from '@swagger-api/apidom-core';

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

describe('refractor', function () {
context('elements', function () {
context('TagElement', function () {
specify('should refract to semantic ApiDOM tree', function () {
const tagElement = TagElement.refract({
name: 'tag-name',
description: 'tag-description',
externalDocs: {},
});

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

0 comments on commit 7adf623

Please sign in to comment.