Skip to content

fix(sdk): the Android SDK sends the user's attributes when it evaluates a flag - #2074

Open
amarkanday wants to merge 3 commits into
mainfrom
fix/android-sdk-flag-attributes
Open

amarkanday wants to merge 3 commits into
mainfrom
fix/android-sdk-flag-attributes

Conversation

@amarkanday

Copy link
Copy Markdown
Contributor

Part of #438

What changed

The Android SDK now sends the user's attributes when it evaluates a flag: GET /api/v1/feature-flags/evaluate/{key}?user_id=...&context=<percent-encoded compact JSON>, context after user_id. Before, the attributes were accepted and dropped, so a targeting rule on a user attribute never matched. A call with no attributes sends the same bytes as before. The shared docs and claims now say only Flutter sends just the user id.

It was built on the iOS change (#2045) and the #1747 fix (#2046), which have both merged. Its three commits are b88ec5b1 (SDK code and tests), 8de12031 (docs, claims registries, the backend gates, the video description) and 91adb5da (the SDK's errors drop every form of the query). Their 18 files (924 insertions, 80 deletions) are:

  • sdk/android/sdk/src/main/kotlin/com/controlvar/android/ExperimentationClient.kt, Cache.kt, HttpClient.kt
  • sdk/android/sdk/src/test/kotlin/ExperimentationClientTest.kt
  • docs/sdk/android.md, sdk/android/README.md
  • the four shared pages docs/getting-started/limitations.md, docs/guides/guided-examples.md, demo/DEMO_GUIDE.md, demo/streampulse/README.md, and their four docs/_claims/**.toml registries
  • backend/tests/unit/api/test_documented_limits.py, backend/tests/unit/docs/test_deferred_sdk_pages.py, backend/tests/unit/showcase/test_site_walkthroughs.py
  • frontend/src/content/videos.json (video 4's description)

OfflineStore.kt is untouched, and #1747's catch line and its 404 branch are unchanged. The choices, copied from the iOS pattern:

  1. user_id first, then context, in evaluateFlagPath; the context value goes through OkHttp addQueryParameter (no new encoder).
  2. Cache slot: CacheEntry gains context (default ""); get(key, context?) is a new signature beside the old one (null means no check; a mismatch keeps the entry), and so is set(key, value, context). Flag evaluation always passes its context. The old get and set signatures are kept. Android has no in-flight dedupe, so there is no dedupe key.
  3. The offline key is unchanged; the stored answer is for any attributes and is read only after a non-404 failure.
  4. Key order: the attributes are written with every object's keys sorted, recursively (the same value writer as the assign body), because org.json order differs between the JVM test library and a device. The same attributes in another order share one cache slot and are sent once.
  5. Attributes that cannot be sent: a NaN or an infinity at any depth throws IllegalArgumentException("user.attributes cannot be sent as JSON") before the cache, the store and the request. Any other value (a Date, say) goes as its toString(), as it does for assignment.
  6. Errors name no query: the SDK's message is cut at the query in every form (the encoded query, the decoded query, the context value alone); the underlying cause is kept unless some message in its chain repeats the query, in which case it is dropped.
  7. Decode helpers serverQuery(path) and serverContext(path) split on &, then on the first =, use URLDecoder.decode(s, UTF_8) (+ is a space) and parse the JSON.

How it was verified

On the three commits as replayed onto main: the 23 backend unit files that name Android, 1743 passed and 15 skipped; python scripts/doc_claims.py check: problems 0; ruff clean; DCO: all 3 commits carry a matching sign-off; the secret scan over the diff: no findings. Putting android back in the "does not send" table fails test_every_sdk_directory_is_classified_by_whether_flag_evaluation_sends_attributes. In 16 of the 18 files the change is byte-identical to the reviewed commits' change. In the two backend test files it is the same change beside main's moves of the Ruby, PHP, Edge and Elixir SDKs (#2035, #2037, #2043, #2044): they had already left the same tables, and the Android section now follows the sections they added.

Measured on the reviewed commits before that replay, with the same SDK code:

  • JVM leg as CI runs it (sdk/android/jvm, ./mvnw clean test, JDK 21): 109 tests, 0 failures (base 81 including Android SDK: a stored offline answer is still served after the flag is deleted (404) #1747's three, so +28).
  • test_documented_limits.py, test_deferred_sdk_pages.py and backend/tests/unit/showcase: 2212 passed. python scripts/doc_claims.py check: problems 0. mkdocs build --strict: exit 0. The secret scan over origin/main..HEAD: no findings. DCO: every commit carried a matching sign-off.
  • On the docs commit: test_docs_only_gate.py 47 passed; ruff clean on the three touched Python files; the frozen check (no modules/ path, no pyproject.toml) holds. Name-only diff against backend/ modules/ frontend/ pyproject.toml lists exactly the three backend test files and videos.json.
  • Old code (base ExperimentationClient.kt and HttpClient.kt, the new Cache.kt kept so the new get and set compile), on the docs commit: 108 run, 13 failures and 2 errors (the reviewer's run: the same).
  • Plants, each restored: context unencoded fails 4; + left raw fails 3; & left raw fails 3 and 1 error (reviewer); the error-form plants for withoutQuery and repeatsQuery each fail the new test in the third commit; the claims, cache, offline, sorting and unencodable plants each fail at least one test (on the docs commit). Video 4: the iOS wording put back fails test_video_4s_native_sdk_sentence_is_the_documented_limitation (1 failed, 57 passed).
  • Review: the first two commits were approved with optional nits. The third commit implements the two error-message nits and the page wrap nits; it had no code review of its own.
  • The wide DB-free sweep (pytest backend/tests/unit backend/tests/smoke) was started on the docs commit but its result was not kept, so no classification is claimed here. On the other branches the same sweep gave 168 PostgreSQL errors and the 2 pep440 failures, identical on main.

Checklist:

  • Tests run locally and green (the JVM leg, the backend suites above)
  • make lint clean: not run as a whole; ruff clean on the touched Python files; CI's Frontend Tests must show success at the final head before merge

Not verified: the Gradle :sdk:testDebugUnitTest leg (CI only); a device's org.json (insertion order, \/); the real server route; failure messages beyond the five measured (refused, dropped, unknown host, timeout, bad URL), for example TLS and proxy; a self-containing attribute map (assignment has the same limit); the frontend suite.

Regression test

  • This is a bug fix, and it adds a test that fails without the fix:
    sdk/android/sdk/src/test/kotlin/ExperimentationClientTest.kt -- regression 438 - evaluateFlag sends the attributes as context after user_id and regression 438 - a targeting rule on attributes matches (both @Tag("regression"))
  • Not a bug fix

On the base ExperimentationClient.kt the first fails with expected ...?user_id=u1&context=%7B%22plan%22%3A%22pro%22%7D but was ...?user_id=u1, the second with "the rule plan == pro must match" (expected true, was false) (measured on the docs commit).

Risk and rollback

This changes the SDK's request to the public flag-evaluation route for callers that pass attributes; callers that pass none send the same request as before. It also changes what Android's network errors say (the query is cut). If it is wrong, flag calls with attributes answer from the wrong context or fail; revert this PR to undo it. Nothing changes on the server, and no release is cut here. It also edits public docs and video 4's description, which go with the revert.

  • No migration, or every migration here is expand-only

🤖 Generated with Claude Code

https://claude.ai/code/session_01NpxPyav9uBxNuiXsDvD8qn

amarkanday and others added 3 commits November 19, 2026 16:52
…es a flag

Part of #438.

evaluateFlag sent only user_id, so a flag rule on attributes could not match
from Android. It now sends user.attributes as context, after user_id, written
as compact JSON with the keys of every map sorted and percent-encoded by
OkHttp's addQueryParameter, the encoder user_id already uses. With no
attributes or an empty map the request is the one sent before.

- ResultCache's entry remembers the context it was answered for; the new
  get(key, context) returns it only for the same context, and the old get and
  set are unchanged. Flag evaluation always passes its context ("" for none).
- The offline store key stays user + flag.
- A NaN or an infinite number at any depth throws IllegalArgumentException
  ("user.attributes cannot be sent as JSON") before the cache, the store and
  the request. Any other value is written as the assignment writes it.
- HttpClient's network error message is cut at the request's query, and its
  cause is kept only when no message in its chain repeats the query.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011kqtSDC7sd5ue3m6LuwdXV
Signed-off-by: amarkanday <a.markanday@gmail.com>
…tributes

Part of #438.

The Android SDK page and README replace the "sends only the user id" note and
add the cache, offline and unencodable sentences (the offline paragraph from
the 404 change gains "whatever attributes it was asked with"). The shared
pages, their claims units and video 4's description name Flutter as the only
native SDK that still sends the user id alone.

- test_documented_limits.py: Android moves from DOES_NOT_SEND to SENDS with
  its marker and leaves NATIVE_SDKS.
- test_deferred_sdk_pages.py: Android leaves SENDS_NO_CONTEXT; nine sentences
  are pinned to the JUnit tests that hold them, and the old note must be gone.
- test_site_walkthroughs.py and videos.json: the description ends "; Flutter
  sends only the user id to flags." and the row opens "Flutter sends only the
  user id on flag evaluation, so rules on attributes do not match".

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011kqtSDC7sd5ue3m6LuwdXV
Signed-off-by: amarkanday <a.markanday@gmail.com>
Part of #438.

HttpClient now treats the request's query and its context value, each encoded
and decoded, as one list. withoutQuery cuts every form from the SDK's own
network error message, and repeatsQuery drops the cause when any message in
its chain repeats one of them (the encoded context value alone included).
Empty forms are left out so nothing matches everything.

A new test injects transport messages that carry the decoded query, the
decoded context value and the encoded context value alone; the SDK message
holds no canary, "context=" or "%7B", and the cause is dropped. The
android.md cache paragraph is re-wrapped.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011kqtSDC7sd5ue3m6LuwdXV
Signed-off-by: amarkanday <a.markanday@gmail.com>
@amarkanday amarkanday added the bug Something isn't working label Nov 20, 2026

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

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant