`init({ traits })` double-wraps `{ value, transient }` trait objects, sending a nested `trait_value`
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- typescript
調査の方向性
flagsmith-core.ts の init 周辺を読み、trait の正規化を identify() および setTraits() と比較してください。報告によると、init は TraitEvaluationContext オブジェクトを含むすべての trait を { value } でラップします。初期化時の 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時間
- マージ済み PR(30日)
- 4
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Flagsmith/flagsmith-js-client のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
Flagsmith/flagsmith-js-client#445 ·
-
Dependency Dashboardオープン
難易度 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
-
ui
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
mermaid-js/mermaid-v13#1370 ·
メンテナーはふだん 1 日以内に返信
-
compiler: the island_chunks_not_emitted report gives a count but not which islands were skippedオープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
-
Broken Website
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
darkreader/darkreader#16259 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
メンテナーはふだん 1 日以内に返信