Skip to content

Commit

Permalink
fix: use the TargetingKey property in the Flagsmith provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ghelyar committed Jul 5, 2024
1 parent 7cccc8d commit 5c8a5a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/OpenFeature.Contrib.Providers.Flagsmith/FlagsmithProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,16 @@ public FlagsmithProvider(IFlagsmithProviderConfiguration providerOptions, IFlags
private Task<IFlags> GetFlags(EvaluationContext ctx)
{
string key = null;
if (ctx != null && ctx.TryGetValue(Configuration.TargetingKey, out var value))
if (ctx != null)
{
key = value?.AsString;
if (ctx.TargetingKey is string { Length: > 0 } targetingKey)
{
key = targetingKey;
}
else if (ctx.TryGetValue(Configuration.TargetingKey, out var value))
{
key = value?.AsString;
}
}

return string.IsNullOrEmpty(key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,29 @@ public async Task GetStructureValue_ForEnabledFeatureWithWrongFormatValue_Throws
// Act and Assert
await Assert.ThrowsAsync<TypeMismatchException>(() => flagsmithProvider.ResolveStructureValue("example-feature", defaultObject));
}

[Theory]
[InlineData("property", "attribute", "property")]
[InlineData(null, "attribute", "attribute")]
[InlineData("", "attribute", "attribute")]
[InlineData("property", null, "property")]
[InlineData("property", "", "property")]
public async Task GetValue_WithTargetingKey_UsesPropertyOverAttribute(string property, string attribute, string expected)
{
// Arrange
var flagsmithClient = Substitute.For<IFlagsmithClient>();
var providerConfig = GetDefaultFlagsmithProviderConfigurationConfiguration();
var flagsmithProvider = new FlagsmithProvider(providerConfig, flagsmithClient);

var contextBuilder = EvaluationContext.Builder()
.SetTargetingKey(property)
.Set(FlagsmithProviderConfiguration.DefaultTargetingKey, attribute);

// Act
await flagsmithProvider.ResolveBooleanValue("example-feature", false, contextBuilder.Build());

// Assert
await flagsmithClient.Received().GetIdentityFlags(expected, Arg.Any<List<ITrait>>());
}
}
}

0 comments on commit 5c8a5a9

Please sign in to comment.