From c6b29f3dbedda70a36b6fd92194912a2a9dd37f0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 11 Aug 2023 15:04:40 +0200 Subject: [PATCH] todo: move filter encoding fix to backend Signed-off-by: Patrik Oldsberg --- .changeset/pretty-ways-knock.md | 4 ++-- .../src/schema/openapi.generated.ts | 1 + plugins/todo-backend/src/schema/openapi.yaml | 1 + .../todo-backend/src/service/router.test.ts | 17 +++++++++++++++++ plugins/todo/src/api/TodoClient.ts | 18 +++++++----------- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/.changeset/pretty-ways-knock.md b/.changeset/pretty-ways-knock.md index d06a107fe3..1b8d56c178 100644 --- a/.changeset/pretty-ways-knock.md +++ b/.changeset/pretty-ways-knock.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-todo': patch +'@backstage/plugin-todo-backend': patch --- -Fixed URL encoding of filters where the filter was always rejected by the backend validation. +Fixed a bug where filter queries from the frontend would always fail validation. diff --git a/plugins/todo-backend/src/schema/openapi.generated.ts b/plugins/todo-backend/src/schema/openapi.generated.ts index a58a96ddb8..effe851790 100644 --- a/plugins/todo-backend/src/schema/openapi.generated.ts +++ b/plugins/todo-backend/src/schema/openapi.generated.ts @@ -173,6 +173,7 @@ export const spec = { name: 'filter', in: 'query', required: false, + allowReserved: true, schema: { type: 'array', description: diff --git a/plugins/todo-backend/src/schema/openapi.yaml b/plugins/todo-backend/src/schema/openapi.yaml index 0e6203e002..8023144f48 100644 --- a/plugins/todo-backend/src/schema/openapi.yaml +++ b/plugins/todo-backend/src/schema/openapi.yaml @@ -111,6 +111,7 @@ paths: - name: filter in: query required: false + allowReserved: true schema: type: array description: A list of filters used to narrow down the listed TODO items diff --git a/plugins/todo-backend/src/service/router.test.ts b/plugins/todo-backend/src/service/router.test.ts index 0bf591316c..64f894a147 100644 --- a/plugins/todo-backend/src/service/router.test.ts +++ b/plugins/todo-backend/src/service/router.test.ts @@ -131,6 +131,23 @@ describe('createRouter', () => { ); }); + it('forwards filter query', async () => { + mockService.listTodos.mockResolvedValueOnce(mockListBody); + + const response = await request(app).get('/v1/todos?filter=text=*borked*'); + expect(response.status).toEqual(200); + expect(response.body).toEqual(mockListBody); + expect(mockService.listTodos).toHaveBeenCalledWith( + { + entity: undefined, + offset: undefined, + limit: undefined, + filters: [{ field: 'text', value: '*borked*' }], + }, + { token: undefined }, + ); + }); + it('rejects invalid queries', async () => { request(app) .get('/v1/todos?entity=k:n&entity=k:n') diff --git a/plugins/todo/src/api/TodoClient.ts b/plugins/todo/src/api/TodoClient.ts index e86e444c30..4bfd5b77e5 100644 --- a/plugins/todo/src/api/TodoClient.ts +++ b/plugins/todo/src/api/TodoClient.ts @@ -67,17 +67,13 @@ export class TodoClient implements TodoApi { } } - const res = await fetch( - // TODO(Rugvip): Figure out a better solution to '*' not being URL encoded but that being required by OpenAPI - `${baseUrl}/v1/todos?${String(query).replace(/\*/g, '%2A')}`, - { - headers: token - ? { - Authorization: `Bearer ${token}`, - } - : undefined, - }, - ); + const res = await fetch(`${baseUrl}/v1/todos?${query}`, { + headers: token + ? { + Authorization: `Bearer ${token}`, + } + : undefined, + }); if (!res.ok) { throw await ResponseError.fromResponse(res);