Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/ldclient-openfeature/impl/context_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ def to_ld_context(context)
next
end

key = attributes.fetch(:key, nil)
targeting_key = attributes.fetch(:targeting_key, nil)

next unless targeting_key.nil? || targeting_key.is_a?(String)
attributes = attributes.transform_keys(&:to_s)
key = attributes.fetch('key', nil)
targeting_key = attributes.fetch('targeting_key', nil)

targeting_key = get_targeting_key(targeting_key, key)
single_context = build_single_context(attributes, kind, targeting_key)
Expand Down
61 changes: 61 additions & 0 deletions spec/impl/context_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,67 @@
end

describe "multi kind context" do
[true, false].each do |string_keys|
["user", "multi", 42].each do |nested_kind|
it "ignores nested kind #{nested_kind.inspect} in #{string_keys ? 'string' : 'symbol'}-keyed attributes" do
attributes = { key: "org-key", kind: nested_kind, tier: "premium" }
attributes = attributes.transform_keys(&:to_s) if string_keys
context = OpenFeature::SDK::EvaluationContext.new(kind: "multi", org: attributes, user: { key: "user-key" })

ld_context = context_converter.to_ld_context(context)

expect(ld_context.valid?).to be(true)
expect(ld_context.multi_kind?).to be(true)
expect(ld_context.individual_context_count).to eq(2)

{ "org" => "org-key", "user" => "user-key" }.each do |kind, key|
individual_context = ld_context.individual_context(kind)
expect(individual_context).not_to be_nil
expect(individual_context.kind).to eq(kind)
expect(individual_context.key).to eq(key)
expect(individual_context.get_custom_attribute_names).not_to include(:kind, "kind")
end

expect(ld_context.individual_context("org")[:tier]).to eq("premium")
end
end
end

[true, false].each do |string_keys|
it "converts #{string_keys ? 'string' : 'symbol'}-keyed attributes without mutating them" do
attributes = {
key: "fallback",
targeting_key: "user-key",
name: 30,
anonymous: "invalid",
privateAttributes: ["email"],
email: "sandy@example.com",
}
attributes = attributes.transform_keys(&:to_s) if string_keys
attributes.freeze
context = OpenFeature::SDK::EvaluationContext.new(kind: "multi", user: attributes, org: { key: "org-key" })

ld_context = context_converter.to_ld_context(context).individual_context("user")

expect(ld_context.key).to eq("user-key")
expect(ld_context[:name]).to be_nil
expect(ld_context[:anonymous]).to be(false)
expect(ld_context[:email]).to eq("sandy@example.com")
expect(ld_context.private_attributes).to eq([LaunchDarkly::Reference.create("email")])
expect(ld_context[:targeting_key]).to be_nil
expect(log_output.string).to include("both a 'key' and 'targeting_key'")
end
end

it "preserves an invalid nested targeting key as an invalid context rather than dropping its kind" do
context = OpenFeature::SDK::EvaluationContext.new(kind: "multi", user: { targeting_key: 42 }, org: { key: "org-key" })

ld_context = context_converter.to_ld_context(context)

expect(ld_context.valid?).to be(false)
expect(log_output.string).to include("must contain either a 'targeting_key' or a 'key'")
end

it "can create multi kind context" do
attributes = {
kind: "multi",
Expand Down
Loading