From d1f203c3d843adb76217a750c0f414c608ceab0f Mon Sep 17 00:00:00 2001 From: John Kaster Date: Wed, 6 Oct 2021 03:05:12 -0700 Subject: [PATCH] fix: validateBody for API Explorer (#846) * fix: validateBody for already-parsed JSON * removed stale code comment --- .../components/RequestForm/formUtils.spec.tsx | 5 +++ .../src/components/RequestForm/formUtils.tsx | 4 +- packages/sdk-node/test/methods.spec.ts | 40 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/packages/run-it/src/components/RequestForm/formUtils.spec.tsx b/packages/run-it/src/components/RequestForm/formUtils.spec.tsx index f23f440ae..bfbcd3b2c 100644 --- a/packages/run-it/src/components/RequestForm/formUtils.spec.tsx +++ b/packages/run-it/src/components/RequestForm/formUtils.spec.tsx @@ -264,6 +264,11 @@ describe('Complex Item', () => { describe('validateBody', () => { test.each` value | error + ${{ + model: 'thelook', + view: 'users', + fields: ['users.id', 'users.first_name'], +}} | ${''} ${'na.-_me=Vapor&age=3&luckyNumbers[]=5&luckyNumbers[]=7'} | ${''} ${'name=Vapor&age=3&luckyNumbers[]=5&luckyNumbers[]7'} | ${'luckyNumbers[]7'} ${'{'} | ${'Unexpected end of JSON input'} diff --git a/packages/run-it/src/components/RequestForm/formUtils.tsx b/packages/run-it/src/components/RequestForm/formUtils.tsx index 1d36bf189..2b55f815a 100644 --- a/packages/run-it/src/components/RequestForm/formUtils.tsx +++ b/packages/run-it/src/components/RequestForm/formUtils.tsx @@ -323,9 +323,9 @@ export const validateEncodedValues = (body: string) => { * * @param body string to validate */ -export const validateBody = (body: string) => { +export const validateBody = (body: string | Record) => { let result = '' - if (body) { + if (body && typeof body === 'string') { if (/^[[{}"]/.test(body)) { // most likely JSON try { diff --git a/packages/sdk-node/test/methods.spec.ts b/packages/sdk-node/test/methods.spec.ts index d36601c01..58d662a8b 100644 --- a/packages/sdk-node/test/methods.spec.ts +++ b/packages/sdk-node/test/methods.spec.ts @@ -865,6 +865,46 @@ describe('LookerNodeSDK', () => { }, testTimeout ) + + it( + 'parses a query with no results', + async () => { + const sdk = new LookerSDK(session) + const query = await sdk.ok( + sdk.create_query({ + model: 'system__activity', + view: 'dashboard', + limit: '2', + fields: ['dashboard.id', 'dashboard.title'], + filters: { 'dashboard.id': '-1' }, + }) + ) + expect(query).toBeDefined() + expect(query.id).toBeDefined() + for (const format of ['csv', 'json', 'json_detail', 'txt', 'md']) { + let failed = '' + try { + const live = await sdk.ok( + sdk.run_query({ query_id: query.id!, result_format: format }) + ) + const cached = await sdk.ok( + sdk.run_query({ + query_id: query.id!, + result_format: format, + cache: true, + }) + ) + expect(live).not.toEqual('{}') + expect(cached).not.toEqual('{}') + } catch (e: any) { + failed = e.message + } + expect(failed).toEqual('') + } + await sdk.authSession.logout() + }, + testTimeout + ) }) describe('Dashboard endpoints', () => {