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
28 changes: 27 additions & 1 deletion lib/ldclient-openfeature/impl/context_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'ldclient-rb'
require 'open_feature/sdk'
require 'date'
require 'time'

module LaunchDarkly
module OpenFeature
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions spec/impl/context_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading