diff --git a/plugins/catalog-backend/src/service/createRouter.test.ts b/plugins/catalog-backend/src/service/createRouter.test.ts index 53f831d320..2d158f7f16 100644 --- a/plugins/catalog-backend/src/service/createRouter.test.ts +++ b/plugins/catalog-backend/src/service/createRouter.test.ts @@ -53,7 +53,7 @@ describe('createRouter readonly disabled', () => { let locationAnalyzer: jest.Mocked; let permissionsService: jest.Mocked; - beforeAll(async () => { + beforeEach(async () => { entitiesCatalog = { entities: jest.fn(), entitiesBatch: jest.fn(), @@ -135,6 +135,38 @@ describe('createRouter readonly disabled', () => { { apiVersion: 'a', kind: 'b', metadata: { name: 'n' } }, ]; + entitiesCatalog.entities.mockResolvedValueOnce({ + entities: { type: 'object', entities: [entities[0]] }, + pageInfo: { hasNextPage: false }, + }); + + const response = await request(app).get('/entities'); + + expect(response.status).toEqual(200); + expect(response.body).toEqual(entities); + }); + + it('happy path: lists entities when by-entities emulation is enabled', async () => { + const router = await createRouter({ + entitiesCatalog, + locationService, + orchestrator, + logger: mockServices.logger.mock(), + refreshService, + config: new ConfigReader(undefined), + permissionIntegrationRouter: express.Router(), + auth: mockServices.auth(), + httpAuth: mockServices.httpAuth(), + locationAnalyzer, + permissionsService: permissionsService, + disableRelationsCompatibility: true, // added + }); + app = await wrapServer(express().use(router)); + + const entities: Entity[] = [ + { apiVersion: 'a', kind: 'b', metadata: { name: 'n' } }, + ]; + entitiesCatalog.queryEntities.mockResolvedValueOnce({ items: { type: 'object', entities: [entities[0]] }, pageInfo: {}, @@ -148,6 +180,49 @@ describe('createRouter readonly disabled', () => { }); it('parses single and multiple request parameters and passes them down', async () => { + entitiesCatalog.entities.mockResolvedValueOnce({ + entities: { type: 'object', entities: [] }, + pageInfo: { hasNextPage: false }, + }); + const response = await request(app).get( + '/entities?filter=a=1,a=2,b=3&filter=c=4', + ); + + expect(response.status).toEqual(200); + expect(entitiesCatalog.entities).toHaveBeenCalledTimes(1); + expect(entitiesCatalog.entities).toHaveBeenCalledWith({ + filter: { + anyOf: [ + { + allOf: [ + { key: 'a', values: ['1', '2'] }, + { key: 'b', values: ['3'] }, + ], + }, + { key: 'c', values: ['4'] }, + ], + }, + credentials: mockCredentials.user(), + }); + }); + + it('parses single and multiple request parameters and passes them down when by-entities emulation is enabled', async () => { + const router = await createRouter({ + entitiesCatalog, + locationService, + orchestrator, + logger: mockServices.logger.mock(), + refreshService, + config: new ConfigReader(undefined), + permissionIntegrationRouter: express.Router(), + auth: mockServices.auth(), + httpAuth: mockServices.httpAuth(), + locationAnalyzer, + permissionsService: permissionsService, + disableRelationsCompatibility: true, // added + }); + app = await wrapServer(express().use(router)); + entitiesCatalog.queryEntities.mockResolvedValueOnce({ items: { type: 'object', entities: [] }, pageInfo: {}, diff --git a/plugins/catalog-backend/src/service/createRouter.ts b/plugins/catalog-backend/src/service/createRouter.ts index 50ccb08b4c..58474f4dbc 100644 --- a/plugins/catalog-backend/src/service/createRouter.ts +++ b/plugins/catalog-backend/src/service/createRouter.ts @@ -43,7 +43,6 @@ import { LocationService, RefreshService } from './types'; import { disallowReadonlyMode, encodeCursor, - expandLegacyCompoundRelationsInEntity, locationInput, validateRequestBody, } from './util'; @@ -60,7 +59,6 @@ import { LocationAnalyzer } from '@backstage/plugin-catalog-node'; import { AuthorizedValidationService } from './AuthorizedValidationService'; import { createEntityArrayJsonStream, - processEntitiesResponseItems, writeEntitiesResponse, writeSingleEntityResponse, } from './response'; @@ -153,7 +151,7 @@ export async function createRouter( // When pagination parameters are passed in, use the legacy slow path // that loads all entities into memory - if (pagination) { + if (pagination || disableRelationsCompatibility !== true) { const { entities, pageInfo } = await entitiesCatalog.entities({ filter, fields, @@ -194,12 +192,6 @@ export async function createRouter( ); if (result.items.entities.length) { - if (!disableRelationsCompatibility) { - result.items = processEntitiesResponseItems( - result.items, - expandLegacyCompoundRelationsInEntity, - ); - } if (await responseStream.send(result.items)) { return; // Client closed connection }