From c0146eaf79ab5dab254f1f41a78e3e2a42f48826 Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Thu, 26 Sep 2024 10:17:47 -0700 Subject: [PATCH 1/2] fix: Multi-kind context containing only 1 kind conveted incorrectly. --- .../common/__tests__/ContextFilter.test.ts | 34 +++++++++++++++++++ packages/shared/common/src/Context.ts | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/shared/common/__tests__/ContextFilter.test.ts b/packages/shared/common/__tests__/ContextFilter.test.ts index 3f1d7c0b1..1741db444 100644 --- a/packages/shared/common/__tests__/ContextFilter.test.ts +++ b/packages/shared/common/__tests__/ContextFilter.test.ts @@ -1,3 +1,4 @@ +import { LDContext } from '../dist'; import { AttributeReference, Context } from '../src'; import ContextFilter from '../src/ContextFilter'; @@ -416,6 +417,23 @@ describe('when handling mult-kind contexts', () => { }, }; + const multiWithSingleContext = Context.fromLDContext({ + kind: 'multi', + user: { + key: 'abc', + name: 'alphabet', + letters: ['a', 'b', 'c'], + order: 3, + object: { + a: 'a', + b: 'b', + }, + _meta: { + privateAttributes: ['letters', '/object/b'], + }, + }, + }); + it('it should remove attributes from all contexts when all attributes are private.', () => { const uf = new ContextFilter(true, []); expect(uf.filter(orgAndUserContext)).toEqual(orgAndUserContextAllPrivate); @@ -430,4 +448,20 @@ describe('when handling mult-kind contexts', () => { const uf = new ContextFilter(false, [new AttributeReference('name', true)]); expect(uf.filter(orgAndUserContext)).toEqual(orgAndUserGlobalNamePrivate); }); + + it('should produce event with valid single context', () => { + const uf = new ContextFilter(false, []); + expect(uf.filter(multiWithSingleContext)).toEqual({ + kind: 'user', + _meta: { + redactedAttributes: ['/object/b', 'letters'], + }, + key: 'abc', + name: 'alphabet', + object: { + a: 'a', + }, + order: 3, + }); + }); }); diff --git a/packages/shared/common/src/Context.ts b/packages/shared/common/src/Context.ts index 6bf677ac4..9220afcdf 100644 --- a/packages/shared/common/src/Context.ts +++ b/packages/shared/common/src/Context.ts @@ -265,7 +265,7 @@ export default class Context { if (kinds.length === 1) { const kind = kinds[0]; const created = new Context(true, kind); - created.context = contexts[kind]; + created.context = { ...contexts[kind], kind }; created.privateAttributeReferences = privateAttributes; created.isUser = kind === 'user'; return created; From ff36c7911cf64889aaad7e404e2c4e80670dfc4e Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Thu, 26 Sep 2024 10:21:31 -0700 Subject: [PATCH 2/2] lint --- packages/shared/common/__tests__/ContextFilter.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/shared/common/__tests__/ContextFilter.test.ts b/packages/shared/common/__tests__/ContextFilter.test.ts index 1741db444..17cd827cf 100644 --- a/packages/shared/common/__tests__/ContextFilter.test.ts +++ b/packages/shared/common/__tests__/ContextFilter.test.ts @@ -1,4 +1,3 @@ -import { LDContext } from '../dist'; import { AttributeReference, Context } from '../src'; import ContextFilter from '../src/ContextFilter';