Skip to content

feat: Add payloadFilter option to request a filtered environment - #548

Open
abelonogov-ld wants to merge 7 commits into
v11from
andrey/payload-filter
Open

abelonogov-ld wants to merge 7 commits into
v11from
andrey/payload-filter

Conversation

@abelonogov-ld

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

Copy link
Copy Markdown
Contributor

Requirements

image
  • 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. Android counterpart: android-client-sdk#422.

Describe the solution you've provided

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

  • streaming (/meval, GET and REPORT)
  • polling (/msdk/evalx, GET and REPORT)

DarklyService validates the key once when it's created. A nil 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).

shouldGetReasons(url:) used to overwrite the URL's query items with withReasons=true. It's replaced by addingQueryItems(to:), which builds withReasons and filter together, so both are sent when both are configured.

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:

  • payloadFilter applies only to the primary environment.
  • addSecondaryEnvironment(_:mobileKey:payloadFilter:) adds a secondary environment, with an optional filter (nil means none). It throws LDInvalidArgumentError on the same name and key conflicts as setSecondaryMobileKeys(_:).
  • payloadFilter(forEnvironment:) returns an environment's filter, or nil for an unknown name.
  • Objective-C: addSecondaryEnvironment:mobileKey:payloadFilter:error: and payloadFilterForEnvironment:.
var config = LDConfig(mobileKey: "mob-primary", autoEnvAttributes: .enabled)
config.payloadFilter = "ios-flags"
try config.addSecondaryEnvironment("shared", mobileKey: "mob-a", payloadFilter: "ios-flags")
try config.addSecondaryEnvironment("other-project", mobileKey: "mob-b", payloadFilter: "mobile-flags")
try config.addSecondaryEnvironment("unfiltered", mobileKey: "mob-c")

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

LDClient.start sets each environment's filter on that environment's copy of the config, so the rest of the SDK is unchanged.

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

  • Validating in a property setter. logger may be assigned after payloadFilter, so the error would go to the default logger; validating in DarklyService uses the final config.
  • 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 [String: String] map of filters by environment name next to setSecondaryMobileKeys(_:), with payloadFilter as the default. The two maps can drift apart, a mistyped name silently falls back to another project's filter, and an empty string is needed to mean "no filter".

Additional context

Payload filters are an FDv1 feature, and this SDK uses only FDv1. With FDv2, LaunchDarkly filters flag data by the views linked to the mobile key ("custom payload") instead, and the FDv2 payload filter is being removed across SDKs (sdk-test-harness#431, go-server-sdk#440). A mobile key with a custom payload can't be used with this SDK yet: LaunchDarkly rejects FDv1 requests made with it with 422 (FDV2REL 1.1.1.3). The docs for payloadFilter and addSecondaryEnvironment say so. When the SDK gets FDv2, such a key will need no new API: it goes in mobileKey, with no payloadFilter.

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:

  • Polling (/msdk/evalx): the streamer drops the query string before it reaches the origin, so these responses are unfiltered.
  • 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 falls back to polling (or runs in the background, where iOS polls) may see the full flag set. The React Native SDK has the same limitation today.

I haven't run the contract test harness against this branch locally; the contract-test service builds. All 730 tests pass under swift test.


Note

Overview
Adds payloadFilter on LDConfig / ObjcLDConfig so flag streaming and polling requests can include a LaunchDarkly filter= query parameter. DarklyService validates keys against the spec regex (filtering-strict: invalid keys are dropped with an error log and the full environment is fetched) and builds query strings via addingQueryItems, combining withReasons and filter where both apply.

Secondary environments gain addSecondaryEnvironment(_:mobileKey:payloadFilter:), payloadFilter(forEnvironment:), and setSecondaryMobileKeys behavior that preserves per-environment filters when name and mobile key are unchanged. LDClient.start assigns each instance its environment-specific filter. Contract tests declare filtering / filtering-strict and pass harness streaming.filter / polling.filter through.

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

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

# Conflicts:
#	LaunchDarkly/LaunchDarkly/Networking/DarklyService.swift
#	LaunchDarkly/LaunchDarkly/ObjectiveC/ObjcLDConfig.swift
#	LaunchDarkly/LaunchDarklyTests/Networking/DarklyServiceSpec.swift
@abelonogov-ld
abelonogov-ld marked this pull request as ready for review November 20, 2026 21:22
@abelonogov-ld
abelonogov-ld requested a review from a team as a code owner November 20, 2026 21:22
@cursor cursor Bot added the risk:low label Nov 20, 2026

@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: low.

Automated risk review (dry run): Low

This PR would be automatically approved.

Why: Adds opt-in LDConfig.payloadFilter (and the ObjC wrapper) so DarklyService can append a validated filter query parameter on streaming and polling requests; default nil behavior is unchanged, and unit plus contract tests cover validation and URL construction.

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

@abelonogov-ld
abelonogov-ld enabled auto-merge (squash) November 20, 2026 22:02

@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. Cursor Bugbot completed with no findings, but Rule 2 (allowed PR authors) blocks approval because the author is not on the SDK allowlist, and the change is a new public SDK option that alters flag-request URLs. 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

…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 empty. Names that are not
secondary environments are logged when the SDK starts and ignored.

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. Rule 2 (allowed PR authors) blocks approval, and this new public payloadFilter option is above the low-risk threshold. 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

…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.
addSecondaryEnvironment throws on the same name and key conflicts as setSecondaryMobileKeys.
setSecondaryMobileKeys is unchanged apart from clearing filters of environments it replaces.

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. Rule 2 (allowed PR authors) blocks approval, and this public payload-filter API is above the low-risk threshold. 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

…ys need FDv2

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. Rule 2 (allowed PR authors) blocks approval, and this public payload-filter API is above the low-risk threshold. 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

@cursor cursor Bot added risk:medium and removed risk:low labels Nov 25, 2026

@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. The earlier dry-run Low / would-approve decision no longer applies.

Why: Later commits add public Swift and ObjC APIs (payloadFilter, addSecondaryEnvironment, per-environment filters on LDConfig / ObjcLDConfig / LDClient) that change which flag payloads streaming and polling requests receive, which is a meaningful SDK behavior change above Low.

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

… lists with the same key

Reading the keys with getSecondaryMobileKeys, adding one and setting them back dropped every
filter given to addSecondaryEnvironment, silently. setSecondaryMobileKeys now keeps the filter
of each environment it lists again under the same name and mobile key; a new name, or a name
given a different key, has none. A call that throws changes nothing.

LDConfig equality now compares the secondary mobile keys and their filters, which it did not
compare at all, so ObjcLDConfig.isEqual tells such configs apart too.

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. Rule 2 (allowed PR authors) blocks approval, and this public payload-filter API is above the low-risk threshold. 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.

1 participant