From 776eb56523b29920592f4ca688d0613481f0bd72 Mon Sep 17 00:00:00 2001 From: Thomas Cardonne Date: Thu, 25 Jul 2024 00:52:49 +0200 Subject: [PATCH] fix(catalog-backend): error if metadata.annotations isn't a valid object Signed-off-by: Thomas Cardonne --- .changeset/cuddly-penguins-glow.md | 7 ++++ .../processing/ProcessorOutputCollector.ts | 35 ++++++++++--------- 2 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 .changeset/cuddly-penguins-glow.md diff --git a/.changeset/cuddly-penguins-glow.md b/.changeset/cuddly-penguins-glow.md new file mode 100644 index 0000000000..b00422cf8b --- /dev/null +++ b/.changeset/cuddly-penguins-glow.md @@ -0,0 +1,7 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +`ProcessorOutputCollector` returns an error when receiving deferred entities that have an invalid `metadata.annotations` format. + +This allows to return an error on an actual validation issue instead of reporting that the location annotations are missing afterwards, which is misleading for the users. diff --git a/plugins/catalog-backend/src/processing/ProcessorOutputCollector.ts b/plugins/catalog-backend/src/processing/ProcessorOutputCollector.ts index eb8cbcc81a..0de6b12b90 100644 --- a/plugins/catalog-backend/src/processing/ProcessorOutputCollector.ts +++ b/plugins/catalog-backend/src/processing/ProcessorOutputCollector.ts @@ -115,24 +115,27 @@ export class ProcessorOutputCollector { // Note that at this point, we have only validated the envelope part of // the entity data. Annotations are not part of that, so we have to be - // defensive. If the annotations were malformed (e.g. were not a valid - // object), we just skip over this step and let the full entity - // validation at the next step of processing catch that. + // defensive and report an error if the annotations isn't a valid object, to avoid + // hiding errors when adding location annotations. const annotations = entity.metadata.annotations || {}; - if (typeof annotations === 'object' && !Array.isArray(annotations)) { - const originLocation = getEntityOriginLocationRef(this.parentEntity); - entity = { - ...entity, - metadata: { - ...entity.metadata, - annotations: { - ...annotations, - [ANNOTATION_ORIGIN_LOCATION]: originLocation, - [ANNOTATION_LOCATION]: location, - }, - }, - }; + if (typeof annotations !== 'object' || Array.isArray(annotations)) { + this.errors.push( + new Error('metadata.annotations must be a valid object'), + ); + return; } + const originLocation = getEntityOriginLocationRef(this.parentEntity); + entity = { + ...entity, + metadata: { + ...entity.metadata, + annotations: { + ...annotations, + [ANNOTATION_ORIGIN_LOCATION]: originLocation, + [ANNOTATION_LOCATION]: location, + }, + }, + }; this.deferredEntities.push({ entity, locationKey: location }); } else if (i.type === 'location') {