Skip to content

Commit

Permalink
fix(ns-openapi-3-0): provide missing reference metadata for Schema Ob…
Browse files Browse the repository at this point in the history
…ject definitions keyword (#2988)

Refs #2980
  • Loading branch information
char0n committed Jul 25, 2023
1 parent d7cc458 commit 5127577
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/apidom-ns-openapi-3-0/src/refractor/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import SchemaVisitor from './visitors/open-api-3-0/schema';
import SchemaAllOfVisitor from './visitors/open-api-3-0/schema/AllOfVisitor';
import SchemaAnyOfVisitor from './visitors/open-api-3-0/schema/AnyOfVisitor';
import SchemaOneOfVisitor from './visitors/open-api-3-0/schema/OneOfVisitor';
import SchemaDefinitionsVisitor from './visitors/open-api-3-0/schema/DefinitionsVisitor';
import SchemaDependenciesVisitor from './visitors/open-api-3-0/schema/DependenciesVisitor';
import SchemaItemsVisitor from './visitors/open-api-3-0/schema/ItemsVisitor';
import SchemaPropertiesVisitor from './visitors/open-api-3-0/schema/PropertiesVisitor';
Expand Down Expand Up @@ -187,6 +188,7 @@ const SchemaSpecification = {
allOf: SchemaAllOfVisitor,
anyOf: SchemaAnyOfVisitor,
oneOf: SchemaOneOfVisitor,
definitions: SchemaDefinitionsVisitor,
// validation keywords for arrays
items: SchemaItemsVisitor,
// Validation keywords for objects
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import stampit from 'stampit';
import { ObjectElement } from '@swagger-api/apidom-core';
import { specificationObj as JSONSchemaDraft4Specification } from '@swagger-api/apidom-ns-json-schema-draft-4';

import ReferenceElement from '../../../../elements/Reference';
import { isReferenceElement } from '../../../../predicates';

const { definitions: JSONSchemaDefinitionsVisitor } =
JSONSchemaDraft4Specification.visitors.document.objects.JSONSchema.fixedFields;

const DefinitionsVisitor = stampit(JSONSchemaDefinitionsVisitor, {
methods: {
ObjectElement(objectElement: ObjectElement) {
// @ts-ignore
const result = JSONSchemaDefinitionsVisitor.compose.methods.ObjectElement.call(
this,
objectElement,
);

this.element.filter(isReferenceElement).forEach((referenceElement: ReferenceElement) => {
referenceElement.setMetaProperty('referenced-element', 'schema');
});

return result;
},
},
});

export default DefinitionsVisitor;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ exports[`refractor elements SchemaElement given anyOf keyword with reference sho
(StringElement))))))
`;

exports[`refractor elements SchemaElement given definitions keyword with reference should refract to semantic ApiDOM tree 1`] = `
(SchemaElement
(MemberElement
(StringElement)
(ObjectElement
(MemberElement
(StringElement)
(ReferenceElement
(MemberElement
(StringElement)
(StringElement)))))))
`;

exports[`refractor elements SchemaElement given dependencies keyword with reference should refract to semantic ApiDOM tree 1`] = `
(SchemaElement
(MemberElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ describe('refractor', function () {
});
});

context('given definitions keyword with reference', function () {
const schemaElement = SchemaElement.refract({
definitions: { def1: { $ref: '#/path/to/schema' } },
}) as SchemaElement;

specify('should refract to semantic ApiDOM tree', function () {
expect(sexprs(schemaElement)).toMatchSnapshot();
});

specify('should contain referenced-element meta', function () {
const referenceElement = schemaElement.definitions?.get('def1');
const referencedElementMeta = referenceElement?.getMetaProperty('referenced-element');

assert.strictEqual(referencedElementMeta.toValue(), 'schema');
});
});

context('given dependencies keyword with reference', function () {
const schemaElement = SchemaElement.refract({
dependencies: { dep1: { $ref: '#/path/to/schema' } },
Expand Down

0 comments on commit 5127577

Please sign in to comment.