diff --git a/.changeset/seven-socks-study.md b/.changeset/seven-socks-study.md new file mode 100644 index 0000000000..8193d3deb2 --- /dev/null +++ b/.changeset/seven-socks-study.md @@ -0,0 +1,5 @@ +--- +'@backstage/catalog-client': patch +--- + +Fix the `CatalogClient::getEntities` method to only sort the resulting entities in case no `order`-parameter is provided. diff --git a/packages/catalog-client/src/CatalogClient.test.ts b/packages/catalog-client/src/CatalogClient.test.ts index a5ff7c6e31..ce44b93b59 100644 --- a/packages/catalog-client/src/CatalogClient.test.ts +++ b/packages/catalog-client/src/CatalogClient.test.ts @@ -237,6 +237,10 @@ describe('CatalogClient', () => { it('handles ordering properly', async () => { expect.assertions(2); + const sortedEntities = [ + { apiVersion: '1', kind: 'Component', metadata: { name: 'b' } }, + { apiVersion: '1', kind: 'Component', metadata: { name: 'a' } }, + ]; server.use( rest.get(`${mockBaseUrl}/entities`, (req, res, ctx) => { @@ -245,7 +249,7 @@ describe('CatalogClient', () => { 'asc:kind', 'desc:metadata.name', ]); - return res(ctx.json([])); + return res(ctx.json(sortedEntities)); }), ); @@ -259,7 +263,7 @@ describe('CatalogClient', () => { { token }, ); - expect(response.items).toEqual([]); + expect(response.items).toEqual(sortedEntities); }); }); diff --git a/packages/catalog-client/src/CatalogClient.ts b/packages/catalog-client/src/CatalogClient.ts index 947195d291..2440f043d7 100644 --- a/packages/catalog-client/src/CatalogClient.ts +++ b/packages/catalog-client/src/CatalogClient.ts @@ -143,6 +143,11 @@ export class CatalogClient implements CatalogApi { ), ); + // do not sort entities, if order is provided + if (encodedOrder.length) { + return { items: entities }; + } + const refCompare = (a: Entity, b: Entity) => { // in case field filtering is used, these fields might not be part of the response if (