Merge pull request #25778 from leboncoin/fix-25622

fix(catalog-backend): error if metadata.annotations isn't a valid object
This commit is contained in:
Ben Lambert
2024-08-14 09:28:38 +02:00
committed by GitHub
2 changed files with 26 additions and 16 deletions
+7
View File
@@ -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.
@@ -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') {