Skip to content

Commit

Permalink
feat(ns-openapi-2): add support for Responses Definitions Object (#3294)
Browse files Browse the repository at this point in the history
Refs #3097
  • Loading branch information
char0n authored Oct 18, 2023
1 parent 006ec69 commit 51b15fe
Show file tree
Hide file tree
Showing 12 changed files with 145 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 @@ -206,7 +206,7 @@ Only fully implemented specification objects should be checked here.
- [x] [XML Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-xml-object)
- [x] [Definitions Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-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] [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)
- [x] [Scopes Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-scopes-object)
Expand Down
10 changes: 10 additions & 0 deletions packages/apidom-ns-openapi-2/src/elements/ResponsesDefinitions.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 ResponsesDefinitions extends ObjectElement {
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'responsesDefinitions';
}
}

export default ResponsesDefinitions;
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 @@ -40,6 +40,7 @@ export {
isXmlElement,
isDefinitionsElement,
isParametersDefinitionsElement,
isResponsesDefinitionsElement,
isSecurityDefinitionsElement,
isSecuritySchemeElement,
isScopesElement,
Expand All @@ -66,6 +67,7 @@ export {
XmlElement,
DefinitionsElement,
ParametersDefinitionsElement,
ResponsesDefinitionsElement,
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 @@ -16,6 +16,7 @@ import XmlElement from './elements/Xml';
import ReferenceElement from './elements/Reference';
import DefinitionsElement from './elements/Definitions';
import ParametersDefinitionsElement from './elements/ParametersDefinitions';
import ResponsesDefinitionsElement from './elements/ResponsesDefinitions';
import SecurityDefinitionsElement from './elements/SecurityDefinitions';
import SecuritySchemeElement from './elements/SecurityScheme';
import ScopesElement from './elements/Scopes';
Expand All @@ -41,6 +42,7 @@ const openApi2 = {
base.register('xml', XmlElement);
base.register('definitions', DefinitionsElement);
base.register('parametersDefinitions', ParametersDefinitionsElement);
base.register('responsesDefinitions', ResponsesDefinitionsElement);
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 @@ -16,6 +16,7 @@ import SchemaElement from './elements/Schema';
import XmlElement from './elements/Xml';
import DefinitionsElement from './elements/Definitions';
import ParametersDefinitionsElement from './elements/ParametersDefinitions';
import ResponsesDefinitionsElement from './elements/ResponsesDefinitions';
import SecurityDefinitionsElement from './elements/SecurityDefinitions';
import SecuritySchemeElement from './elements/SecurityScheme';
import SecurityRequirementElement from './elements/SecurityRequirement';
Expand Down Expand Up @@ -160,6 +161,16 @@ export const isXmlElement = createPredicate(
},
);

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

export const isSecurityDefinitionsElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is SecurityDefinitionsElement =>
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 @@ -14,6 +14,7 @@ import SchemaElement from '../elements/Schema';
import XmlElement from '../elements/Xml';
import DefinitionsElement from '../elements/Definitions';
import ParametersDefinitionsElement from '../elements/ParametersDefinitions';
import ResponsesDefinitionsElement from '../elements/ResponsesDefinitions';
import SecurityDefinitionsElement from '../elements/SecurityDefinitions';
import SecuritySchemeElement from '../elements/SecurityScheme';
import ScopesElement from '../elements/Scopes';
Expand Down Expand Up @@ -97,6 +98,13 @@ ParametersDefinitionsElement.refract = createRefractor([
'ParametersDefinitions',
'$visitor',
]);
ResponsesDefinitionsElement.refract = createRefractor([
'visitors',
'document',
'objects',
'ResponsesDefinitions',
'$visitor',
]);
SecurityDefinitionsElement.refract = createRefractor([
'visitors',
'document',
Expand Down Expand Up @@ -137,6 +145,7 @@ export {
XmlElement,
DefinitionsElement,
ParametersDefinitionsElement,
ResponsesDefinitionsElement,
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 @@ -22,6 +22,7 @@ import SchemaPropertiesVisitor from './visitors/open-api-2/schema/PropertiesVisi
import SchemaOrJSONReferenceVisitor from './visitors/open-api-2/schema/SchemaOrJSONReferenceVisitor';
import XmlVisitor from './visitors/open-api-2/xml';
import DefinitionsVisitor from './visitors/open-api-2/definitions';
import ResponsesDefinitionsVisitor from './visitors/open-api-2/responses-definitions';
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';
Expand Down Expand Up @@ -257,6 +258,9 @@ const specification = {
ParametersDefinitions: {
$visitor: ParametersDefinitionsVisitor,
},
ResponsesDefinitions: {
$visitor: ResponsesDefinitionsVisitor,
},
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 ResponsesDefinitionsElement from '../../../../elements/ResponsesDefinitions';
import MapVisitor from '../../generics/MapVisitor';
import FallbackVisitor from '../../FallbackVisitor';

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

export default ResponsesDefinitionsVisitor;
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 @@ -36,6 +36,7 @@ export const keyMap = {
XmlElement: ['content'],
DefinitionsElement: ['content'],
ParametersDefinitionsElement: ['content'],
ResponsesDefinitionsElement: ['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 @@ -17,6 +17,7 @@ import {
XmlElement,
DefinitionsElement,
ParametersDefinitionsElement,
ResponsesDefinitionsElement,
SecurityDefinitionsElement,
SecuritySchemeElement,
ScopesElement,
Expand All @@ -37,6 +38,7 @@ import {
isXmlElement,
isDefinitionsElement,
isParametersDefinitionsElement,
isResponsesDefinitionsElement,
isSecurityDefinitionsElement,
isSecuritySchemeElement,
isScopesElement,
Expand Down Expand Up @@ -940,6 +942,62 @@ describe('predicates', function () {
});
});

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

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

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

assert.isTrue(isResponsesDefinitionsElement(new ResponsesDefinitionsSubElement()));
});
});

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

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

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

assert.isTrue(isResponsesDefinitionsElement(responsesDefinitionsElementDuck));
assert.isFalse(isResponsesDefinitionsElement(responsesDefinitionsElementSwan));
});
});

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 ResponsesDefinitionsElement should refract to semantic ApiDOM tree 1`] = `
(ResponsesDefinitionsElement
(MemberElement
(StringElement)
(ResponseElement))
(MemberElement
(StringElement)
(ResponseElement)))
`;
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 { ResponsesDefinitionsElement } from '../../../../src';

describe('refractor', function () {
context('elements', function () {
context('ResponsesDefinitionsElement', function () {
specify('should refract to semantic ApiDOM tree', function () {
const responsesDefinitionsElement = ResponsesDefinitionsElement.refract({
response1: {},
response2: {},
});

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

0 comments on commit 51b15fe

Please sign in to comment.