Skip to content

feat: add payloadFilter option to request a filtered environment - #422

Open
abelonogov-ld wants to merge 5 commits into
mainfrom
andrey/payload-filter
Open

abelonogov-ld wants to merge 5 commits into
mainfrom
andrey/payload-filter

Conversation

@abelonogov-ld

@abelonogov-ld abelonogov-ld commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

Mirrors payloadFilterKey in the React Native and browser SDKs (js-core#551) and follows the filtered environments SDK spec. iOS counterpart: ios-client-sdk#548.

Describe the solution you've provided

Adds LDConfig.Builder.payloadFilter(String). When set, the SDK adds filter=<key> to every FDv1 flag data request:

  • FDv1 streaming (/meval, GET and REPORT)
  • FDv1 polling (/msdk/evalx, GET and REPORT), including the FDv1 fallback synchronizer in the data system

FDv2 requests don't get the filter. With FDv2, LaunchDarkly filters flag data by the views linked to the mobile key ("custom payload"), so an app picks a filtered payload by picking a mobile key, with no SDK option. The FDv2 payload filter is being removed across SDKs: the harness dropped its FDv2 filter scenarios (sdk-test-harness#431), and Go and Java deprecated it (go-server-sdk#440). When a filter is combined with dataSystem(...), build() logs a warning for each environment that has one.

The two cases use the same API:

// FDv1 payload filter: a normal mobile key plus a filter key
.secondaryEnvironment("search", "mob-normal", "search-flags")
// FDv2 view filtering: the mobile key itself has a custom payload
.secondaryEnvironment("checkout", "mob-with-views", null)

A mobile key with a custom payload works only with FDv2. LaunchDarkly rejects FDv1 requests made with it with 422 (FDV2REL 1.1.1.3). For the same reason, the Relay Proxy spec forbids sending the FDv1 fallback directive for such a key (1.1.1.6).

The key is checked when the config is built. A null or empty key means no filter. A key that doesn't match ^[a-zA-Z0-9][._\-a-zA-Z0-9]*$ is dropped with an error log, and the full environment is requested, as the spec requires (filtering-strict behavior).

Payload filters belong to a project, and secondary mobile keys may be in other projects, so a secondary environment's filter is given together with its mobile key:

  • Builder.payloadFilter(String) applies only to the primary environment.
  • Builder.secondaryEnvironment(String name, String mobileKey, String payloadFilter) adds a secondary environment, with an optional filter (null means none). It throws IllegalArgumentException on the same name and key conflicts as secondaryMobileKeys(Map).
  • LDConfig.getPayloadFilter(String environmentName) returns an environment's filter, or null for an unknown name.
new LDConfig.Builder(AutoEnvAttributes.Enabled)
        .mobileKey("mob-primary")
        .payloadFilter("android-flags")
        .secondaryEnvironment("shared", "mob-a", "android-flags")
        .secondaryEnvironment("other-project", "mob-b", "mobile-flags")
        .secondaryEnvironment("unfiltered", "mob-c", null)
        .build();

Apps that don't use filters are unaffected: secondaryMobileKeys(Map) is unchanged and its environments have no filter. Calling it replaces every secondary environment, including ones added with a filter.

build() validates every environment's filter once. Data sources look it up with ClientContext.getEnvironmentName(), so the value reaches FDv1 data sources through ClientContext.getConfig() and the FDv1 fallback synchronizer through the internal DataSourceBuildInputsInternal, with no changes to the public ClientContext or DataSourceBuildInputs APIs.

The contract-test service now maps the harness's streaming.filter / polling.filter to the new option and declares the filtering and filtering-strict capabilities.

Describe alternatives you've considered

  • Putting the option on StreamingDataSourceBuilder / PollingDataSourceBuilder, as the Java server SDK does. A single top-level option matches React Native and covers the FDv2 data system without changes to each builder.
  • Sending invalid keys as-is (React Native's current behavior). Rejected in favor of the spec.
  • One filter for all environments. React Native has one client per key, so it never has this problem; here a filter from one project would be sent to environments in other projects.
  • A Map<String, String> of filters by environment name next to secondaryMobileKeys(Map), with payloadFilter as the default. The two maps can drift apart, and a mistyped name silently falls back to another project's filter.
  • Sending the filter on FDv2 requests too. The FDv2 streaming and polling endpoints ignore it, and the FDv2 filter surface is being removed across SDKs.
  • A separate option for FDv2 views. Views are attached to the key in LaunchDarkly, not chosen per request, so there is nothing for the SDK to send.

Additional context

This PR only sends the parameter; the SDK does no filtering of its own and doesn't change how it picks streaming or polling. The server doesn't currently apply the filter on every mobile endpoint:

  • FDv1 polling (/msdk/evalx): the streamer drops the query string before it reaches the origin, so these responses are unfiltered.
  • FDv1 streaming (/meval): the filter is forwarded and applied to the initial put when the payload-filter-gonfalon-sdk-endpoints flag is on. Live updates are only routed by filter when payload-filter-streamer-updates is on.

So an app that switches between streaming and polling may see the full flag set after a poll. The React Native SDK has the same limitation today.

I haven't run the contract test harness against this branch locally. All 782 unit tests pass.


Note

Overview
Adds LDConfig.Builder.payloadFilter(String) so apps can request a LaunchDarkly filtered environment. When set and valid, the SDK appends filter=<key> on flag-data HTTP calls across FDv1 streaming and polling (GET/REPORT) and FDv2 streaming and polling (including FDv1 fallback in the data system).

Invalid or empty keys are normalized at build(): empty becomes no filter; keys outside ^[a-zA-Z0-9][._\-a-zA-Z0-9]*$ are dropped with an error log and the SDK requests the full environment (filtering-strict).

The option is threaded through LDUtil, DataSourceBuildInputsInternal, and existing request builders without changing public ClientContext / DataSourceBuildInputs APIs. Contract tests accept harness streaming.filter / polling.filter, map them to payloadFilter, and advertise filtering and filtering-strict capabilities.

Reviewed by Cursor Bugbot for commit 71d96b6. Bugbot is set up for automated code reviews on this repo. Configure here.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor 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.

Risk: medium.

Automated risk review (dry run): Medium

This PR would not be automatically approved. Human approval required.

Why: This adds a public LDConfig.Builder.payloadFilter option that changes flag-data request URLs across FDv1/FDv2 streaming and polling (StreamingDataSource, HttpFeatureFlagFetcher, FDv2StreamingSynchronizer, DataSystemComponents). That is a meaningful SDK behavior change spanning several data-source paths, so it is Medium even with unit tests and a clean Bugbot check.

If you disagree with this review, share this PR in #feedback-agent-approvals or submit a changes-requested review explaining why.

Open in Web View Automation 

Sent by Cursor Approval Agent: Dry-Run PR Approver

Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	contract-tests/src/main/java/com/launchdarkly/sdktest/TestService.java
#	launchdarkly-android-client-sdk/src/test/java/com/launchdarkly/sdk/android/DataSystemComponentsTest.java
@Nullable
private final FDv2Requestor requestor;
private final boolean evaluationReasons;
private final String payloadFilter;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we've been actively deprecating and removing payload filters from FDv2. either we decide to reverse course on that work, or we should leave payload filtering to FDv1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agreed. payloadFilters cannot be added to FDv2 sources as it is deprecated.

…ry environment

payloadFilter stays the filter for the primary environment and the default for every
secondary environment. A secondary environment named in secondaryPayloadFilters uses its
own key instead, or requests the full environment if the key is null or empty. Names that
are not secondary environments are logged when the config is built and ignored.

Data sources look up their environment's filter by ClientContext.getEnvironmentName().

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor 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.

Stale comment

Risk: medium. Not approved under Rule 2 (allowed PR authors): this author is not on the SDK approval list, and the change is above the low-risk threshold. Cursor Bugbot passed with no findings; human review is needed and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver

…nt(name, mobileKey, payloadFilter)

A secondary environment's payload filter is now given together with its mobile key, so it
can't name an unknown environment or drift from the keys. payloadFilter applies only to the
primary environment; secondary environments are unfiltered unless added with a filter.
secondaryEnvironment throws IllegalArgumentException on the same name and key conflicts as
secondaryMobileKeys. getPayloadFilter(String) returns null for unknown environment names.

@cursor cursor 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.

Risk: medium. Not approved under Rule 2 (allowed PR authors): this author is not on the SDK approval list, and the change is above the low-risk threshold. Cursor Bugbot passed with no findings; human review is needed and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver

FDv2 filters flag data by the views linked to the mobile key, and
LaunchDarkly no longer supports payload filters on FDv2 requests. Stop
sending the filter from the FDv2 streaming and polling synchronizers,
keep it on the FDv1 fallback, and warn when a filter is combined with
the data system.

Co-authored-by: Cursor <cursoragent@cursor.com>

@cursor cursor 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.

Risk: medium. Not approved under Rule 2 (allowed PR authors): this author is not on the SDK approval list, and the change is above the low-risk threshold. Cursor Bugbot passed with no findings; human review is needed and no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants