Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ns-openapi-2): add support for Security Definitions Object #3228

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -206,7 +206,7 @@ Only fully implemented specification objects should be checked here.
- [ ] [Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-definitions-object)
- [ ] [Parameters Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-paramters-definitions-object)
- [ ] [Responses Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-responses-definitions-object)
- [ ] [Security Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-security-definitions-object)
- [x] [Security Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-security-definitions-object)
- [x] [Security Scheme Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-security-scheme-object)
- [x] [Scopes Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-scopes-object)
- [x] [Security Requirement Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-security-requirement-object)
Expand Down
10 changes: 10 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/SecurityDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ObjectElement, Attributes, Meta } from '@swagger-api/apidom-core';

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

export default SecurityDefinitions;
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 {
isSecurityDefinitionsElement,
isSecuritySchemeElement,
isScopesElement,
isSecurityRequirementElement,
Expand All @@ -29,6 +30,7 @@ export { keyMap, getNodeType } from './traversal/visitor';

// OpenAPI 2.0 elements
export {
SecurityDefinitionsElement,
SecuritySchemeElement,
ScopesElement,
SecurityRequirementElement,
Expand Down
6 changes: 4 additions & 2 deletions packages/apidom-ns-openapi-2/src/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { NamespacePluginOptions } from '@swagger-api/apidom-core';

import SecurityDefinitionsElement from './elements/SecurityDefinitions';
import SecuritySchemeElement from './elements/SecurityScheme';
import ScopesElement from './elements/ScopesElement';
import ScopesElement from './elements/Scopes';
import SecurityRequirementElement from './elements/SecurityRequirement';

const openApi2 = {
namespace: (options: NamespacePluginOptions) => {
const { base } = options;

base.register('securityDefinitions', SecurityDefinitionsElement);
base.register('securityScheme', SecuritySchemeElement);
base.register('scopes', ScopesElement);
base.register('securityRequirement', SecurityRequirementElement);
base.register('securityScheme', SecuritySchemeElement);

return base;
},
Expand Down
13 changes: 12 additions & 1 deletion packages/apidom-ns-openapi-2/src/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { createPredicate } from '@swagger-api/apidom-core';

import SecurityDefinitionsElement from './elements/SecurityDefinitions';
import SecuritySchemeElement from './elements/SecurityScheme';
import SecurityRequirementElement from './elements/SecurityRequirement';
import ScopesElement from './elements/ScopesElement';
import ScopesElement from './elements/Scopes';

export const isSecurityDefinitionsElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: any) =>
element instanceof SecurityDefinitionsElement ||
(hasBasicElementProps(element) &&
isElementType('securityDefinitions', element) &&
primitiveEq('object', element));
},
);

export const isSecuritySchemeElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
Expand Down
17 changes: 15 additions & 2 deletions packages/apidom-ns-openapi-2/src/refractor/registration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import SecurityDefinitionsElement from '../elements/SecurityDefinitions';
import SecuritySchemeElement from '../elements/SecurityScheme';
import ScopesElement from '../elements/ScopesElement';
import ScopesElement from '../elements/Scopes';
import SecurityRequirementElement from '../elements/SecurityRequirement';
import { createRefractor } from './index';

// register refractors specific to element types
SecurityDefinitionsElement.refract = createRefractor([
'visitors',
'document',
'objects',
'SecurityDefinitions',
'$visitor',
]);
SecuritySchemeElement.refract = createRefractor([
'visitors',
'document',
Expand All @@ -20,4 +28,9 @@ SecurityRequirementElement.refract = createRefractor([
'$visitor',
]);

export { SecuritySchemeElement, ScopesElement, SecurityRequirementElement };
export {
SecurityDefinitionsElement,
SecuritySchemeElement,
ScopesElement,
SecurityRequirementElement,
};
4 changes: 4 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 SecurityDefinitionsVisitor from './visitors/open-api-2/security-definitions';
import SecuritySchemeVisitor from './visitors/open-api-2/security-scheme';
import ScopesVisitor from './visitors/open-api-2/scopes';
import SecurityRequirementVisitor from './visitors/open-api-2/security-requirement';
Expand All @@ -18,6 +19,9 @@ const specification = {
value: FallbackVisitor,
document: {
objects: {
SecurityDefinitions: {
$visitor: SecurityDefinitionsVisitor,
},
SecurityScheme: {
$visitor: SecuritySchemeVisitor,
fixedFields: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import stampit from 'stampit';
import { always } from 'ramda';

import ScopesElement from '../../../../elements/ScopesElement';
import ScopesElement from '../../../../elements/Scopes';
import MapVisitor from '../../generics/MapVisitor';
import FallbackVisitor from '../../FallbackVisitor';

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 SecurityDefinitionsElement from '../../../../elements/SecurityDefinitions';
import MapVisitor from '../../generics/MapVisitor';
import FallbackVisitor from '../../FallbackVisitor';

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

export default SecurityDefinitionsVisitor;
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 = {
SecurityDefinitionsElement: ['content'],
SecuritySchemeElement: ['content'],
ScopesElement: ['content'],
SecurityRequirementElement: ['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,15 +1,74 @@
import { assert } from 'chai';

import {
SecurityDefinitionsElement,
SecuritySchemeElement,
ScopesElement,
SecurityRequirementElement,
isSecurityDefinitionsElement,
isSecuritySchemeElement,
isScopesElement,
isSecurityRequirementElement,
} from '../src';

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

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

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

assert.isTrue(isSecurityDefinitionsElement(new SecurityDefinitionsSubElement()));
});
});

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

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

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

assert.isTrue(isSecurityDefinitionsElement(securityDefinitionsElementDuck));
assert.isFalse(isSecurityDefinitionsElement(securityDefinitionsElementSwan));
});
});

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

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

describe('refractor', function () {
context('elements', function () {
context('SecurityDefinitions', function () {
specify('should refract to semantic ApiDOM tree', function () {
const securityDefinitionsElement = SecurityDefinitionsElement.refract({
api_key: {},
petstore_auth: {},
});

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