Skip to content

Commit

Permalink
feat(ns-openapi-2): add support for Contact Object (#3236)
Browse files Browse the repository at this point in the history
Refs #3097
  • Loading branch information
char0n authored Oct 6, 2023
1 parent b12d915 commit 4f0c783
Show file tree
Hide file tree
Showing 12 changed files with 193 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 @@ -186,7 +186,7 @@ Only fully implemented specification objects should be checked here.

- [ ] [Swagger Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-swagger-object)
- [ ] [Info Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-info-object)
- [ ] [Contact Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-contact-object)
- [x] [Contact Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-contact-object)
- [ ] [License Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-license-object)
- [ ] [Paths Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-paths-object)
- [ ] [Path Item Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-path-item-object)
Expand Down
34 changes: 34 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/Contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { StringElement, ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core';

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

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

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

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

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

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

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

export default Contact;
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 @@ -20,6 +20,7 @@ export { default as refract, createRefractor } from './refractor';
export { default as specificationObj } from './refractor/specification';

export {
isContactElement,
isExternalDocumentationElement,
isXmlElement,
isSecurityDefinitionsElement,
Expand All @@ -32,6 +33,7 @@ export { keyMap, getNodeType } from './traversal/visitor';

// OpenAPI 2.0 elements
export {
ContactElement,
ExternalDocumentationElement,
XmlElement,
SecurityDefinitionsElement,
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
@@ -1,5 +1,6 @@
import { NamespacePluginOptions } from '@swagger-api/apidom-core';

import ContactElement from './elements/Contact';
import ExternalDocumentation from './elements/ExternalDocumentation';
import XmlElement from './elements/Xml';
import SecurityDefinitionsElement from './elements/SecurityDefinitions';
Expand All @@ -11,6 +12,7 @@ const openApi2 = {
namespace: (options: NamespacePluginOptions) => {
const { base } = options;

base.register('contact', ContactElement);
base.register('externalDocumentation', ExternalDocumentation);
base.register('xml', XmlElement);
base.register('securityDefinitions', SecurityDefinitionsElement);
Expand Down
10 changes: 10 additions & 0 deletions packages/apidom-ns-openapi-2/src/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { createPredicate } from '@swagger-api/apidom-core';

import ContactElement from './elements/Contact';
import ExternalDocumentation from './elements/ExternalDocumentation';
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 isContactElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: any) =>
element instanceof ContactElement ||
(hasBasicElementProps(element) &&
isElementType('contact', element) &&
primitiveEq('object', element));
},
);
export const isExternalDocumentationElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: any) =>
Expand Down
9 changes: 9 additions & 0 deletions packages/apidom-ns-openapi-2/src/refractor/registration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ContactElement from '../elements/Contact';
import ExternalDocumentationElement from '../elements/ExternalDocumentation';
import XmlElement from '../elements/Xml';
import SecurityDefinitionsElement from '../elements/SecurityDefinitions';
Expand All @@ -7,6 +8,13 @@ import SecurityRequirementElement from '../elements/SecurityRequirement';
import { createRefractor } from './index';

// register refractors specific to element types
ContactElement.refract = createRefractor([
'visitors',
'document',
'objects',
'Contact',
'$visitor',
]);
ExternalDocumentationElement.refract = createRefractor([
'visitors',
'document',
Expand Down Expand Up @@ -39,6 +47,7 @@ SecurityRequirementElement.refract = createRefractor([
]);

export {
ContactElement,
ExternalDocumentationElement,
XmlElement,
SecurityDefinitionsElement,
Expand Down
9 changes: 9 additions & 0 deletions packages/apidom-ns-openapi-2/src/refractor/specification.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FallbackVisitor from './visitors/FallbackVisitor';
import ContactVisitor from './visitors/open-api-2/contact';
import ExternalDocumentationElement from './visitors/open-api-2/external-documentation';
import XmlVisitor from './visitors/open-api-2/xml';
import SecurityDefinitionsVisitor from './visitors/open-api-2/security-definitions';
Expand All @@ -21,6 +22,14 @@ const specification = {
value: FallbackVisitor,
document: {
objects: {
Contact: {
$visitor: ContactVisitor,
fixedFields: {
name: FallbackVisitor,
url: FallbackVisitor,
email: FallbackVisitor,
},
},
ExternalDocumentation: {
$visitor: ExternalDocumentationElement,
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 ContactElement from '../../../../elements/Contact';
import FallbackVisitor from '../../FallbackVisitor';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';

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

export default ContactVisitor;
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 @@ -19,6 +19,7 @@ export const getNodeType = <T extends Element>(element: T): string | undefined =
*/

export const keyMap = {
ContactElement: ['content'],
ExternalDocumentationElement: ['content'],
XmlElement: ['content'],
SecurityDefinitionsElement: ['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
@@ -1,12 +1,14 @@
import { assert } from 'chai';

import {
ContactElement,
ExternalDocumentationElement,
XmlElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
ScopesElement,
SecurityRequirementElement,
isContactElement,
isExternalDocumentationElement,
isXmlElement,
isSecurityDefinitionsElement,
Expand All @@ -16,6 +18,63 @@ import {
} from '../src';

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

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

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

assert.isTrue(isContactElement(new ContactSubElement()));
});
});

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

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

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

assert.isTrue(isContactElement(contactElementDuck));
assert.isFalse(isContactElement(contactElementSwan));
});
});

context('isExternalDocumentationElement', function () {
context('given ExternalDocumentationElement 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 ContactElement should refract to semantic ApiDOM tree 1`] = `
(ContactElement
(MemberElement
(StringElement)
(StringElement))
(MemberElement
(StringElement)
(StringElement))
(MemberElement
(StringElement)
(StringElement)))
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { assert, expect } from 'chai';
import { includesClasses, sexprs } from '@swagger-api/apidom-core';

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

describe('refractor', function () {
context('elements', function () {
context('ContactElement', function () {
specify('should refract to semantic ApiDOM tree', function () {
const contactElement = ContactElement.refract({
name: 'API Support',
url: 'https://www.example.com/support',
email: '[email protected]',
});

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

specify('should support specification extensions', function () {
const contactElement = ContactElement.refract({
name: 'API support',
'x-extension': 'extension',
}) as ContactElement;

assert.isFalse(
includesClasses(['specification-extension'], contactElement.getMember('name')),
);
assert.isTrue(
includesClasses(['specification-extension'], contactElement.getMember('x-extension')),
);
});
});
});
});

0 comments on commit 4f0c783

Please sign in to comment.