Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed incorrect implementation of GetFeatureResult call #26

Merged
merged 2 commits into from
Feb 6, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.2]

- Fixed issue with incorrect logic in GrowthBook.GetFeatureResult<T>() call.

## [1.0.1]

- Fixed issue with empty string value sent to IsIn condition evaluation.
Expand Down
33 changes: 33 additions & 0 deletions GrowthBook.Tests/CustomTests/RegressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,39 @@ namespace GrowthBook.Tests.CustomTests;

public class RegressionTests : UnitTest
{
[Fact]
public void GetFeatureValueShouldOnlyReturnFallbackValueWhenTheFeatureResultValueIsNull()
{
const string FeatureName = "test-false-default-value";

var feature = new Feature { DefaultValue = false };

var staticFeatures = new Dictionary<string, Feature>
{
[FeatureName] = feature
};

var context = new Context
{
Features = staticFeatures
};

var growthBook = new GrowthBook(context);

growthBook.IsOn(FeatureName).Should().BeFalse("because the value of the feature is considered falsy");
growthBook.GetFeatureValue(FeatureName, true).Should().BeFalse("because the default value can be served from the feature");

growthBook.Features[FeatureName] = new Feature { DefaultValue = null };

growthBook.IsOn(FeatureName).Should().BeFalse("because the value of the feature is considered falsy");
growthBook.GetFeatureValue(FeatureName, false).Should().BeFalse("because the fallback value should be returned when the result value is null");

growthBook.Features[FeatureName] = new Feature { DefaultValue = true };

growthBook.IsOn(FeatureName).Should().BeTrue("because the value of the feature is considered truthy");
growthBook.GetFeatureValue(FeatureName, false).Should().BeTrue("because the default value can be served from the feature");
}

[Fact]
public void EvalIsInConditionShouldNotDifferWhenAttributeIsEmptyInsteadOfNull()
{
Expand Down
9 changes: 3 additions & 6 deletions GrowthBook/GrowthBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,9 @@ public bool IsOff(string key)
/// <inheritdoc />
public T GetFeatureValue<T>(string key, T fallback)
{
FeatureResult result = EvalFeature(key);
if (result.On)
{
return result.Value.ToObject<T>();
}
return fallback;
var value = EvalFeature(key).Value;

return value.IsNull() ? fallback : value.ToObject<T>();
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion GrowthBook/GrowthBook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<RepositoryUrl>https://github.com/growthbook/growthbook-csharp.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>GrowthBook,A/B test,feature toggle,flag</PackageTags>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<Title>GrowthBook C# SDK</Title>
<PackageProjectUrl>https://www.growthbook.io/</PackageProjectUrl>
</PropertyGroup>
Expand Down
Loading