Skip to content

Commit

Permalink
feat(ns-openapi-2): add support for External Documentation Object (#3235
Browse files Browse the repository at this point in the history
)

Refs #3097
  • Loading branch information
char0n authored Oct 6, 2023
1 parent 9c0b4f5 commit b12d915
Show file tree
Hide file tree
Showing 12 changed files with 187 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 @@ -191,7 +191,7 @@ Only fully implemented specification objects should be checked here.
- [ ] [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)
- [ ] [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-operation-object)
- [ ] [External Documentation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-external-documentation-object)
- [x] [External Documentation Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-external-documentation-object)
- [ ] [Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-parameter-object)
- [ ] [Items Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-items-object)
- [ ] [Responses Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-responses-object)
Expand Down
26 changes: 26 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/ExternalDocumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { StringElement, ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core';

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

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

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

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

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

export default ExternalDocumentation;
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 {
isExternalDocumentationElement,
isXmlElement,
isSecurityDefinitionsElement,
isSecuritySchemeElement,
Expand All @@ -31,6 +32,7 @@ export { keyMap, getNodeType } from './traversal/visitor';

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

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

base.register('externalDocumentation', ExternalDocumentation);
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
@@ -1,11 +1,22 @@
import { createPredicate } from '@swagger-api/apidom-core';

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 isExternalDocumentationElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: any) =>
element instanceof ExternalDocumentation ||
(hasBasicElementProps(element) &&
isElementType('externalDocumentation', element) &&
primitiveEq('object', element));
},
);

export const isXmlElement = 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 ExternalDocumentationElement from '../elements/ExternalDocumentation';
import XmlElement from '../elements/Xml';
import SecurityDefinitionsElement from '../elements/SecurityDefinitions';
import SecuritySchemeElement from '../elements/SecurityScheme';
Expand All @@ -6,6 +7,13 @@ import SecurityRequirementElement from '../elements/SecurityRequirement';
import { createRefractor } from './index';

// register refractors specific to element types
ExternalDocumentationElement.refract = createRefractor([
'visitors',
'document',
'objects',
'ExternalDocumentation',
'$visitor',
]);
XmlElement.refract = createRefractor(['visitors', 'document', 'objects', 'XML', '$visitor']);
SecurityDefinitionsElement.refract = createRefractor([
'visitors',
Expand All @@ -31,6 +39,7 @@ SecurityRequirementElement.refract = createRefractor([
]);

export {
ExternalDocumentationElement,
XmlElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
Expand Down
8 changes: 8 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 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';
import SecuritySchemeVisitor from './visitors/open-api-2/security-scheme';
Expand All @@ -20,6 +21,13 @@ const specification = {
value: FallbackVisitor,
document: {
objects: {
ExternalDocumentation: {
$visitor: ExternalDocumentationElement,
fixedFields: {
description: FallbackVisitor,
url: FallbackVisitor,
},
},
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 ExternalDocumentationElement from '../../../../elements/ExternalDocumentation';
import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor';
import FallbackVisitor from '../../FallbackVisitor';

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

export default ExternalDocumentationVisitor;
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 = {
ExternalDocumentationElement: ['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
@@ -1,11 +1,13 @@
import { assert } from 'chai';

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

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

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

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

assert.isTrue(isExternalDocumentationElement(new ExternalDocumentationSubElement()));
});
});

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

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

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

assert.isTrue(isExternalDocumentationElement(externalDocumentationElementDuck));
assert.isFalse(isExternalDocumentationElement(externalDocumentationElementSwan));
});
});

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

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

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

describe('refractor', function () {
context('elements', function () {
context('ExternalDocumentationElement', function () {
specify('should refract to semantic ApiDOM tree', function () {
const externalDocumentationElement = ExternalDocumentationElement.refract({
description: 'Find more info here',
url: 'https://swagger.io',
});

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

specify('should support specification extensions', function () {
const externalDocumentationElement = ExternalDocumentationElement.refract({
description: 'Find more info here',
'x-extension': 'extension',
}) as ExternalDocumentationElement;

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

0 comments on commit b12d915

Please sign in to comment.