`init({ traits })` double-wraps `{ value, transient }` trait objects, sending a nested `trait_value`

Đang mở Phù hợp với người mới
#444 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Đọc flagsmith-core.ts quanh init và so sánh cách chuẩn hóa trait với identify() và setTraits(). Báo cáo cho biết init bọc mọi trait trong { value }, bao gồm cả các đối tượng TraitEvaluationContext; hãy kiểm tra các bài kiểm thử hiện có cho trait khởi tạo và bổ sung kiểm thử cho dạng transient. Hoàn tất khi yêu cầu identities gửi trait_value: true và transient: true cho dữ liệu đầu vào tái hiện lỗi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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 as ITraits, which accepts TraitEvaluationContext, so this compiles without a warning.
    -- The 5.0.0 changelog also says these calls are deprecated in favour of setContext / evaluationContext. However, the current JS docs still document init({ traits }) as a regular option ("Specifying traits will send the traits for that identity in the initial API call"), and it isn't marked @deprecated in the code or types. setContext() before init works as a workaround, but init({ traits }) should either handle transient traits or reject them in the types.
  • I read the init code 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 touch init.
Ngôn ngữ chính
TypeScript
Star
67
Fork
47
Merge trung bình
1 ngày 5 giờ
Pull request đã merge (30 ngày)
4

Chuẩn bị môi trường

  • Không có Dockerfile hay tệp Docker Compose
  • Có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Flagsmith/flagsmith-js-client

Tất cả issue của Flagsmith/flagsmith-js-client

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.