From ae5e27d67974e4fd2a367bc8a70815ff52beb1d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Thu, 12 Oct 2023 15:36:26 +0200 Subject: [PATCH] feat(ns-openapi-2): add support for Parameter Object (#3257) Refs #3097 --- packages/apidom-ns-openapi-2/README.md | 2 +- .../src/elements/Parameter.ts | 320 ++++++++++++++++++ .../src/elements/Schema.ts | 12 + 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 | 11 + .../src/refractor/registration.ts | 9 + .../src/refractor/specification.ts | 36 ++ .../visitors/open-api-2/parameter/index.ts | 18 + .../visitors/open-api-2/schema/index.ts | 18 + .../src/traversal/visitor.ts | 1 + .../apidom-ns-openapi-2/test/predicates.ts | 58 ++++ .../Parameter/__snapshots__/index.ts.snap | 26 ++ .../refractor/elements/Parameter/index.ts | 38 +++ 14 files changed, 552 insertions(+), 1 deletion(-) create mode 100644 packages/apidom-ns-openapi-2/src/elements/Parameter.ts create mode 100644 packages/apidom-ns-openapi-2/src/elements/Schema.ts create mode 100644 packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/parameter/index.ts create mode 100644 packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/schema/index.ts create mode 100644 packages/apidom-ns-openapi-2/test/refractor/elements/Parameter/__snapshots__/index.ts.snap create mode 100644 packages/apidom-ns-openapi-2/test/refractor/elements/Parameter/index.ts diff --git a/packages/apidom-ns-openapi-2/README.md b/packages/apidom-ns-openapi-2/README.md index b1e73be98..544cbef06 100644 --- a/packages/apidom-ns-openapi-2/README.md +++ b/packages/apidom-ns-openapi-2/README.md @@ -192,7 +192,7 @@ Only fully implemented specification objects should be checked here. - [ ] [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) - [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) +- [x] [Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-parameter-object) - [x] [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) - [ ] [Response Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#user-content-response-object) diff --git a/packages/apidom-ns-openapi-2/src/elements/Parameter.ts b/packages/apidom-ns-openapi-2/src/elements/Parameter.ts new file mode 100644 index 000000000..1e352ca20 --- /dev/null +++ b/packages/apidom-ns-openapi-2/src/elements/Parameter.ts @@ -0,0 +1,320 @@ +import { + StringElement, + BooleanElement, + Attributes, + Meta, + NumberElement, + ArrayElement, + ObjectElement, +} from '@swagger-api/apidom-core'; +import { UnsupportedOperationError } from '@swagger-api/apidom-error'; +import { + JSONReferenceElement, + JSONSchemaElement, + MediaElement, +} from '@swagger-api/apidom-ns-json-schema-draft-4'; + +import SchemaElement from './Schema'; + +/* eslint-disable class-methods-use-this */ +class Parameter extends JSONSchemaElement { + constructor(content?: Record, meta?: Meta, attributes?: Attributes) { + super(content, meta, attributes); + this.element = 'parameter'; + this.classes.push('json-schema-draft-4'); + } + + /** + * Core vocabulary + * + * URI: https://tools.ietf.org/html/draft-wright-json-schema-00 + */ + + get idProp(): StringElement | undefined { + throw new UnsupportedOperationError('idProp getter in Parameter class is not not supported.'); + } + + set idProp(idProps: StringElement | undefined) { + throw new UnsupportedOperationError('idProp setter in Parameter class is not not supported.'); + } + + get $schema(): StringElement | undefined { + throw new UnsupportedOperationError('$schema getter in Parameter class is not not supported.'); + } + + set $schema($schema: StringElement | undefined) { + throw new UnsupportedOperationError('$schema setter in Parameter class is not not supported.'); + } + + /** + * Validation keywords for arrays + */ + + get additionalItems(): this | JSONReferenceElement | BooleanElement | undefined { + throw new UnsupportedOperationError( + 'additionalItems getter in Parameter class is not not supported.', + ); + } + + set additionalItems(additionalItems: this | JSONReferenceElement | BooleanElement | undefined) { + throw new UnsupportedOperationError( + 'additionalItems setter in Parameter class is not not supported.', + ); + } + + get items(): this | undefined { + return this.get('items'); + } + + set items(items: this | undefined) { + this.set('items', items); + } + + /** + * Validation keywords for objects + */ + + get maxProperties(): NumberElement | undefined { + throw new UnsupportedOperationError( + 'maxProperties getter in Parameter class is not not supported.', + ); + } + + set maxProperties(maxProperties: NumberElement | undefined) { + throw new UnsupportedOperationError( + 'maxProperties setter in Parameter class is not not supported.', + ); + } + + get minProperties(): NumberElement | undefined { + throw new UnsupportedOperationError( + 'minProperties getter in Parameter class is not not supported.', + ); + } + + set minProperties(minProperties: NumberElement | undefined) { + throw new UnsupportedOperationError( + 'minProperties setter in Parameter class is not not supported.', + ); + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + get required(): BooleanElement | undefined | any { + return this.get('required'); + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + set required(required: BooleanElement | undefined | any) { + this.set('required', required); + } + + get properties(): ObjectElement | undefined { + throw new UnsupportedOperationError( + 'properties getter in Parameter class is not not supported.', + ); + } + + set properties(properties: ObjectElement | undefined) { + throw new UnsupportedOperationError( + 'properties setter in Parameter class is not not supported.', + ); + } + + get additionalProperties(): this | JSONReferenceElement | BooleanElement | undefined { + throw new UnsupportedOperationError( + 'additionalProperties getter in Parameter class is not not supported.', + ); + } + + set additionalProperties( + additionalProperties: this | JSONReferenceElement | BooleanElement | undefined, + ) { + throw new UnsupportedOperationError( + 'additionalProperties setter in Parameter class is not not supported.', + ); + } + + get patternProperties(): ObjectElement | undefined { + throw new UnsupportedOperationError( + 'patternProperties getter in Parameter class is not not supported.', + ); + } + + set patternProperties(patternProperties: ObjectElement | undefined) { + throw new UnsupportedOperationError( + 'patternProperties setter in Parameter class is not not supported.', + ); + } + + get dependencies(): ObjectElement | undefined { + throw new UnsupportedOperationError( + 'dependencies getter in Parameter class is not not supported.', + ); + } + + set dependencies(dependencies: ObjectElement | undefined) { + throw new UnsupportedOperationError( + 'dependencies setter in Parameter class is not not supported.', + ); + } + + /** + * Validation keywords for any instance type + */ + + get type(): StringElement | undefined { + return this.get('type'); + } + + set type(type: StringElement | undefined) { + this.set('type', type); + } + + get allOf(): ArrayElement | undefined { + throw new UnsupportedOperationError('allOf getter in Parameter class is not not supported.'); + } + + set allOf(allOf: ArrayElement | undefined) { + throw new UnsupportedOperationError('allOf setter in Parameter class is not not supported.'); + } + + get anyOf(): ArrayElement | undefined { + throw new UnsupportedOperationError('anyOf getter in Parameter class is not not supported.'); + } + + set anyOf(anyOf: ArrayElement | undefined) { + throw new UnsupportedOperationError('anyOf setter in Parameter class is not not supported.'); + } + + get oneOf(): ArrayElement | undefined { + throw new UnsupportedOperationError('oneOf getter in Parameter class is not not supported.'); + } + + set oneOf(oneOf: ArrayElement | undefined) { + throw new UnsupportedOperationError('oneOf setter in Parameter class is not not supported.'); + } + + get not(): this | JSONReferenceElement | undefined { + throw new UnsupportedOperationError('not getter in Parameter class is not not supported.'); + } + + set not(not: this | JSONReferenceElement | undefined) { + throw new UnsupportedOperationError('not setter in Parameter class is not not supported.'); + } + + get definitions(): ObjectElement | undefined { + throw new UnsupportedOperationError( + 'definitions getter in Parameter class is not not supported.', + ); + } + + set definitions(definitions: ObjectElement | undefined) { + throw new UnsupportedOperationError( + 'definitions setter in Parameter class is not not supported.', + ); + } + + /** + * Metadata keywords + * + * URI: https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00#section-6 + */ + + get title(): StringElement | undefined { + throw new UnsupportedOperationError('title getter in Parameter class is not not supported.'); + } + + set title(title: StringElement | undefined) { + throw new UnsupportedOperationError('title setter in Parameter class is not not supported.'); + } + + get description(): StringElement | undefined { + return this.get('description'); + } + + set description(description: StringElement | undefined) { + this.set('description,', description); + } + + /** + * Semantic validation with "format" + * + * URI: https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00#section-7 + */ + + get format(): StringElement | undefined { + return this.get('format'); + } + + set format(format: StringElement | undefined) { + this.set('format', format); + } + + /** + * JSON Hyper-Schema + * + * URI: https://datatracker.ietf.org/doc/html/draft-wright-json-schema-hyperschema-00 + */ + + get base(): StringElement | undefined { + throw new UnsupportedOperationError('base getter in Parameter class is not not supported.'); + } + + set base(base: StringElement | undefined) { + throw new UnsupportedOperationError('base setter in Parameter class is not not supported.'); + } + + get links(): ArrayElement | undefined { + throw new UnsupportedOperationError('links getter in Parameter class is not not supported.'); + } + + set links(links: ArrayElement | undefined) { + throw new UnsupportedOperationError('links setter in Parameter class is not not supported.'); + } + + get media(): MediaElement | undefined { + throw new UnsupportedOperationError('media getter in Parameter class is not not supported.'); + } + + set media(media: MediaElement | undefined) { + throw new UnsupportedOperationError('media setter in Parameter class is not not supported.'); + } + + get readOnly(): BooleanElement | undefined { + throw new UnsupportedOperationError('readOnly getter in Parameter class is not not supported.'); + } + + set readOnly(readOnly: BooleanElement | undefined) { + throw new UnsupportedOperationError('readOnly setter in Parameter class is not not supported.'); + } + + /** + * OpenAPI vocabulary + */ + get name(): StringElement | undefined { + return this.get('name'); + } + + set name(name: StringElement | undefined) { + this.set('name', name); + } + + get in(): StringElement | undefined { + return this.get('in'); + } + + set in(val: StringElement | undefined) { + this.set('in', val); + } + + get schema(): SchemaElement | undefined { + return this.get('schema'); + } + + set schema(schema: SchemaElement | undefined) { + this.set('schema', schema); + } +} +/* eslint-enable class-methods-use-this */ + +export default Parameter; diff --git a/packages/apidom-ns-openapi-2/src/elements/Schema.ts b/packages/apidom-ns-openapi-2/src/elements/Schema.ts new file mode 100644 index 000000000..bccefc031 --- /dev/null +++ b/packages/apidom-ns-openapi-2/src/elements/Schema.ts @@ -0,0 +1,12 @@ +import { Attributes, Meta } from '@swagger-api/apidom-core'; +import { JSONSchemaElement } from '@swagger-api/apidom-ns-json-schema-draft-4'; + +class Schema extends JSONSchemaElement { + constructor(content?: Record, meta?: Meta, attributes?: Attributes) { + super(content, meta, attributes); + this.element = 'schema'; + this.classes.push('json-schema-draft-4'); + } +} + +export default Schema; diff --git a/packages/apidom-ns-openapi-2/src/index.ts b/packages/apidom-ns-openapi-2/src/index.ts index 8ecb3d630..707b832f9 100644 --- a/packages/apidom-ns-openapi-2/src/index.ts +++ b/packages/apidom-ns-openapi-2/src/index.ts @@ -24,6 +24,7 @@ export { isLicenseElement, isContactElement, isExternalDocumentationElement, + isParameterElement, isItemsElement, isHeadersElement, isExampleElement, @@ -44,6 +45,7 @@ export { LicenseElement, ContactElement, ExternalDocumentationElement, + ParameterElement, ItemsElement, HeadersElement, ExampleElement, diff --git a/packages/apidom-ns-openapi-2/src/namespace.ts b/packages/apidom-ns-openapi-2/src/namespace.ts index bf3167511..a164c1f5f 100644 --- a/packages/apidom-ns-openapi-2/src/namespace.ts +++ b/packages/apidom-ns-openapi-2/src/namespace.ts @@ -4,6 +4,7 @@ import InfoElement from './elements/Info'; import LicenseElement from './elements/License'; import ContactElement from './elements/Contact'; import ExternalDocumentation from './elements/ExternalDocumentation'; +import ParameterElement from './elements/Parameter'; import ItemsElement from './elements/Items'; import ExampleElement from './elements/Example'; import HeadersElement from './elements/Headers'; @@ -23,6 +24,7 @@ const openApi2 = { base.register('license', LicenseElement); base.register('contact', ContactElement); base.register('externalDocumentation', ExternalDocumentation); + base.register('parameter', ParameterElement); base.register('items', ItemsElement); base.register('headers', HeadersElement); base.register('example', ExampleElement); diff --git a/packages/apidom-ns-openapi-2/src/predicates.ts b/packages/apidom-ns-openapi-2/src/predicates.ts index dca95a9ed..b5c61a317 100644 --- a/packages/apidom-ns-openapi-2/src/predicates.ts +++ b/packages/apidom-ns-openapi-2/src/predicates.ts @@ -4,6 +4,7 @@ import InfoElement from './elements/Info'; import LicenseElement from './elements/License'; import ContactElement from './elements/Contact'; import ExternalDocumentation from './elements/ExternalDocumentation'; +import ParameterElement from './elements/Parameter'; import ItemsElement from './elements/Items'; import ExampleElement from './elements/Example'; import HeadersElement from './elements/Headers'; @@ -54,6 +55,16 @@ export const isExternalDocumentationElement = createPredicate( }, ); +export const isParameterElement = createPredicate( + ({ hasBasicElementProps, isElementType, primitiveEq }) => { + return (element: unknown) => + element instanceof ParameterElement || + (hasBasicElementProps(element) && + isElementType('parameter', element) && + primitiveEq('object', element)); + }, +); + export const isItemsElement = createPredicate( ({ hasBasicElementProps, isElementType, primitiveEq }) => { return (element: unknown) => diff --git a/packages/apidom-ns-openapi-2/src/refractor/registration.ts b/packages/apidom-ns-openapi-2/src/refractor/registration.ts index 4ac9f9605..ca738c0c2 100644 --- a/packages/apidom-ns-openapi-2/src/refractor/registration.ts +++ b/packages/apidom-ns-openapi-2/src/refractor/registration.ts @@ -2,6 +2,7 @@ import InfoElement from '../elements/Info'; import LicenseElement from '../elements/License'; import ContactElement from '../elements/Contact'; import ExternalDocumentationElement from '../elements/ExternalDocumentation'; +import ParameterElement from '../elements/Parameter'; import ItemsElement from '../elements/Items'; import HeadersElement from '../elements/Headers'; import ExampleElement from '../elements/Example'; @@ -37,6 +38,13 @@ ExternalDocumentationElement.refract = createRefractor([ 'ExternalDocumentation', '$visitor', ]); +ParameterElement.refract = createRefractor([ + 'visitors', + 'document', + 'objects', + 'Parameter', + '$visitor', +]); ItemsElement.refract = createRefractor(['visitors', 'document', 'objects', 'Items', '$visitor']); HeadersElement.refract = createRefractor([ 'visitors', @@ -83,6 +91,7 @@ export { LicenseElement, ContactElement, ExternalDocumentationElement, + ParameterElement, ItemsElement, HeadersElement, ExampleElement, diff --git a/packages/apidom-ns-openapi-2/src/refractor/specification.ts b/packages/apidom-ns-openapi-2/src/refractor/specification.ts index cbdcb8dd1..31a83943b 100644 --- a/packages/apidom-ns-openapi-2/src/refractor/specification.ts +++ b/packages/apidom-ns-openapi-2/src/refractor/specification.ts @@ -6,11 +6,13 @@ import InfoVersionVisitor from './visitors/open-api-2/info/VersionVisitor'; import LicenseVisitor from './visitors/open-api-2/license'; import ContactVisitor from './visitors/open-api-2/contact'; import ExternalDocumentationElement from './visitors/open-api-2/external-documentation'; +import ParameterVisitor from './visitors/open-api-2/parameter'; import ItemsVisitor from './visitors/open-api-2/items'; import HeadersVisitor from './visitors/open-api-2/headers'; import ExampleVisitor from './visitors/open-api-2/example'; import HeaderVisitor from './visitors/open-api-2/header'; import TagVisitor from './visitors/open-api-2/tag'; +import SchemaVisitor from './visitors/open-api-2/schema'; 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'; @@ -72,6 +74,37 @@ const specification = { url: FallbackVisitor, }, }, + Parameter: { + $visitor: ParameterVisitor, + fixedFields: { + name: FallbackVisitor, + in: FallbackVisitor, + description: jsonSchemaFixedFields.description, + required: jsonSchemaFixedFields.required, + schema: { + $ref: '#/visitors/document/objects/Schema', + }, + type: jsonSchemaFixedFields.type, + format: jsonSchemaFixedFields.format, + items: { + $ref: '#/visitors/document/objects/Items', + }, + collectionFormat: FallbackVisitor, + default: jsonSchemaFixedFields.default, + maximum: jsonSchemaFixedFields.maximum, + exclusiveMaximum: jsonSchemaFixedFields.exclusiveMaximum, + minimum: jsonSchemaFixedFields.minimum, + exclusiveMinimum: jsonSchemaFixedFields.exclusiveMinimum, + maxLength: jsonSchemaFixedFields.maxLength, + minLength: jsonSchemaFixedFields.minLength, + pattern: jsonSchemaFixedFields.pattern, + maxItems: jsonSchemaFixedFields.maxItems, + minItems: jsonSchemaFixedFields.minItems, + uniqueItems: jsonSchemaFixedFields.uniqueItems, + enum: jsonSchemaFixedFields.enum, + multipleOf: jsonSchemaFixedFields.multipleOf, + }, + }, Items: { $visitor: ItemsVisitor, fixedFields: { @@ -137,6 +170,9 @@ const specification = { }, }, }, + Schema: { + $visitor: SchemaVisitor, + }, XML: { $visitor: XmlVisitor, fixedFields: { diff --git a/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/parameter/index.ts b/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/parameter/index.ts new file mode 100644 index 000000000..6bbeb6fde --- /dev/null +++ b/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/parameter/index.ts @@ -0,0 +1,18 @@ +import stampit from 'stampit'; +import { always } from 'ramda'; + +import ParameterElement from '../../../../elements/Parameter'; +import FallbackVisitor from '../../FallbackVisitor'; +import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor'; + +const ParameterVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, { + props: { + specPath: always(['document', 'objects', 'Parameter']), + canSupportSpecificationExtensions: true, + }, + init() { + this.element = new ParameterElement(); + }, +}); + +export default ParameterVisitor; diff --git a/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/schema/index.ts b/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/schema/index.ts new file mode 100644 index 000000000..4f3e37439 --- /dev/null +++ b/packages/apidom-ns-openapi-2/src/refractor/visitors/open-api-2/schema/index.ts @@ -0,0 +1,18 @@ +import stampit from 'stampit'; +import { always } from 'ramda'; + +import SchemaElement from '../../../../elements/Schema'; +import FallbackVisitor from '../../FallbackVisitor'; +import FixedFieldsVisitor from '../../generics/FixedFieldsVisitor'; + +const ParameterVisitor = stampit(FixedFieldsVisitor, FallbackVisitor, { + props: { + specPath: always(['document', 'objects', 'Schema']), + canSupportSpecificationExtensions: true, + }, + init() { + this.element = new SchemaElement(); + }, +}); + +export default ParameterVisitor; diff --git a/packages/apidom-ns-openapi-2/src/traversal/visitor.ts b/packages/apidom-ns-openapi-2/src/traversal/visitor.ts index 669e42cf7..792f9ebe8 100644 --- a/packages/apidom-ns-openapi-2/src/traversal/visitor.ts +++ b/packages/apidom-ns-openapi-2/src/traversal/visitor.ts @@ -23,6 +23,7 @@ export const keyMap = { LicenseElement: ['content'], ContactElement: ['content'], ExternalDocumentationElement: ['content'], + ParameterElement: ['content'], ItemsElement: ['content'], ExampleElement: ['content'], HeadersElement: ['content'], diff --git a/packages/apidom-ns-openapi-2/test/predicates.ts b/packages/apidom-ns-openapi-2/test/predicates.ts index 78ef1d8fa..c34bde280 100644 --- a/packages/apidom-ns-openapi-2/test/predicates.ts +++ b/packages/apidom-ns-openapi-2/test/predicates.ts @@ -5,6 +5,7 @@ import { LicenseElement, ContactElement, ExternalDocumentationElement, + ParameterElement, ItemsElement, HeadersElement, ExampleElement, @@ -19,6 +20,7 @@ import { isLicenseElement, isContactElement, isExternalDocumentationElement, + isParameterElement, isItemsElement, isHeadersElement, isExampleElement, @@ -256,6 +258,62 @@ describe('predicates', function () { }); }); + context('isParameterElement', function () { + context('given ParameterElement instance value', function () { + specify('should return true', function () { + const element = new ParameterElement(); + + assert.isTrue(isParameterElement(element)); + }); + }); + + context('given subtype instance value', function () { + specify('should return true', function () { + class ParameterSubElement extends ParameterElement {} + + assert.isTrue(isParameterElement(new ParameterSubElement())); + }); + }); + + context('given non ParameterSubElement instance value', function () { + specify('should return false', function () { + assert.isFalse(isParameterElement(1)); + assert.isFalse(isParameterElement(null)); + assert.isFalse(isParameterElement(undefined)); + assert.isFalse(isParameterElement({})); + assert.isFalse(isParameterElement([])); + assert.isFalse(isParameterElement('string')); + }); + }); + + specify('should support duck-typing', function () { + const parameterElementDuck = { + _storedElement: 'parameter', + _content: [], + primitive() { + return 'object'; + }, + get element() { + return this._storedElement; + }, + }; + + const parameterElementSwan = { + _storedElement: undefined, + _content: undefined, + primitive() { + return 'swan'; + }, + get length() { + return 0; + }, + }; + + assert.isTrue(isParameterElement(parameterElementDuck)); + assert.isFalse(isParameterElement(parameterElementSwan)); + }); + }); + context('isItemsElement', function () { context('given ItemsElement instance value', function () { specify('should return true', function () { diff --git a/packages/apidom-ns-openapi-2/test/refractor/elements/Parameter/__snapshots__/index.ts.snap b/packages/apidom-ns-openapi-2/test/refractor/elements/Parameter/__snapshots__/index.ts.snap new file mode 100644 index 000000000..a059de876 --- /dev/null +++ b/packages/apidom-ns-openapi-2/test/refractor/elements/Parameter/__snapshots__/index.ts.snap @@ -0,0 +1,26 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`refractor elements ParameterElement should refract to semantic ApiDOM tree 1`] = ` +(ParameterElement + (MemberElement + (StringElement) + (StringElement)) + (MemberElement + (StringElement) + (StringElement)) + (MemberElement + (StringElement) + (StringElement)) + (MemberElement + (StringElement) + (StringElement)) + (MemberElement + (StringElement) + (BooleanElement)) + (MemberElement + (StringElement) + (SchemaElement)) + (MemberElement + (StringElement) + (ItemsElement))) +`; diff --git a/packages/apidom-ns-openapi-2/test/refractor/elements/Parameter/index.ts b/packages/apidom-ns-openapi-2/test/refractor/elements/Parameter/index.ts new file mode 100644 index 000000000..4f24bd0a8 --- /dev/null +++ b/packages/apidom-ns-openapi-2/test/refractor/elements/Parameter/index.ts @@ -0,0 +1,38 @@ +import { assert, expect } from 'chai'; +import { includesClasses, sexprs } from '@swagger-api/apidom-core'; + +import { ParameterElement } from '../../../../src'; + +describe('refractor', function () { + context('elements', function () { + context('ParameterElement', function () { + specify('should refract to semantic ApiDOM tree', function () { + const parameterElement = ParameterElement.refract({ + type: 'array', + name: 'user', + in: 'body', + description: 'user to add to the system', + required: true, + schema: {}, + items: {}, + }); + + expect(sexprs(parameterElement)).toMatchSnapshot(); + }); + + specify('should support specification extensions', function () { + const parameterElement = ParameterElement.refract({ + type: 'array', + 'x-extension': 'extension', + }) as ParameterElement; + + assert.isFalse( + includesClasses(['specification-extension'], parameterElement.getMember('type')), + ); + assert.isTrue( + includesClasses(['specification-extension'], parameterElement.getMember('x-extension')), + ); + }); + }); + }); +});