Repository navigation
feat: add payloadFilter option to request a filtered environment - #422
abelonogov-ld wants to merge 5 commits into
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
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.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
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.
There was a problem hiding this 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.
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>
There was a problem hiding this 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.
Sent by Cursor Approval Agent: SDK - Pull Request Router and Approver
| */ | ||
| public Builder secondaryEnvironment(String name, String mobileKey, String payloadFilter) { | ||
| if (primaryEnvironmentName.equals(name)) { | ||
| throw new IllegalArgumentException("The primary environment name is not a valid key."); |
There was a problem hiding this comment.
This error message isn't necessarily true. A name collision doesn't have anything to do with validity.
| throw new IllegalArgumentException("A secondary environment with this name already exists."); | ||
| } | ||
| if (mobileKey != null && mobileKey.equals(this.mobileKey)) { | ||
| throw new IllegalArgumentException("The primary environment key cannot be in the secondary mobile keys."); |
There was a problem hiding this comment.
This error message does not match the condition. The condition doesn't check the contents of secondary mobile keys.
| * @return the builder | ||
| * @throws IllegalArgumentException if the name is the primary environment name or is already | ||
| * used, or if the mobile key is the primary key or is already used | ||
| */ |
There was a problem hiding this comment.
Consider an environment builder like this to eliminate the parameter blow up as more configuration options are added.
new LDConfig.Builder(AutoEnvAttributes.Enabled)
.mobileKey("primary-key")
.secondaryEnvironment("other",
new LDConfig.SecondaryEnvironmentBuilder("other-mobile-key")
.payloadFilter("my-filter"))
.build();
I suppose it would also be possible to do something like
new LDConfig.Builder(AutoEnvAttributes.Enabled)
.primaryEnvironment("primary",
new LDConfig.SecondaryEnvironmentBuilder("primary-mobile-key")
.payloadFilter("my-filter"))
.build();
| return null; | ||
| } | ||
| if (!PAYLOAD_FILTER_PATTERN.matcher(payloadFilter).matches()) { | ||
| logger.error("Payload filter \"{}\" is invalid; the full \"{}\" environment will be requested instead", |
There was a problem hiding this comment.
| logger.error("Payload filter \"{}\" is invalid; the full \"{}\" environment will be requested instead", | |
| logger.error("Payload filter \"{}\" is invalid; \"{}\" environment will be used without payload filter.", |
Rationale: no reason to talk about what a "full" environment is, and no reason to talk about requests.
| return payloadFilter; | ||
| } | ||
|
|
||
| private Map<String, String> validPayloadFilters(Map<String, String> mobileKeys, LDLogger logger) { |
There was a problem hiding this comment.
Probably worth commenting on this private functions about the dropping of invalid pieces of data.
| return LDTimberLogging.adapter(); | ||
| } | ||
|
|
||
| private static String validPayloadFilterOrNull(String payloadFilter, String environmentName, LDLogger logger) { |
There was a problem hiding this comment.
Probably worth commenting on this private functions about the dropping of invalid pieces of data.
| logger.warn("Payload filter \"{}\" for the \"{}\" environment applies only to FDv1 fallback requests;" | ||
| + " FDv2 requests receive the flags in the views linked to the mobile key", | ||
| filter, environmentName); | ||
| } |
There was a problem hiding this comment.
Isn't it a problem if we use different filtering between regular initializers / synchronizers and the fallback? All of a sudden when you fallback you will get a different set of flags. I suppose there is already precedent for this in other SDKs that support filters and the data system?
There was a problem hiding this comment.
My thoughts: When dataSystem(...) is configured, don't pass the filter to the FDv1 fallback either. Log a warning that the payload filter is discarded. I have to think more on if provide a log guiding the customer to consider using views makes sense or not. It may not make sense until after we deprecate/remove the FDv1 datasources / FDv1 fallback.
There was a problem hiding this comment.
Going to discuss with team today at tech meeting. I'm seeing some SDK implementations that are removing the filter even from the fallback when the data system is used. Will respond here when I have that answer.
There was a problem hiding this comment.
From sdk team discussion:
It should never be sent when using the FDv2 datasystem (including the FDv1 fallback).
For SDKs that accept receiving the payload filter but aren’t going to be used in FDv2, we should log a warning for the customer so they are aware.


Requirements
Related issues
Mirrors
payloadFilterKeyin 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 addsfilter=<key>to every FDv1 flag data request:/meval, GET and REPORT)/msdk/evalx, GET and REPORT), including the FDv1 fallback synchronizer in the data systemFDv2 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:
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-strictbehavior).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 throwsIllegalArgumentExceptionon the same name and key conflicts assecondaryMobileKeys(Map).LDConfig.getPayloadFilter(String environmentName)returns an environment's filter, or null for an unknown name.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 withClientContext.getEnvironmentName(), so the value reaches FDv1 data sources throughClientContext.getConfig()and the FDv1 fallback synchronizer through the internalDataSourceBuildInputsInternal, with no changes to the publicClientContextorDataSourceBuildInputsAPIs.The contract-test service now maps the harness's
streaming.filter/polling.filterto the new option and declares thefilteringandfiltering-strictcapabilities.Describe alternatives you've considered
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.Map<String, String>of filters by environment name next tosecondaryMobileKeys(Map), withpayloadFilteras the default. The two maps can drift apart, and a mistyped name silently falls back to another project's filter.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:
/msdk/evalx): the streamer drops the query string before it reaches the origin, so these responses are unfiltered./meval): the filter is forwarded and applied to the initialputwhen thepayload-filter-gonfalon-sdk-endpointsflag is on. Live updates are only routed by filter whenpayload-filter-streamer-updatesis 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 appendsfilter=<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 publicClientContext/DataSourceBuildInputsAPIs. Contract tests accept harnessstreaming.filter/polling.filter, map them topayloadFilter, and advertisefilteringandfiltering-strictcapabilities.Reviewed by Cursor Bugbot for commit 71d96b6. Bugbot is set up for automated code reviews on this repo. Configure here.