diff --git a/lib/ldclient-openfeature/impl/context_converter.rb b/lib/ldclient-openfeature/impl/context_converter.rb index e6621b9..119e6b2 100644 --- a/lib/ldclient-openfeature/impl/context_converter.rb +++ b/lib/ldclient-openfeature/impl/context_converter.rb @@ -2,6 +2,8 @@ require 'ldclient-rb' require 'open_feature/sdk' +require 'date' +require 'time' module LaunchDarkly module OpenFeature @@ -135,12 +137,36 @@ def to_ld_context(context) elsif k == 'privateAttributes' @logger.error("The attribute 'privateAttributes' must be an array") else - context[k.to_sym] = v + context[k.to_sym] = convert_value(v) end end LaunchDarkly::LDContext.create(context) end + + # + # @param value [any] + # + # @return [any] a JSON-compatible LaunchDarkly context attribute value + # + private def convert_value(value) + case value + when NilClass, TrueClass, FalseClass, String, Integer, Float + value + when Time, DateTime + utc_time = value.to_time.getutc + utc_time.iso8601(utc_time.nsec.zero? ? 0 : 9) + when Date + Time.utc(value.year, value.month, value.day).iso8601 + when Array + value.map { |item| convert_value(item) } + when Hash + value.transform_values { |item| convert_value(item) } + else + @logger.error("An evaluation context attribute of type #{value.class} cannot be converted; using null") + nil + end + end end end end diff --git a/spec/impl/context_converter_spec.rb b/spec/impl/context_converter_spec.rb index dade1f1..e0df68a 100644 --- a/spec/impl/context_converter_spec.rb +++ b/spec/impl/context_converter_spec.rb @@ -114,6 +114,57 @@ end end + describe "attribute value conversion" do + it "converts instants to UTC without mutating frozen input values" do + time = Time.new(2026, 11, 16, 9, 30, 0, "+02:00").freeze + date_time = DateTime.new(2026, 11, 16, 9, 30, 0, "+02:00").freeze + values = { "time" => time, "date_time" => date_time, "date" => Date.new(2026, 11, 16) }.freeze + context = OpenFeature::SDK::EvaluationContext.new(key: "user-key", values: values) + + ld_context = context_converter.to_ld_context(context) + + expect(ld_context[:values]).to eq({ + "time" => "2026-11-16T07:30:00Z", + "date_time" => "2026-11-16T07:30:00Z", + "date" => "2026-11-16T00:00:00Z", + }) + expect(time.utc_offset).to eq(7200) + expect(values["time"]).to equal(time) + end + + it "recursively converts list and structure values and replaces unsupported values with null" do + values = [nil, true, false, 42, 1.5, "text", { "nested" => [Time.utc(2026, 11, 16), Object.new] }].freeze + context = OpenFeature::SDK::EvaluationContext.new(key: "user-key", values: values, unsupported: Object.new) + + ld_context = context_converter.to_ld_context(context) + + expect(ld_context[:values]).to eq([nil, true, false, 42, 1.5, "text", { "nested" => ["2026-11-16T00:00:00Z", nil] }]) + expect(ld_context[:unsupported]).to be_nil + expect(log_output.string).to include("ERROR", "cannot be converted; using null") + expect(values.last["nested"].first).to be_a(Time) + end + + it "preserves fractional seconds in instants used for targeting" do + time = Time.utc(2026, 11, 16, 0, 0, Rational(3, 2)) + context = OpenFeature::SDK::EvaluationContext.new(key: "user-key", time: time, date_time: time.to_datetime) + + ld_context = context_converter.to_ld_context(context) + + expect(ld_context[:time]).to eq("2026-11-16T00:00:01.500000000Z") + expect(ld_context[:date_time]).to eq("2026-11-16T00:00:01.500000000Z") + end + + it "converts custom attribute values inside multi-contexts" do + context = OpenFeature::SDK::EvaluationContext.new( + kind: "multi", user: { key: "user-key", signed_up: Time.utc(2026, 11, 16) }, org: { key: "org-key" } + ) + + ld_context = context_converter.to_ld_context(context) + + expect(ld_context.individual_context("user")[:signed_up]).to eq("2026-11-16T00:00:00Z") + end + end + describe "private attribute handling" do it "private attributes are processed correctly" do attributes = {