From 4f0c783ff7910940d28f4fe3a166418d36c12357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Fri, 6 Oct 2023 16:45:44 +0200 Subject: [PATCH] feat(ns-openapi-2): add support for Contact Object (#3236) Refs #3097 --- packages/apidom-ns-openapi-2/README.md | 2 +- .../src/elements/Contact.ts | 34 +++++++++++ packages/apidom-ns-openapi-2/src/index.ts | 2 + packages/apidom-ns-openapi-2/src/namespace.ts | 2 + .../apidom-ns-openapi-2/src/predicates.ts | 10 ++++ .../src/refractor/registration.ts | 9 +++ .../src/refractor/specification.ts | 9 +++ .../visitors/open-api-2/contact/index.ts | 18 ++++++ .../src/traversal/visitor.ts | 1 + .../apidom-ns-openapi-2/test/predicates.ts | 59 +++++++++++++++++++ .../Contact/__snapshots__/index.ts.snap | 14 +++++ .../test/refractor/elements/Contact/index.ts | 34 +++++++++++ 12 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 packages/apidom-ns-openapi-2/src/elements/Contact.ts create mode 100644 packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/contact/index.ts create mode 100644 packages/apidom-ns-openapi-2/test/refractor/elements/Contact/__snapshots__/index.ts.snap create mode 100644 packages/apidom-ns-openapi-2/test/refractor/elements/Contact/index.ts diff --git a/packages/apidom-ns-openapi-2/README.md b/packages/apidom-ns-openapi-2/README.md index c500fe69bf..ae823d532d 100644 --- a/packages/apidom-ns-openapi-2/README.md +++ b/packages/apidom-ns-openapi-2/README.md @@ -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) diff --git a/packages/apidom-ns-openapi-2/src/elements/Contact.ts b/packages/apidom-ns-openapi-2/src/elements/Contact.ts new file mode 100644 index 0000000000..7b7eb26031 --- /dev/null +++ b/packages/apidom-ns-openapi-2/src/elements/Contact.ts @@ -0,0 +1,34 @@ +import { StringElement, ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core'; + +class Contact extends ObjectElement { + constructor(content?: Record, 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; diff --git a/packages/apidom-ns-openapi-2/src/index.ts b/packages/apidom-ns-openapi-2/src/index.ts index 7688e200b2..9beb06bd11 100644 --- a/packages/apidom-ns-openapi-2/src/index.ts +++ b/packages/apidom-ns-openapi-2/src/index.ts @@ -20,6 +20,7 @@ export { default as refract, createRefractor } from './refractor'; export { default as specificationObj } from './refractor/specification'; export { + isContactElement, isExternalDocumentationElement, isXmlElement, isSecurityDefinitionsElement, @@ -32,6 +33,7 @@ export { keyMap, getNodeType } from './traversal/visitor'; // OpenAPI 2.0 elements export { + ContactElement, ExternalDocumentationElement, XmlElement, SecurityDefinitionsElement, diff --git a/packages/apidom-ns-openapi-2/src/namespace.ts b/packages/apidom-ns-openapi-2/src/namespace.ts index d4feccaabb..2c772df40b 100644 --- a/packages/apidom-ns-openapi-2/src/namespace.ts +++ b/packages/apidom-ns-openapi-2/src/namespace.ts @@ -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'; @@ -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); diff --git a/packages/apidom-ns-openapi-2/src/predicates.ts b/packages/apidom-ns-openapi-2/src/predicates.ts index 62d253c3d2..daf881ce0b 100644 --- a/packages/apidom-ns-openapi-2/src/predicates.ts +++ b/packages/apidom-ns-openapi-2/src/predicates.ts @@ -1,5 +1,6 @@ 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'; @@ -7,6 +8,15 @@ 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) => diff --git a/packages/apidom-ns-openapi-2/src/refractor/registration.ts b/packages/apidom-ns-openapi-2/src/refractor/registration.ts index 79618a68a7..0380cb196e 100644 --- a/packages/apidom-ns-openapi-2/src/refractor/registration.ts +++ b/packages/apidom-ns-openapi-2/src/refractor/registration.ts @@ -1,3 +1,4 @@ +import ContactElement from '../elements/Contact'; import ExternalDocumentationElement from '../elements/ExternalDocumentation'; import XmlElement from '../elements/Xml'; import SecurityDefinitionsElement from '../elements/SecurityDefinitions'; @@ -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', @@ -39,6 +47,7 @@ SecurityRequirementElement.refract = createRefractor([ ]); export { + ContactElement, ExternalDocumentationElement, XmlElement, SecurityDefinitionsElement, diff --git a/packages/apidom-ns-openapi-2/src/refractor/specification.ts b/packages/apidom-ns-openapi-2/src/refractor/specification.ts index cf76514b81..66ce32c52b 100644 --- a/packages/apidom-ns-openapi-2/src/refractor/specification.ts +++ b/packages/apidom-ns-openapi-2/src/refractor/specification.ts @@ -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'; @@ -21,6 +22,14 @@ const specification = { value: FallbackVisitor, document: { objects: { + Contact: { + $visitor: ContactVisitor, + fixedFields: { + name: FallbackVisitor, + url: FallbackVisitor, + email: FallbackVisitor, + }, + }, ExternalDocumentation: { $visitor: ExternalDocumentationElement, fixedFields: { diff --git a/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/contact/index.ts b/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/contact/index.ts new file mode 100644 index 0000000000..f12c7a7a7e --- /dev/null +++ b/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/contact/index.ts @@ -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; diff --git a/packages/apidom-ns-openapi-2/src/traversal/visitor.ts b/packages/apidom-ns-openapi-2/src/traversal/visitor.ts index 989a8930ce..eac54072c9 100644 --- a/packages/apidom-ns-openapi-2/src/traversal/visitor.ts +++ b/packages/apidom-ns-openapi-2/src/traversal/visitor.ts @@ -19,6 +19,7 @@ export const getNodeType = (element: T): string | undefined = */ export const keyMap = { + ContactElement: ['content'], ExternalDocumentationElement: ['content'], XmlElement: ['content'], SecurityDefinitionsElement: ['content'], diff --git a/packages/apidom-ns-openapi-2/test/predicates.ts b/packages/apidom-ns-openapi-2/test/predicates.ts index a884b22c79..ada90e5555 100644 --- a/packages/apidom-ns-openapi-2/test/predicates.ts +++ b/packages/apidom-ns-openapi-2/test/predicates.ts @@ -1,12 +1,14 @@ import { assert } from 'chai'; import { + ContactElement, ExternalDocumentationElement, XmlElement, SecurityDefinitionsElement, SecuritySchemeElement, ScopesElement, SecurityRequirementElement, + isContactElement, isExternalDocumentationElement, isXmlElement, isSecurityDefinitionsElement, @@ -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 () { diff --git a/packages/apidom-ns-openapi-2/test/refractor/elements/Contact/__snapshots__/index.ts.snap b/packages/apidom-ns-openapi-2/test/refractor/elements/Contact/__snapshots__/index.ts.snap new file mode 100644 index 0000000000..ba97c975e4 --- /dev/null +++ b/packages/apidom-ns-openapi-2/test/refractor/elements/Contact/__snapshots__/index.ts.snap @@ -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))) +`; diff --git a/packages/apidom-ns-openapi-2/test/refractor/elements/Contact/index.ts b/packages/apidom-ns-openapi-2/test/refractor/elements/Contact/index.ts new file mode 100644 index 0000000000..2ebcd9bcfe --- /dev/null +++ b/packages/apidom-ns-openapi-2/test/refractor/elements/Contact/index.ts @@ -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: 'support@example.com', + }); + + 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')), + ); + }); + }); + }); +});