Skip to content

Commit feb3fe3

Browse files
authored
feat: Support snake case security_context in COMPILE_CONTEXT (#6519)
* chore: Support snake case security_context in COMPILE_CONTEXT * fix:lint
1 parent f023484 commit feb3fe3

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

packages/cubejs-schema-compiler/src/compiler/DataSchemaCompiler.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,21 @@ export class DataSchemaCompiler {
235235
return exports[foundFile.fileName];
236236
}
237237
},
238-
COMPILE_CONTEXT: this.standalone ? this.standaloneCompileContextProxy() : R.clone(this.compileContext || {}),
238+
COMPILE_CONTEXT: this.standalone ? this.standaloneCompileContextProxy() : this.cloneCompileContextWithGetterAlias(this.compileContext || {}),
239239
}, { filename: file.fileName, timeout: 15000 });
240240
} catch (e) {
241241
errorsReport.error(e);
242242
}
243243
}
244244

245+
// Alias "securityContext" with "security_context" (snake case version)
246+
// to support snake case based data models
247+
cloneCompileContextWithGetterAlias(compileContext) {
248+
const clone = R.clone(compileContext || {});
249+
clone.security_context = compileContext.securityContext;
250+
return clone;
251+
}
252+
245253
standaloneCompileContextProxy() {
246254
return new Proxy({}, {
247255
get: () => {

packages/cubejs-schema-compiler/test/integration/postgres/yaml-compiler.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,41 @@ cubes:
444444
}]
445445
);
446446
});
447+
448+
it('COMPILE_CONTEXT', async () => {
449+
const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler(`
450+
cubes:
451+
- name: orders
452+
sql: "SELECT 1 as id, 'completed' as status"
453+
public: COMPILE_CONTEXT.security_context.can_see_orders
454+
455+
measures:
456+
- name: count
457+
type: count
458+
`,
459+
{},
460+
{
461+
compileContext: {
462+
authInfo: null,
463+
securityContext: { can_see_orders: true },
464+
requestId: 'XXX'
465+
}
466+
});
467+
468+
await compiler.compile();
469+
470+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
471+
measures: [
472+
'orders.count'
473+
],
474+
timeDimensions: [],
475+
timezone: 'America/Los_Angeles'
476+
});
477+
478+
return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
479+
expect(res).toEqual(
480+
[{ orders__count: '1' }]
481+
);
482+
});
483+
});
447484
});

packages/cubejs-schema-compiler/test/unit/PrepareCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const prepareCompiler = (content, options) => originalPrepareCompiler({
77
])
88
}, { adapter: 'postgres', ...options });
99

10-
export const prepareYamlCompiler = (content, { yamlExtension, ...options } = {}) => originalPrepareCompiler({
10+
export const prepareYamlCompiler = (content, yamlExtension, options = {}) => originalPrepareCompiler({
1111
localPath: () => __dirname,
1212
dataSchemaFiles: () => Promise.resolve([
1313
{ fileName: yamlExtension ? 'main.yaml' : 'main.yml', content }

0 commit comments

Comments
 (0)