fix(core): Prevent mutation of credential type parents (#16841)

This commit is contained in:
Iván Ovejero
2025-06-30 17:32:51 +02:00
committed by GitHub
parent ed2cb3c701
commit cb1103e742
2 changed files with 30 additions and 5 deletions

View File

@@ -117,5 +117,26 @@ describe('CredentialTypes', () => {
expect(credentialTypes.getParentTypes('unknownCredential')).toBeEmptyArray();
});
test('Should not mutate the original extends array', () => {
const knownCredentials = {
childType: { extends: ['parentType'] },
parentType: { extends: ['grandparentType'] },
grandparentType: { extends: [] },
};
const credentialTypes = new CredentialTypes(
mock<LoadNodesAndCredentials>({ knownCredentials }),
);
const originalExtends = knownCredentials.childType.extends;
const originalLength = originalExtends.length;
credentialTypes.getParentTypes('childType');
credentialTypes.getParentTypes('childType');
credentialTypes.getParentTypes('childType');
expect(originalExtends).toHaveLength(originalLength);
});
});
});

View File

@@ -25,11 +25,15 @@ export class CredentialTypes implements ICredentialTypes {
*/
getParentTypes(typeName: string): string[] {
const extendsArr = this.loadNodesAndCredentials.knownCredentials[typeName]?.extends ?? [];
if (extendsArr.length) {
extendsArr.forEach((type) => {
extendsArr.push(...this.getParentTypes(type));
});
if (extendsArr.length === 0) return [];
const extendsArrCopy = [...extendsArr];
for (const type of extendsArrCopy) {
extendsArrCopy.push(...this.getParentTypes(type));
}
return extendsArr;
return extendsArrCopy;
}
}