diff --git a/.changeset/sour-cherries-occur.md b/.changeset/sour-cherries-occur.md new file mode 100644 index 0000000000..a8837749d3 --- /dev/null +++ b/.changeset/sour-cherries-occur.md @@ -0,0 +1,5 @@ +--- +'@backstage/catalog-client': patch +--- + +Don't crash if the entities response doesn't include the entities name and kind diff --git a/packages/catalog-client/src/CatalogClient.test.ts b/packages/catalog-client/src/CatalogClient.test.ts index 6d43af85ed..4e3d95f99f 100644 --- a/packages/catalog-client/src/CatalogClient.test.ts +++ b/packages/catalog-client/src/CatalogClient.test.ts @@ -151,6 +151,26 @@ describe('CatalogClient', () => { expect(response.items).toEqual([]); }); + + it('handles field filtered entities', async () => { + server.use( + rest.get(`${mockBaseUrl}/entities`, (_req, res, ctx) => { + return res(ctx.json([{ apiVersion: '1' }, { apiVersion: '2' }])); + }), + ); + + const response = await client.getEntities( + { + fields: ['apiVersion'], + }, + { token }, + ); + + expect(response.items).toEqual([ + { apiVersion: '1' }, + { apiVersion: '2' }, + ]); + }); }); describe('getLocationById', () => { diff --git a/packages/catalog-client/src/CatalogClient.ts b/packages/catalog-client/src/CatalogClient.ts index 10c282f786..35b8045d27 100644 --- a/packages/catalog-client/src/CatalogClient.ts +++ b/packages/catalog-client/src/CatalogClient.ts @@ -92,6 +92,16 @@ export class CatalogClient implements CatalogApi { ); const refCompare = (a: Entity, b: Entity) => { + // in case field filtering is used, these fields might not be part of the response + if ( + a.metadata?.name === undefined || + a.metadata?.kind === undefined || + b.metadata?.name === undefined || + b.metadata?.kind === undefined + ) { + return 0; + } + const aRef = stringifyEntityRef(a); const bRef = stringifyEntityRef(b); if (aRef < bRef) {