frontend-app-api: reject undeclared attachments

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-10 10:10:27 +02:00
parent 2724c331ed
commit 68ffb9e67d
3 changed files with 57 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/frontend-app-api': patch
---
The app will now reject any extensions that attach to nonexistent inputs.
@@ -250,6 +250,49 @@ describe('createExtensionInstance', () => {
);
});
it('should refuse to create an instance with undeclared inputs', () => {
expect(() =>
createExtensionInstance({
attachments: new Map([
[
'declared',
[
createExtensionInstance({
attachments: new Map(),
config: { output: 'many1' },
extension: simpleExtension,
}),
],
],
[
'undeclared',
[
createExtensionInstance({
attachments: new Map(),
config: { output: 'many1' },
extension: simpleExtension,
}),
],
],
]),
config: undefined,
extension: createExtension({
id: 'core.test',
attachTo: { id: 'ignored', input: 'ignored' },
inputs: {
declared: createExtensionInput({
test: testDataRef,
}),
},
output: {},
factory() {},
}),
}),
).toThrow(
"Failed to instantiate extension 'core.test', received undeclared input(s) 'undeclared'",
);
});
it('should refuse to create an instance with multiple inputs for required singleton', () => {
expect(() =>
createExtensionInstance({
@@ -60,6 +60,15 @@ function resolveInputs(
inputMap: AnyExtensionInputMap,
attachments: Map<string, ExtensionInstance[]>,
) {
const undeclaredAttachments = Array.from(attachments.keys()).filter(
name => inputMap[name] === undefined,
);
if (undeclaredAttachments.length > 0) {
throw new Error(
`received undeclared input(s) '${undeclaredAttachments.join("', '")}'`,
);
}
return mapValues(inputMap, (input, inputName) => {
const attachedInstances = attachments.get(inputName) ?? [];
if (input.config.singleton) {