Skip to content

Commit 01d5c87

Browse files
authored
Merge pull request #331 from DataDog/blake.thomas/fix-configurationToString
fix(core): serialize precomputed.response in configurationToString
2 parents 2e1c06d + 3fd7e42 commit 01d5c87

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { FlagsConfiguration } from './configuration'
2+
import { configurationFromString, configurationToString } from './wire'
3+
4+
const configuration: FlagsConfiguration = {
5+
precomputed: {
6+
response: {
7+
data: {
8+
attributes: {
9+
createdAt: '2026-07-06T23:01:56.822Z',
10+
flags: {
11+
'my-flag': {
12+
allocationKey: 'alloc-1',
13+
variationKey: 'true',
14+
variationType: 'boolean',
15+
variationValue: true,
16+
reason: 'STATIC',
17+
doLog: true,
18+
extraLogging: {},
19+
},
20+
},
21+
},
22+
},
23+
},
24+
context: { targetingKey: 'user-1', country: 'US' },
25+
},
26+
}
27+
28+
describe('configuration wire', () => {
29+
it('round-trips a precomputed configuration', () => {
30+
const restored = configurationFromString(configurationToString(configuration))
31+
32+
expect(restored).toEqual(configuration)
33+
})
34+
35+
it('keeps flags readable after a round-trip', () => {
36+
const restored = configurationFromString(configurationToString(configuration))
37+
38+
expect(restored.precomputed?.response.data.attributes.flags['my-flag'].variationValue).toBe(true)
39+
})
40+
41+
it('serializes precomputed.response as a stringified response object, not the whole precomputed', () => {
42+
const wire = JSON.parse(configurationToString(configuration))
43+
44+
// `precomputed.response` on the wire must be the response only, so parsing it yields
45+
// the response object (regression guard: it previously stringified the whole
46+
// precomputed object, double-nesting the response and losing the flags).
47+
expect(JSON.parse(wire.precomputed.response)).toEqual(configuration.precomputed?.response)
48+
})
49+
50+
it('returns an empty configuration for an unknown version', () => {
51+
expect(configurationFromString(JSON.stringify({ version: 2 }))).toEqual({})
52+
})
53+
54+
it('returns an empty configuration for malformed input', () => {
55+
expect(configurationFromString('not json')).toEqual({})
56+
})
57+
})

packages/core/src/configuration/wire.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function configurationToString(configuration: FlagsConfiguration): string
5050
if (configuration.precomputed) {
5151
wire.precomputed = {
5252
...configuration.precomputed,
53-
response: JSON.stringify(configuration.precomputed),
53+
response: JSON.stringify(configuration.precomputed.response),
5454
}
5555
}
5656

0 commit comments

Comments
 (0)