Skip to content

Commit

Permalink
fix: immutability
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Nov 7, 2023
1 parent a49ea11 commit cd191b3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import stampit from 'stampit';
import { propEq } from 'ramda';
import {
cloneDeep,
cloneShallow,
Element,
isElement,
isMemberElement,
isPrimitiveElement,
isStringElement,
IdentityManager,
cloneDeep,
cloneShallow,
visit,
toValue,
refractorPluginElementIdentity,
Element,
} from '@swagger-api/apidom-core';
import { ApiDOMError } from '@swagger-api/apidom-error';
import { evaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
Expand All @@ -36,23 +36,20 @@ import Reference from '../../../Reference';
// @ts-ignore
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];

// initialize element identity manager
const identityManager = IdentityManager();

/**
* Predicate for detecting if element was created by merging referencing
* element with particular element identity with a referenced element.
*/
const wasReferencedBy =
<T extends Element, U extends Element>(referencingElement: T) =>
(element: U) => {
// @ts-ignore
return (
element.meta.hasKey('ref-referencing-element-id') &&
element.meta.get('ref-referencing-element-id').equals(toValue(referencingElement.id))
);
};

// initialize element identity plugin
const elementIdentity = refractorPluginElementIdentity()();
elementIdentity.pre();
(element: U) =>
element.meta.hasKey('ref-referencing-element-id') &&
element.meta
.get('ref-referencing-element-id')
.equals(toValue(identityManager.identify(referencingElement)));

