Skip to content

fix: Keep variant keys, and send the whole variant on update - #127

Open
Anuragkumar-687 wants to merge 2 commits into
Flagsmith:mainfrom
Anuragkumar-687:fix/variant-keys
Open

Anuragkumar-687 wants to merge 2 commits into
Flagsmith:mainfrom
Anuragkumar-687:fix/variant-keys

Conversation

@Anuragkumar-687

Copy link
Copy Markdown

Closes #126. Closes #125.

feature create --variants drops each variant's key (#126). variantInput had no key field, and the API ignores key on variants nested in a feature create anyway: NestedMultivariateFeatureOptionSerializer marks it read-only, and it is only writable through the mv-options endpoint. Keyed variants are now added through mv-options once the feature exists; unkeyed ones are still sent nested, so nothing changes for them. If adding a keyed variant fails, the error says the feature was created and how to add the rest. CreateMVOption now sets feature in the body, which that endpoint requires (variant add already did this in the command).

feature variant update returns a 500 (#125). It sent only the changed fields, and the mv-options serializer rejects a partial PATCH unless the weight is included. It now sends the whole option with the change applied, so --key and --value updates work. A --value change replaces the old value fields rather than merging them.

Why it matters: exposures are attributed by variant key, so a multivariate flag created with the CLI could not be used in an Experimentation experiment.

The --variants help now lists the accepted fields: {value, weight, key}.

Tested

  • go test -race ./... passes. New cases: keyed inline variants go to mv-options exactly once; a key-only and a value-only variant update send the full option. The existing "update a variant by key" test asserted the partial body that causes the 500, so it now checks the full option instead.
  • Against self-hosted Flagsmith v2.295.2:
    flagsmith feature create customer_stats_read --value legacy --enabled \
      --variants '[{"key":"stats_table","value":"stats_table","weight":0}]' --jq '{type,variants}'
    # {"type":"multivariate","variants":[{"id":3,"key":"stats_table","value":"stats_table","weight":0}]}
    flagsmith feature variant update customer_stats_read 3 --key stats_tbl --yes
    # v2.0.0: 500 Internal Server Error    this branch: ✓ Updated variant
    An experiment on that flag then recorded exposures for both variants.

`feature create --variants` dropped each variant's `key`: `variantInput`
had no `key` field, and the API ignores `key` on variants nested in a
feature create anyway (it is read-only there and only writable through
the mv-options endpoint). Keyed variants are now added through
mv-options once the feature exists; unkeyed ones are still sent nested,
as before. `CreateMVOption` now sets `feature` in the body, which that
endpoint requires.

`feature variant update` sent only the changed fields, which the API's
mv-options serializer rejects with a 500 unless the weight is included.
It now sends the whole option with the change applied, so `--key` and
`--value` updates work.

Without keys, exposures can't be attributed to a variant, so a
multivariate flag created with the CLI could not be used in an
Experimentation experiment.

Closes Flagsmith#126
Closes Flagsmith#125
@Anuragkumar-687
Anuragkumar-687 requested a review from a team as a code owner November 28, 2026 15:19
@Anuragkumar-687
Anuragkumar-687 requested review from emyller and removed request for a team November 28, 2026 15:19

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files

Confidence score: 3/5

  • The feature create test in internal/cmd/cmd_test.go will fail for its keyed input because keyed variants are removed from multivariate_options. Expect zero nested options and keep the lastMVBody assertion to verify the keyed variant.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="internal/cmd/cmd_test.go">

<violation number="1" location="internal/cmd/cmd_test.go:6380">
P1: This expects one nested option, but `feature create` removes keyed variants from `multivariate_options`, so this test fails for its sole keyed input. Expect zero here; the `lastMVBody` assertion verifies the keyed variant was posted separately.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

Re-trigger cubic

Comment thread internal/cmd/cmd_test.go Outdated
Comment on lines +6380 to +6381
if opts, _ := f.lastFeatureBody["multivariate_options"].([]any); len(opts) != 1 {
t.Errorf("options = %+v, want the keyed variant created exactly once", opts)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: This expects one nested option, but feature create removes keyed variants from multivariate_options, so this test fails for its sole keyed input. Expect zero here; the lastMVBody assertion verifies the keyed variant was posted separately.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At internal/cmd/cmd_test.go, line 6380:

<comment>This expects one nested option, but `feature create` removes keyed variants from `multivariate_options`, so this test fails for its sole keyed input. Expect zero here; the `lastMVBody` assertion verifies the keyed variant was posted separately.</comment>

<file context>
@@ -6363,6 +6363,27 @@ func TestFeatureCreate(t *testing.T) {
+		}
+		// The API drops `key` on nested variants, so keyed ones go to mv-options,
+		// once each.
+		if opts, _ := f.lastFeatureBody["multivariate_options"].([]any); len(opts) != 1 {
+			t.Errorf("options = %+v, want the keyed variant created exactly once", opts)
+		}
</file context>
Suggested change
if opts, _ := f.lastFeatureBody["multivariate_options"].([]any); len(opts) != 1 {
t.Errorf("options = %+v, want the keyed variant created exactly once", opts)
if opts, _ := f.lastFeatureBody["multivariate_options"].([]any); len(opts) != 0 {
t.Errorf("options = %+v, want keyed variants sent only to mv-options", opts)
}

Anuragkumar-687 added a commit to Anuragkumar-687/flagway that referenced this pull request Nov 28, 2026
./flagway up and ./flagway app never returned when their output was
piped: the subshell that launched the server stayed alive and kept the
pipe open. The server now starts directly, with stdin from /dev/null.

The README links the upstream CLI fix (Flagsmith/flagsmith-cli#127).

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 issues found across 1 file (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval blocked by 1 unresolved issue from previous reviews.

Re-trigger cubic

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant