Skip to content

Commit

Permalink
fix(ns-json-schema): retain meta & attributes during refracting
Browse files Browse the repository at this point in the history
This change is specific to cases when semantic ApiDOM is refractored
from generic ApiDOM.

Refs #3842
  • Loading branch information
char0n committed Feb 24, 2024
1 parent e960a76 commit e9da7e7
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Visitor {
to.attributes = deepmerge(to.attributes, from.attributes) as ObjectElement; // eslint-disable-line no-param-reassign
}
}
/* eslint-enable- class-methods-use-this, no-param-reassign */
/* eslint-enable class-methods-use-this, no-param-reassign */
}

export default Visitor;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Visitor {
to.attributes = deepmerge(to.attributes, from.attributes) as ObjectElement; // eslint-disable-line no-param-reassign
}
}
/* eslint-enable- class-methods-use-this, no-param-reassign */
/* eslint-enable class-methods-use-this, no-param-reassign */
}

export default Visitor;
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import stampit from 'stampit';
import { hasElementSourceMap } from '@swagger-api/apidom-core';
import { ObjectElement, hasElementSourceMap, deepmerge } from '@swagger-api/apidom-core';

const Visitor = stampit({
props: {
element: null,
},
// @ts-ignore
methods: {
/* eslint-disable class-methods-use-this, no-param-reassign */
copyMetaAndAttributes(from, to) {
// copy sourcemaps
if (hasElementSourceMap(from)) {
to.meta.set('sourceMap', from.meta.get('sourceMap'));
if (from.meta.length > 0 || to.meta.length > 0) {
to.meta = deepmerge(to.meta, from.meta) as ObjectElement;
if (hasElementSourceMap(from)) {
// avoid deep merging of source maps
to.meta.set('sourceMap', from.meta.get('sourceMap'));
}
}
if (from.attributes.length > 0 || from.meta.length > 0) {
to.attributes = deepmerge(to.attributes, from.attributes) as ObjectElement; // eslint-disable-line no-param-reassign
}
},
/* eslint-enable class-methods-use-this, no-param-reassign */
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,10 @@ exports[`refractor elements given fields of type JSONReference should refract to
(StringElement)
(StringElement)))))))
`;

exports[`refractor elements given generic ApiDOM element should refract to semantic ApiDOM tree 1`] = `
(JSONSchemaDraft4Element
(MemberElement
(StringElement)
(ObjectElement)))
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { sexprs } from '@swagger-api/apidom-core';
import { assert, expect } from 'chai';
import { ObjectElement, sexprs, toValue } from '@swagger-api/apidom-core';

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

Expand Down Expand Up @@ -84,5 +84,30 @@ describe('refractor', function () {
expect(sexprs(jsonSchemaElement)).toMatchSnapshot();
});
});

context('given generic ApiDOM element', function () {
let jsonSchemaElement: JSONSchemaElement;

beforeEach(function () {
const propertiesKeyword = new ObjectElement({}, { classes: ['example'] }, { attr: true });
jsonSchemaElement = JSONSchemaElement.refract(
new ObjectElement({ properties: propertiesKeyword }),
) as JSONSchemaElement;
});

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

specify('should deepmerge meta', function () {
assert.deepEqual(toValue(jsonSchemaElement.properties!.meta), {
classes: ['json-schema-properties', 'example'],
});
});

specify('should deepmerge attributes', function () {
assert.isTrue(jsonSchemaElement.properties!.attributes.get('attr').equals(true));
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -864,3 +864,10 @@ exports[`refractor elements given fields of type JSONReference should refract to
(StringElement)
(StringElement)))))))
`;

exports[`refractor elements given generic ApiDOM element should refract to semantic ApiDOM tree 1`] = `
(JSONSchemaDraft6Element
(MemberElement
(StringElement)
(ObjectElement)))
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { sexprs } from '@swagger-api/apidom-core';
import { assert, expect } from 'chai';
import { ObjectElement, sexprs, toValue } from '@swagger-api/apidom-core';

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

Expand Down Expand Up @@ -113,5 +113,30 @@ describe('refractor', function () {
expect(sexprs(jsonSchemaElement)).toMatchSnapshot();
});
});

context('given generic ApiDOM element', function () {
let jsonSchemaElement: JSONSchemaElement;

beforeEach(function () {
const propertiesKeyword = new ObjectElement({}, { classes: ['example'] }, { attr: true });
jsonSchemaElement = JSONSchemaElement.refract(
new ObjectElement({ properties: propertiesKeyword }),
) as JSONSchemaElement;
});

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

specify('should deepmerge meta', function () {
assert.deepEqual(toValue(jsonSchemaElement.properties!.meta), {
classes: ['json-schema-properties', 'example'],
});
});

specify('should deepmerge attributes', function () {
assert.isTrue(jsonSchemaElement.properties!.attributes.get('attr').equals(true));
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1005,3 +1005,10 @@ exports[`refractor elements given fields of type JSONReference should refract to
(StringElement)
(StringElement)))))))
`;

exports[`refractor elements given generic ApiDOM element should refract to semantic ApiDOM tree 1`] = `
(JSONSchemaDraft7Element
(MemberElement
(StringElement)
(ObjectElement)))
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { sexprs } from '@swagger-api/apidom-core';
import { assert, expect } from 'chai';
import { ObjectElement, sexprs, toValue } from '@swagger-api/apidom-core';

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

Expand Down Expand Up @@ -125,5 +125,30 @@ describe('refractor', function () {
expect(sexprs(jsonSchemaElement)).toMatchSnapshot();
});
});

context('given generic ApiDOM element', function () {
let jsonSchemaElement: JSONSchemaElement;

beforeEach(function () {
const propertiesKeyword = new ObjectElement({}, { classes: ['example'] }, { attr: true });
jsonSchemaElement = JSONSchemaElement.refract(
new ObjectElement({ properties: propertiesKeyword }),
) as JSONSchemaElement;
});

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

specify('should deepmerge meta', function () {
assert.deepEqual(toValue(jsonSchemaElement.properties!.meta), {
classes: ['json-schema-properties', 'example'],
});
});

specify('should deepmerge attributes', function () {
assert.isTrue(jsonSchemaElement.properties!.attributes.get('attr').equals(true));
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Visitor {
to.attributes = deepmerge(to.attributes, from.attributes) as ObjectElement; // eslint-disable-line no-param-reassign
}
}
/* eslint-enable- class-methods-use-this, no-param-reassign */
/* eslint-enable class-methods-use-this, no-param-reassign */
}

export default Visitor;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Visitor {
to.attributes = deepmerge(to.attributes, from.attributes) as ObjectElement; // eslint-disable-line no-param-reassign
}
}
/* eslint-enable- class-methods-use-this, no-param-reassign */
/* eslint-enable class-methods-use-this, no-param-reassign */
}

export default Visitor;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Visitor {
to.attributes = deepmerge(to.attributes, from.attributes) as ObjectElement; // eslint-disable-line no-param-reassign
}
}
/* eslint-enable- class-methods-use-this, no-param-reassign */
/* eslint-enable class-methods-use-this, no-param-reassign */
}

export default Visitor;

0 comments on commit e9da7e7

Please sign in to comment.