const AsyncApi2DereferenceVisitor = stampit({
props: {
Expand Down Expand Up @@ -206,7 +203,7 @@ const AsyncApi2DereferenceVisitor = stampit({
// annotate fragment with info about referencing element
booleanJsonSchemaElement.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(referencingElement.id),
cloneDeep(identityManager.identify(referencingElement)),
);

return booleanJsonSchemaElement;
Expand All @@ -221,15 +218,15 @@ const AsyncApi2DereferenceVisitor = stampit({
});
// annotate fragment with info about origin
copy.setMetaProperty('ref-origin', reference.uri);
// annotate fragment with info about referencing element
copy.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(identityManager.identify(referencingElement)),
);

return copy;
};

// assigning element identity if not already assigned
if (referencingElement.id.equals('')) {
elementIdentity.visitor.enter(referencingElement);
}

// attempting to create cycle
if (
ancestorsLineage.includes(referencingElement) ||
Expand Down Expand Up @@ -345,15 +342,15 @@ const AsyncApi2DereferenceVisitor = stampit({
});
// annotate referenced with info about origin
mergedElement.setMetaProperty('ref-origin', reference.uri);
// annotate fragment with info about referencing element
mergedElement.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(identityManager.identify(referencingElement)),
);

return mergedElement;
};

// assigning element identity if not already assigned
if (referencingElement.id.equals('')) {
elementIdentity.visitor.enter(referencingElement);
}

// attempting to create cycle
if (
ancestorsLineage.includes(referencingElement) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import stampit from 'stampit';
import { propEq } from 'ramda';
import { isUndefined } from 'ramda-adjunct';
import {
Element,
isPrimitiveElement,
isStringElement,
isMemberElement,
isElement,
IdentityManager,
visit,
find,
isElement,
cloneShallow,
cloneDeep,
toValue,
isMemberElement,
refractorPluginElementIdentity,
Element,
} from '@swagger-api/apidom-core';
import { ApiDOMError } from '@swagger-api/apidom-error';
import { evaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
Expand Down Expand Up @@ -42,23 +42,20 @@ import Reference from '../../../Reference';
// @ts-ignore
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];

// initialize element identity manager
const identityManager = IdentityManager();

/**
* Predicate for detecting if element was created by merging referencing
* element with particular element identity with a referenced element.
*/
const wasReferencedBy =
<T extends Element, U extends Element>(referencingElement: T) =>
(element: U) => {
// @ts-ignore
return (
element.meta.hasKey('ref-referencing-element-id') &&
element.meta.get('ref-referencing-element-id').equals(toValue(referencingElement.id))
);
};

// initialize element identity plugin
const elementIdentity = refractorPluginElementIdentity()();
elementIdentity.pre();
(element: U) =>
element.meta.hasKey('ref-referencing-element-id') &&
element.meta
.get('ref-referencing-element-id')
.equals(toValue(identityManager.identify(referencingElement)));

// eslint-disable-next-line @typescript-eslint/naming-convention
const OpenApi3_0DereferenceVisitor = stampit({
Expand Down Expand Up @@ -211,15 +208,15 @@ const OpenApi3_0DereferenceVisitor = stampit({
});
// annotate fragment with info about origin
copy.setMetaProperty('ref-origin', reference.uri);
// annotate fragment with info about referencing element
copy.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(identityManager.identify(referencingElement)),
);

return copy;
};

// assigning element identity if not already assigned
if (referencingElement.id.equals('')) {
elementIdentity.visitor.enter(referencingElement);
}

// attempting to create cycle
if (
ancestorsLineage.includes(referencingElement) ||
Expand Down Expand Up @@ -335,15 +332,15 @@ const OpenApi3_0DereferenceVisitor = stampit({
});
// annotate referenced element with info about origin
mergedElement.setMetaProperty('ref-origin', reference.uri);
// annotate fragment with info about referencing element
mergedElement.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(identityManager.identify(referencingElement)),
);

return mergedElement;
};

// assigning element identity if not already assigned
if (referencingElement.id.equals('')) {
elementIdentity.visitor.enter(referencingElement);
}

// attempting to create cycle
if (
ancestorsLineage.includes(referencingElement) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
isStringElement,
isMemberElement,
isObjectElement,
IdentityManager,
visit,
find,
cloneShallow,
cloneDeep,
toValue,
Element,
refractorPluginElementIdentity,
} from '@swagger-api/apidom-core';
import { ApiDOMError } from '@swagger-api/apidom-error';
import { evaluate as jsonPointerEvaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
Expand Down Expand Up @@ -53,23 +53,20 @@ import EvaluationJsonSchemaUriError from '../../../errors/EvaluationJsonSchemaUr
// @ts-ignore
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];

// initialize element identity manager
const identityManager = IdentityManager();

/**
* Predicate for detecting if element was created by merging referencing
* element with particular element identity with a referenced element.
*/
const wasReferencedBy =
<T extends Element, U extends Element>(referencingElement: T) =>
(element: U) => {
// @ts-ignore
return (
element.meta.hasKey('ref-referencing-element-id') &&
element.meta.get('ref-referencing-element-id').equals(toValue(referencingElement.id))
);
};

// initialize element identity plugin
const elementIdentity = refractorPluginElementIdentity()();
elementIdentity.pre();
(element: U) =>
element.meta.hasKey('ref-referencing-element-id') &&
element.meta
.get('ref-referencing-element-id')
.equals(toValue(identityManager.identify(referencingElement)));

// eslint-disable-next-line @typescript-eslint/naming-convention
const OpenApi3_1DereferenceVisitor = stampit({
Expand Down Expand Up @@ -227,6 +224,11 @@ const OpenApi3_1DereferenceVisitor = stampit({
});
// annotate fragment with info about origin
copy.setMetaProperty('ref-origin', reference.uri);
// annotate fragment with info about referencing element
copy.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(identityManager.identify(referencingElement)),
);

// override description and summary (outer has higher priority then inner)
if (isObjectElement(refedElement)) {
Expand All @@ -247,11 +249,6 @@ const OpenApi3_1DereferenceVisitor = stampit({
return copy;
};

// assigning element identity if not already assigned
if (referencingElement.id.equals('')) {
elementIdentity.visitor.enter(referencingElement);
}

// attempting to create cycle
if (
ancestorsLineage.includes(referencingElement) ||
Expand Down Expand Up @@ -367,15 +364,15 @@ const OpenApi3_1DereferenceVisitor = stampit({
});
// annotate referenced element with info about origin
mergedElement.setMetaProperty('ref-origin', reference.uri);
// annotate fragment with info about referencing element
mergedElement.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(identityManager.identify(referencingElement)),
);

return mergedElement;
};

// assigning element identity if not already assigned
if (referencingElement.id.equals('')) {
elementIdentity.visitor.enter(referencingElement);
}

// attempting to create cycle
if (
ancestorsLineage.includes(referencingElement) ||
Expand Down Expand Up @@ -634,7 +631,7 @@ const OpenApi3_1DereferenceVisitor = stampit({
// annotate fragment with info about referencing element
booleanJsonSchemaElement.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(referencingElement.id),
cloneDeep(identityManager.identify(referencingElement)),
);

return booleanJsonSchemaElement;
Expand Down Expand Up @@ -664,17 +661,12 @@ const OpenApi3_1DereferenceVisitor = stampit({
// annotate fragment with info about referencing element
mergedElement.setMetaProperty(
'ref-referencing-element-id',
cloneDeep(referencingElement.id),
cloneDeep(identityManager.identify(referencingElement)),
);

return mergedElement;
};

// assigning element identity if not already assigned
if (referencingElement.id.equals('')) {
elementIdentity.visitor.enter(referencingElement);
}

// attempting to create cycle
if (
ancestorsLineage.includes(referencingElement) ||
Expand Down

0 comments on commit cd191b3

Please sign in to comment.