`init({ traits })` double-wraps `{ value, transient }` trait objects, sending a nested `trait_value`
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 82/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- typescript
调研方向
阅读 flagsmith-core.ts 中 init 附近的代码,并将其 trait 规范化方式与 identify() 和 setTraits() 进行比较。报告称 init 会将每个 trait 包装在 { value } 中,包括 TraitEvaluationContext 对象;检查现有的初始化 trait 测试,并为 transient 形式添加覆盖。对于复现输入,identities 请求发送 trait_value: true 和 transient: true 时即为完成。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- TypeScript
- 星标
- 67
- 派生
- 47
- 平均合并
- 1 天 5 小时
- 30 天内合并 PR
- 4
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Flagsmith/flagsmith-js-client 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 48/100
Flagsmith/flagsmith-js-client#445 ·
-
难度 1/5 1 小时以内 新手友好度 20/100
Flagsmith/flagsmith-js-client#435 ·
-
Move module-scope Emitter in react.tsx into the Provider可能已有人在做 @EngineerOnTravel 于 101 天前认领。 未关闭bug good first issue
难度 3/5 1-2 天 新手友好度 58/100
Flagsmith/flagsmith-js-client#391 · 1 条评论 ·
-
enhancement good first issue
难度 2/5 1-3 小时 新手友好度 55/100
Flagsmith/flagsmith-js-client#371 ·
-
难度 4/5 3-5 天 新手友好度 35/100
Flagsmith/flagsmith-js-client#330 ·
查看 Flagsmith/flagsmith-js-client 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 67/100
knaw-huc/textannoviz#742 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
cloudflare/realtimekit-ui#219 ·
维护者通常 1 天内回复
-
ui
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
tui: Mermaid renderer displays <b>/<strong> tags literally and ignores \n line breaks可能已有人在做 @argszero 今天认领。 未关闭area/tui platform/wsl status/repro-simulated type/bug version/v2
难度 2/5 1-3 小时 新手友好度 82/100
anomalyco/opencode#66773 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
mermaid-js/mermaid-v13#1370 ·
维护者通常 1 天内回复