`init({ traits })` double-wraps `{ value, transient }` trait objects, sending a nested `trait_value`
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- backend-api-design
Research direction
Read flagsmith-core.ts around init and compare its trait normalization with identify() and setTraits(). The report says init wraps every trait in { value }, including TraitEvaluationContext objects; check the existing tests for initialization traits and add coverage for the transient form. Done when the identities request sends trait_value: true and transient: true for the reproduction input.
Written by the indexing model from the issue text.
Description
Description
flagsmith.init({ traits }) does not accept the { value, transient } form for traits, even though the current type of the option allows it (IInitConfig['traits'] is ITraits, and ITraits accepts TraitEvaluationContext) and the 5.0.0 changelog shows this exact call. Each trait is wrapped in another { value }, so the identities request sends a nested object as trait_value and no transient flag:
{ "trait_key": "foo", "trait_value": { "value": true, "transient": true } }
The request succeeds, but the API stores the nested object as the trait value and ignores transient, so the trait is persisted on the identity instead of being transient.
Expected
{ "trait_key": "foo", "trait_value": true, "transient": true }
This is what setTraits() sends for the same input, and what identify() sends according to the code.
Reproduction
Tested with [email protected] and @flagsmith/[email protected]. fetch is mocked to capture the request body.
import { createFlagsmithInstance } from 'flagsmith/isomorphic';
const bodies = [];
const fetch = async (url, opts) => {
if (opts?.body) bodies.push(JSON.parse(opts.body));
return { status: 200, ok: true, headers: { get: () => null },
text: async () => JSON.stringify({ flags: [], traits: [] }),
json: async () => ({ flags: [], traits: [] }) };
};
const fs = createFlagsmithInstance();
await fs.init({
environmentID: 'env',
fetch,
cacheFlags: false,
identity: 'user',
traits: { foo: { value: true, transient: true } },
});
console.log(JSON.stringify(bodies[0].traits));
Output:
init({ traits }) [{"trait_key":"foo","trait_value":{"value":true,"transient":true}}]
init({ evaluationContext }) [{"trait_key":"foo","trait_value":true,"transient":true}]
setTraits [{"trait_key":"foo","trait_value":true,"transient":true}]
Passing the same traits through evaluationContext.identity.traits, or through setTraits(), sends the correct body.
Cause
In init, every trait is wrapped unconditionally:
evaluationContext.identity = identity || traits ? {
identifier: identity,
traits: traits ? Object.fromEntries(
Object.entries(traits).map(
([tKey, tValue]) => [tKey, {value: tValue}]
)
) : {},
} : evaluationContext.identity;
identify() and setTraits() check for the object form first:
[tKey, isTraitEvaluationContext(tValue) ? tValue : {value: tValue}]
The request builder in getFlags then reads tContext?.value and tContext?.transient, so the double-wrapped object ends up in trait_value.
Suggested fix
Use the same normalization as the other methods, for example toTraitEvaluationContextObject(traits), in init.
Notes
IInitConfig['traits']is typed asITraits, which acceptsTraitEvaluationContext, so this compiles without a warning.
-- The 5.0.0 changelog also says these calls are deprecated in favour ofsetContext/evaluationContext. However, the current JS docs still documentinit({ traits })as a regular option ("Specifying traits will send the traits for that identity in the initial API call"), and it isn't marked@deprecatedin the code or types.setContext()beforeinitworks as a workaround, butinit({ traits })should either handle transient traits or reject them in the types.- I read the
initcode in 5.0.0, 7.0.0, 9.0.0, 9.0.1, 9.0.2, 9.3.2, 9.3.5, 10.0.0 and 12.6.0 and found the same line in every version. - Related: #421 widened the type of
identify()traits to accept this form. It didn't touchinit.
- Dominant language
- TypeScript
- Stars
- 67
- Forks
- 47
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 4
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Flagsmith/flagsmith-js-client
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Flagsmith/flagsmith-js-client#445 ·
-
Dependency DashboardOpen
Difficulty 1/5 Under an hour Newbie friendliness 20/100
Flagsmith/flagsmith-js-client#435 ·
-
Move module-scope Emitter in react.tsx into the ProviderPossibly taken @EngineerOnTravel claimed this 101 days ago. Openbug good first issue
Difficulty 3/5 1-2 days Newbie friendliness 58/100
Flagsmith/flagsmith-js-client#391 · 1 comment ·
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
Flagsmith/flagsmith-js-client#371 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Flagsmith/flagsmith-js-client#330 ·
All issues in Flagsmith/flagsmith-js-client
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
earendil-works/pi#12615 ·
Maintainers usually reply within 1 day
-
My Caps shows "Shared" on private Caps that were never sharedPossibly taken @AksharP5 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
CapSoftware/Cap#4569 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
OHDSI/Data2Evidence#3749 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Flagsmith/flagsmith-nodejs-client#332 ·
Maintainers usually reply within 2 days