diff --git a/libs/client-sdk/src/client_impl.cpp b/libs/client-sdk/src/client_impl.cpp index 8d293528a..a06d77395 100644 --- a/libs/client-sdk/src/client_impl.cpp +++ b/libs/client-sdk/src/client_impl.cpp @@ -368,6 +368,12 @@ EvaluationDetail ClientImpl::VariationInternal(FlagKey const& key, event_processor_->SendAsync(std::move(event)); + // If the flag has no value, the result is the default value. + if (detail.Value().IsNull()) { + return EvaluationDetail(default_value, detail.VariationIndex(), + detail.Reason()); + } + return EvaluationDetail(detail.Value(), detail.VariationIndex(), detail.Reason()); } diff --git a/libs/client-sdk/tests/client_test.cpp b/libs/client-sdk/tests/client_test.cpp index 22a2d5321..680babcba 100644 --- a/libs/client-sdk/tests/client_test.cpp +++ b/libs/client-sdk/tests/client_test.cpp @@ -2,10 +2,39 @@ #include #include #include +#include +#include +#include using namespace launchdarkly; using namespace launchdarkly::client_side; +namespace { + +/* Returns the same flag data for every context. */ +class FlagDataPersistence : public IPersistence { + public: + explicit FlagDataPersistence(std::string flag_data) + : flag_data_(std::move(flag_data)) {} + + void Set(std::string, std::string, std::string) noexcept override {} + + void Remove(std::string, std::string) noexcept override {} + + std::optional Read(std::string, + std::string key) noexcept override { + if (key == "ContextIndex") { + return std::nullopt; + } + return flag_data_; + } + + private: + std::string flag_data_; +}; + +} // namespace + TEST(ClientTest, ClientConstructedWithMinimalConfigAndContext) { tl::expected config = ConfigBuilder("sdk-123").Build(); ASSERT_TRUE(config); @@ -85,6 +114,28 @@ TEST(ClientTest, JsonVariationDefaultPassesThrough) { } } +TEST(ClientTest, JsonVariationReturnsDefaultWhenFlagHasNoValue) { + // The cached data has a flag with a null value and a flag without a + // value. Each flag has no variation. + auto builder = ConfigBuilder("sdk-123"); + builder.Offline(true); + builder.Persistence().Custom(std::make_shared( + R"({"null-value":{"version":1,"value":null,"variation":null,)" + R"("reason":{"kind":"OFF"}},)" + R"("omitted-value":{"version":1,"reason":{"kind":"OFF"}}})")); + Client client(builder.Build().value(), + ContextBuilder().Kind("cat", "shadow").Build()); + + // The JSON variation calls return the default value for each flag. + for (auto const& flag : {"null-value", "omitted-value"}) { + EXPECT_EQ(client.JsonVariation(flag, "default"), Value("default")) + << flag; + auto const detail = client.JsonVariationDetail(flag, "default"); + EXPECT_EQ(detail.Value(), Value("default")) << flag; + EXPECT_EQ(detail.Reason(), EvaluationReason::Off()) << flag; + } +} + // This test mainly serves to catch any changes made to the types in the Data // Source Status API that are not backwards-compatible. TEST(ClientTest, DataSourceStatus) {