diff --git a/.changeset/five-dryers-shout.md b/.changeset/five-dryers-shout.md new file mode 100644 index 0000000000..fbcc85182d --- /dev/null +++ b/.changeset/five-dryers-shout.md @@ -0,0 +1,18 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Ignore empty YAML documents. Having a YAML file like this is now ingested without an error: + +```yaml +apiVersion: backstage.io/v1alpha1 +kind: Component +metadata: + name: web +spec: + type: website +--- + +``` + +This behaves now the same way as Kubernetes handles multiple documents in a single YAML file. diff --git a/plugins/catalog-backend/src/ingestion/processors/util/parse.test.ts b/plugins/catalog-backend/src/ingestion/processors/util/parse.test.ts index 8cb91fb2ce..53201486bc 100644 --- a/plugins/catalog-backend/src/ingestion/processors/util/parse.test.ts +++ b/plugins/catalog-backend/src/ingestion/processors/util/parse.test.ts @@ -115,6 +115,41 @@ describe('parseEntityYaml', () => { ]); }); + it('should handle empty yaml documents', () => { + // This happens if the user accidentially adds a "---" + // at the end of a file + const results = Array.from( + parseEntityYaml( + Buffer.from( + ` + apiVersion: backstage.io/v1alpha1 + kind: Component + metadata: + name: web + spec: + type: website +--- + `, + 'utf8', + ), + testLoc, + ), + ); + + expect(results).toEqual([ + result.entity(testLoc, { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'web', + }, + spec: { + type: 'website', + }, + }), + ]); + }); + it('should emit parsing errors', () => { const results = Array.from( parseEntityYaml(Buffer.from('`', 'utf8'), testLoc), diff --git a/plugins/catalog-backend/src/ingestion/processors/util/parse.ts b/plugins/catalog-backend/src/ingestion/processors/util/parse.ts index 3f07a815b0..c3dcd42d62 100644 --- a/plugins/catalog-backend/src/ingestion/processors/util/parse.ts +++ b/plugins/catalog-backend/src/ingestion/processors/util/parse.ts @@ -40,6 +40,9 @@ export function* parseEntityYaml( const json = document.toJSON(); if (lodash.isPlainObject(json)) { yield result.entity(location, json as Entity); + } else if (json === null) { + // Ignore null values, these happen if there is an empty document in the + // YAML file, for example if --- is added to the end of the file. } else { const message = `Expected object at root, got ${typeof json}`; yield result.generalError(location, message);