Skip to content

Commit 2a0c28f

Browse files
authored
Merge pull request #688 from namecheap/fix/app_filtering_query_fix
fix: resolve SQL ambiguous column reference in apps API filtering
2 parents 6c7e8f3 + 5ae554a commit 2a0c28f

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

registry/server/apps/repositories/AppsRepository.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class AppsRepository {
5050
return;
5151
}
5252
if (filters.id || filters.name) {
53-
query.whereIn('name', [...(filters.id ?? filters.name ?? [])]);
53+
query.whereIn(`${Tables.Apps}.name`, [...(filters.id ?? filters.name ?? [])]);
5454
}
5555
}
5656

@@ -62,14 +62,14 @@ export class AppsRepository {
6262
if (kind.length === 0) {
6363
return;
6464
}
65-
query.whereIn('kind', kind);
65+
query.whereIn(`${Tables.Apps}.kind`, kind);
6666
}
6767

6868
private addFilterByQuery(query: Knex.QueryBuilder, filters: AppsGetListFilters) {
6969
if (!filters.q) {
7070
return;
7171
}
72-
query.where('name', 'like', `%${filters.q}%`);
72+
query.where(`${Tables.Apps}.name`, 'like', `%${filters.q}%`);
7373
}
7474

7575
private addFilterByDomainId(query: Knex.QueryBuilder, filters: AppsGetListFilters) {
@@ -92,8 +92,12 @@ export class AppsRepository {
9292
query
9393
.leftJoin(Tables.RouteSlots, `${Tables.RouteSlots}.appName`, `${Tables.Apps}.name`)
9494
.leftJoin(Tables.Routes, `${Tables.Routes}.id`, `${Tables.RouteSlots}.routeId`)
95-
.where(`${Tables.Routes}.domainId`, filters.domainId)
96-
.orWhere(`${Tables.Apps}.enforceDomain`, filters.domainId)
95+
.where(function () {
96+
this.where(`${Tables.Routes}.domainId`, filters.domainId).orWhere(
97+
`${Tables.Apps}.enforceDomain`,
98+
filters.domainId,
99+
);
100+
})
97101
.groupBy(`${Tables.Apps}.name`)
98102
.groupBy('v.versionId');
99103
}

registry/tests/apps.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,60 @@ describe(`Tests ${example.url}`, () => {
550550
}
551551
});
552552

553+
it('should successfully return records filtered by name search (q) and domainId combined', async () => {
554+
const teardownFns: (() => Promise<void>)[] = [];
555+
556+
try {
557+
for (const app of example.appsList) {
558+
await req.post(example.url).send(app).expect(200);
559+
}
560+
teardownFns.unshift(async () => {
561+
for (const app of example.appsList) {
562+
await req.delete(example.url + app.name).expect(204);
563+
}
564+
});
565+
566+
const template = {
567+
name: 'hello500',
568+
content: '<html><head></head><body class="custom">hello500</body></html>',
569+
};
570+
await req.post('/api/v1/template').send(template).expect(200);
571+
teardownFns.unshift(async () => {
572+
await req.delete('/api/v1/template/' + template.name).expect(204);
573+
});
574+
575+
const domain = { domainName: 'example.com', template500: template.name };
576+
const {
577+
body: { id: routerDomainId },
578+
} = await req.post('/api/v1/router_domains').send(domain).expect(200);
579+
teardownFns.unshift(async () => {
580+
await req.delete('/api/v1/router_domains/' + routerDomainId).expect(204);
581+
});
582+
583+
const route = {
584+
next: false,
585+
slots: { myslot: { appName: example.appsList[1].name, kind: 'primary' } },
586+
route: 'myroute',
587+
domainId: routerDomainId,
588+
};
589+
const {
590+
body: { id: routeId },
591+
} = await req.post('/api/v1/route').send(route).expect(200);
592+
teardownFns.unshift(async () => {
593+
await req.delete('/api/v1/route/' + routeId).expect(204);
594+
});
595+
596+
const query = makeFilterQuery({ q: 'app1', domainId: routerDomainId });
597+
const response = await req.get(`${example.url}?${query}`).expect(200);
598+
599+
expectAppsListEqual(response.body, [example.appsList[1]]);
600+
} finally {
601+
for (const fn of teardownFns) {
602+
await fn();
603+
}
604+
}
605+
});
606+
553607
it('should successfully filter by name array (single name)', async () => {
554608
try {
555609
for (const app of example.appsList) {

0 commit comments

Comments
 (0)