From dfc21184cf9bf8988c7ec84a571399eb8e6db6e9 Mon Sep 17 00:00:00 2001 From: Dean Date: Fri, 8 Jul 2022 09:29:16 +0100 Subject: [PATCH 1/2] Fixed issue where mutation was causing expression bindings to be lost. Request retries will now have them intact for re-enrichment --- packages/server/src/threads/query.js | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/server/src/threads/query.js b/packages/server/src/threads/query.js index 7c94c3d25a..0fe331c7bb 100644 --- a/packages/server/src/threads/query.js +++ b/packages/server/src/threads/query.js @@ -10,6 +10,7 @@ const { } = require("@budibase/backend-core/auth") const { user: userCache } = require("@budibase/backend-core/cache") const { getGlobalIDFromUserMetadataID } = require("../db/utils") +const { cloneDeep } = require("lodash/fp") const { isSQL } = require("../integrations/utils") const { @@ -41,20 +42,22 @@ class QueryRunner { async execute() { let { datasource, fields, queryVerb, transformer } = this - const Integration = integrations[datasource.source] + let datasourceClone = cloneDeep(datasource) + let fieldsClone = cloneDeep(fields) + + const Integration = integrations[datasourceClone.source] if (!Integration) { throw "Integration type does not exist." } - if (datasource.config.authConfigs) { - datasource.config.authConfigs = datasource.config.authConfigs.map( - config => { + if (datasourceClone.config.authConfigs) { + datasourceClone.config.authConfigs = + datasourceClone.config.authConfigs.map(config => { return enrichQueryFields(config, this.ctx) - } - ) + }) } - const integration = new Integration(datasource.config) + const integration = new Integration(datasourceClone.config) // pre-query, make sure datasource variables are added to parameters const parameters = await this.addDatasourceVariables() @@ -65,19 +68,19 @@ class QueryRunner { const enrichedContext = { ...enrichedParameters, ...this.ctx } // Parse global headers - if (datasource.config.defaultHeaders) { - datasource.config.defaultHeaders = enrichQueryFields( - datasource.config.defaultHeaders, + if (datasourceClone.config.defaultHeaders) { + datasourceClone.config.defaultHeaders = enrichQueryFields( + datasourceClone.config.defaultHeaders, enrichedContext ) } let query // handle SQL injections by interpolating the variables - if (isSQL(datasource)) { - query = interpolateSQL(fields, enrichedParameters, integration) + if (isSQL(datasourceClone)) { + query = interpolateSQL(fieldsClone, enrichedParameters, integration) } else { - query = enrichQueryFields(fields, enrichedContext) + query = enrichQueryFields(fieldsClone, enrichedContext) } // Add pagination values for REST queries @@ -116,9 +119,10 @@ class QueryRunner { await this.refreshOAuth2(this.ctx) // Attempt to refresh the access token from the provider this.hasRefreshedOAuth = true + } else { + this.hasRerun = true } - this.hasRerun = true await threadUtils.invalidateDynamicVariables(this.cachedVariables) return this.execute() } From 41d8fa8340dba4b1bad63a562ebf518075edf056 Mon Sep 17 00:00:00 2001 From: Dean Date: Fri, 8 Jul 2022 11:19:41 +0100 Subject: [PATCH 2/2] Updated query tests to remove altered datasource. Updated messaging for refresh failure messaging --- packages/server/src/api/routes/tests/query.spec.js | 1 - packages/server/src/threads/query.js | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/routes/tests/query.spec.js b/packages/server/src/api/routes/tests/query.spec.js index 273bdb9993..00cae3a9c9 100644 --- a/packages/server/src/api/routes/tests/query.spec.js +++ b/packages/server/src/api/routes/tests/query.spec.js @@ -218,7 +218,6 @@ describe("/queries", () => { expect(res.body.schemaFields).toEqual(["a", "b"]) expect(res.body.rows.length).toEqual(1) expect(events.query.previewed).toBeCalledTimes(1) - datasource.config = { schema: "public" } expect(events.query.previewed).toBeCalledWith(datasource, query) }) diff --git a/packages/server/src/threads/query.js b/packages/server/src/threads/query.js index 0fe331c7bb..757fdc040b 100644 --- a/packages/server/src/threads/query.js +++ b/packages/server/src/threads/query.js @@ -191,8 +191,9 @@ class QueryRunner { } else { // In this event the user may have oAuth issues that // could require re-authenticating with their provider. + let errorMessage = resp.err.data ? resp.err.data : resp.err.toString() throw new Error( - "OAuth2 access token could not be refreshed: " + resp.err.toString() + "OAuth2 access token could not be refreshed: " + errorMessage ) }