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 Parameters Definitions Object #3291

Merged
merged 1 commit into from
Oct 18, 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 @@ -205,7 +205,7 @@ Only fully implemented specification objects should be checked here.
- [x] [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)
- [ ] [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)
- [x] [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)
- [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)
Expand Down
10 changes: 10 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/ParametersDefinitions.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 ParametersDefinitions extends ObjectElement {
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'parametersDefinitions';
}
}

export default ParametersDefinitions;
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 @@ -37,6 +37,7 @@ export {
isReferenceElement,
isSchemaElement,
isXmlElement,
isParametersDefinitionsElement,
isSecurityDefinitionsElement,
isSecuritySchemeElement,
isScopesElement,
Expand All @@ -60,6 +61,7 @@ export {
ReferenceElement,
SchemaElement,
XmlElement,
ParametersDefinitionsElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
ScopesElement,
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 @@ -13,6 +13,7 @@ import TagElement from './elements/Tag';
import SchemaElement from './elements/Schema';
import XmlElement from './elements/Xml';
import ReferenceElement from './elements/Reference';
import ParametersDefinitionsElement from './elements/ParametersDefinitions';
import SecurityDefinitionsElement from './elements/SecurityDefinitions';
import SecuritySchemeElement from './elements/SecurityScheme';
import ScopesElement from './elements/Scopes';
Expand All @@ -35,6 +36,7 @@ const openApi2 = {
base.register('reference', ReferenceElement);
base.register('schema', SchemaElement);
base.register('xml', XmlElement);
base.register('parametersDefinitions', ParametersDefinitionsElement);
base.register('securityDefinitions', SecurityDefinitionsElement);
base.register('securityScheme', SecuritySchemeElement);
base.register('scopes', ScopesElement);
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 @@ -13,6 +13,7 @@ import TagElement from './elements/Tag';
import ReferenceElement from './elements/Reference';
import SchemaElement from './elements/Schema';
import XmlElement from './elements/Xml';
import ParametersDefinitionsElement from './elements/ParametersDefinitions';
import SecurityDefinitionsElement from './elements/SecurityDefinitions';
import SecuritySchemeElement from './elements/SecurityScheme';
import SecurityRequirementElement from './elements/SecurityRequirement';
Expand Down Expand Up @@ -157,6 +158,16 @@ export const isSecurityDefinitionsElement = createPredicate(
},
);

export const isParametersDefinitionsElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is ParametersDefinitionsElement =>
element instanceof ParametersDefinitionsElement ||
(hasBasicElementProps(element) &&
isElementType('parametersDefinitions', element) &&
primitiveEq('object', element));
},
);

export const isSecuritySchemeElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is SecuritySchemeElement =>
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
Expand Up @@ -11,6 +11,7 @@ import TagElement from '../elements/Tag';
import ReferenceElement from '../elements/Reference';
import SchemaElement from '../elements/Schema';
import XmlElement from '../elements/Xml';
import ParametersDefinitionsElement from '../elements/ParametersDefinitions';
import SecurityDefinitionsElement from '../elements/SecurityDefinitions';
import SecuritySchemeElement from '../elements/SecurityScheme';
import ScopesElement from '../elements/Scopes';
Expand Down Expand Up @@ -73,6 +74,13 @@ ReferenceElement.refract = createRefractor([
]);
SchemaElement.refract = createRefractor(['visitors', 'document', 'objects', 'Schema', '$visitor']);
XmlElement.refract = createRefractor(['visitors', 'document', 'objects', 'XML', '$visitor']);
ParametersDefinitionsElement.refract = createRefractor([
'visitors',
'document',
'objects',
'ParametersDefinitions',
'$visitor',
]);
SecurityDefinitionsElement.refract = createRefractor([
'visitors',
'document',
Expand Down Expand Up @@ -110,6 +118,7 @@ export {
ReferenceElement,
SchemaElement,
XmlElement,
ParametersDefinitionsElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
ScopesElement,
Expand Down
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
Expand Up @@ -20,6 +20,7 @@ import SchemaItemsVisitor from './visitors/open-api-2/schema/ItemsVisitor';
import SchemaPropertiesVisitor from './visitors/open-api-2/schema/PropertiesVisitor';
import SchemaOrJSONReferenceVisitor from './visitors/open-api-2/schema/SchemaOrJSONReferenceVisitor';
import XmlVisitor from './visitors/open-api-2/xml';
import ParametersDefinitionsVisitor from './visitors/open-api-2/parameters-definitions';
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';
Expand Down Expand Up @@ -237,6 +238,9 @@ const specification = {
wrapped: FallbackVisitor,
},
},
ParametersDefinitions: {
$visitor: ParametersDefinitionsVisitor,
},
SecurityDefinitions: {
$visitor: SecurityDefinitionsVisitor,
},
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 ParametersDefinitionsElement from '../../../../elements/ParametersDefinitions';
import MapVisitor from '../../generics/MapVisitor';
import FallbackVisitor from '../../FallbackVisitor';

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

export default ParametersDefinitionsVisitor;
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 @@ -33,6 +33,7 @@ export const keyMap = {
JSONReferenceElement: ['content'],
SchemaElement: ['content'],
XmlElement: ['content'],
ParametersDefinitionsElement: ['content'],
SecurityDefinitionsElement: ['content'],
SecuritySchemeElement: ['content'],
ScopesElement: ['content'],
Expand Down
58 changes: 58 additions & 0 deletions packages/apidom-ns-openapi-2/test/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ReferenceElement,
SchemaElement,
XmlElement,
ParametersDefinitionsElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
ScopesElement,
Expand All @@ -31,6 +32,7 @@ import {
isReferenceElement,
isSchemaElement,
isXmlElement,
isParametersDefinitionsElement,
isSecurityDefinitionsElement,
isSecuritySchemeElement,
isScopesElement,
Expand Down Expand Up @@ -766,6 +768,62 @@ describe('predicates', function () {
});
});

context('isParametersDefinitionsElement', function () {
context('given ParametersDefinitionsElement instance value', function () {
specify('should return true', function () {
const element = new ParametersDefinitionsElement();

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

context('given subtype instance value', function () {
specify('should return true', function () {
class ParametersDefinitionsSubElement extends ParametersDefinitionsElement {}

assert.isTrue(isParametersDefinitionsElement(new ParametersDefinitionsSubElement()));
});
});

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

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

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

assert.isTrue(isParametersDefinitionsElement(parametersDefinitionsElementDuck));
assert.isFalse(isParametersDefinitionsElement(parametersDefinitionsElementSwan));
});
});

context('isSecurityDefinitionsElement', function () {
context('given SecurityDefinitionsElement 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 ParametersDefinitionsElement should refract to semantic ApiDOM tree 1`] = `
(ParametersDefinitionsElement
(MemberElement
(StringElement)
(ParameterElement))
(MemberElement
(StringElement)
(ParameterElement)))
`;
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 { ParametersDefinitionsElement } from '../../../../src';

describe('refractor', function () {
context('elements', function () {
context('ParametersDefinitionsElement', function () {
specify('should refract to semantic ApiDOM tree', function () {
const parametersDefinitionsElement = ParametersDefinitionsElement.refract({
parameter1: {},
parameter2: {},
});

expect(sexprs(parametersDefinitionsElement)).toMatchSnapshot();
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`refractor elements SecurityDefinitions should refract to semantic ApiDOM tree 1`] = `
exports[`refractor elements SecurityDefinitionsElement should refract to semantic ApiDOM tree 1`] = `
(SecurityDefinitionsElement
(MemberElement
(StringElement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SecurityDefinitionsElement } from '../../../../src';

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