diff --git a/lib/ldclient-openfeature/impl/context_converter.rb b/lib/ldclient-openfeature/impl/context_converter.rb index e6621b9..5573f60 100644 --- a/lib/ldclient-openfeature/impl/context_converter.rb +++ b/lib/ldclient-openfeature/impl/context_converter.rb @@ -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) diff --git a/spec/impl/context_converter_spec.rb b/spec/impl/context_converter_spec.rb index dade1f1..e283bf6 100644 --- a/spec/impl/context_converter_spec.rb +++ b/spec/impl/context_converter_spec.rb @@ -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",