Skip to content

Commit

Permalink
fix(core): assign unique ID to elements only when missing (#3841)
Browse files Browse the repository at this point in the history
Refs #3840
  • Loading branch information
char0n authored Feb 21, 2024
1 parent 83c085e commit 89af0a9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/apidom-core/src/identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const IdentityManager: stampit.Stamp<IdentityManager> = stampit({
// use already assigned identity
if (
element.meta.hasKey('id') &&
isStringElement(element.meta.id) &&
!element.meta.id.equals('')
isStringElement(element.meta.get('id')) &&
!element.meta.get('id').equals('')
) {
return element.id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Element, StringElement } from 'minim';
import { Element } from 'minim';

import { IdentityManager } from '../../identity';

Expand All @@ -17,7 +17,7 @@ const plugin =
},
visitor: {
enter<T extends Element>(element: T) {
element.id = new StringElement(identityManager!.generateId()); // eslint-disable-line no-param-reassign
element.id = identityManager!.identify(element); // eslint-disable-line no-param-reassign
},
},
post() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Element, StringElement } from 'minim';
import { Element } from 'minim';

import { IdentityManager } from '../../identity';
import { isPrimitiveElement } from '../../predicates';
Expand All @@ -23,7 +23,7 @@ const plugin =
visitor: {
enter<T extends Element>(element: T) {
if (!predicates.isPrimitiveElement(element)) {
(element as Element).id = new StringElement(identityManager!.generateId()); // eslint-disable-line no-param-reassign
(element as Element).id = identityManager!.identify(element); // eslint-disable-line no-param-reassign
}
},
},
Expand Down
17 changes: 16 additions & 1 deletion packages/apidom-core/test/refractor/plugins/element-identity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { assert } from 'chai';

import { ObjectElement, refractorPluginElementIdentity } from '../../../src';
import {
ObjectElement,
StringElement,
refractorPluginElementIdentity,
dispatchRefractorPlugins,
} from '../../../src';

describe('refractor', function () {
context('plugins', function () {
Expand Down Expand Up @@ -31,6 +36,16 @@ describe('refractor', function () {
assert.lengthOf(objectElement.getMember('a').value.id, length);
},
);

specify('should not add unique ID when already present', function () {
const objectElement = new ObjectElement({ id: '123' });
objectElement.id = new StringElement('unique-id');
const newObjectElement = dispatchRefractorPlugins(objectElement, [
refractorPluginElementIdentity(),
]);

assert.isTrue(newObjectElement.id.equals('unique-id'));
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { assert } from 'chai';
import { InfoElement } from '@swagger-api/apidom-ns-openapi-3-1';

import { ObjectElement, refractorPluginSemanticElementIdentity } from '../../../src';
import {
ObjectElement,
StringElement,
dispatchRefractorPlugins,
refractorPluginSemanticElementIdentity,
} from '../../../src';

describe('refractor', function () {
context('plugins', function () {
Expand Down Expand Up @@ -57,6 +62,20 @@ describe('refractor', function () {
assert.lengthOf(objectElement.getMember('info').value.get('contact').id, length);
},
);

specify('should not add unique ID when already present', function () {
const infoElement = InfoElement.refract({
title: 'title',
summary: 'summary',
contact: { name: 'John Doe' },
});
infoElement.id = new StringElement('unique-id');
const newInfoElement = dispatchRefractorPlugins(infoElement, [
refractorPluginSemanticElementIdentity(),
]);

assert.isTrue(newInfoElement.id.equals('unique-id'));
});
});
});
});

0 comments on commit 89af0a9

Please sign in to comment.