Skip to content

Conversation

@kylejuliandev
Copy link
Contributor

This PR

This PR fixes an issue when configuring OpenFeature without a Provider. The singleton approach provided by Api.Instance can handle this scenario, so the Dependency Injection approach should be able to. Take the following:

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.AddOpenFeature(featureBuilder =>
{
    featureBuilder
        .AddContext(dict => dict.Set("region", "euw"));
});

var app = builder.Build();

app.MapGet("/welcome", async ([FromServices] IFeatureClient featureClient) =>
{
    var welcomeMessageEnabled = await featureClient.GetBooleanValueAsync("welcome-message", false);

    if (welcomeMessageEnabled)
    {
        return TypedResults.Ok("Hello world! The welcome-message feature flag was enabled!");
    }

    return TypedResults.Ok("Hello world!");
});

app.Run();

When you run this and try to send a request to GET /welcome you will get an error trying to resolve IFeatureClient. This only manifests at runtime.

Related Issues

Fixes #544

Notes

Follow-up Tasks

How to test

* Address issue when adding context to the feature builder without
  providing any provider or default feature client.

Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
@kylejuliandev
Copy link
Contributor Author

Blocked by #606

@codecov
Copy link

codecov bot commented Oct 21, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.85%. Comparing base (c987b58) to head (123f4e8).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #607   +/-   ##
=======================================
  Coverage   89.84%   89.85%           
=======================================
  Files          77       77           
  Lines        3162     3163    +1     
  Branches      364      364           
=======================================
+ Hits         2841     2842    +1     
  Misses        251      251           
  Partials       70       70           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
@kylejuliandev kylejuliandev marked this pull request as ready for review October 29, 2025 20:03
@kylejuliandev kylejuliandev requested a review from a team as a code owner October 29, 2025 20:03
Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
@kylejuliandev
Copy link
Contributor Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively resolves a runtime error that occurred when trying to resolve IFeatureClient via dependency injection without any feature providers configured. The fix involves two key changes: ensuring OpenFeatureOptions is always registered in the service collection, and using FirstOrDefault() instead of First() to safely handle cases with no provider names. A new test case has also been added to validate this fix. The changes are correct and well-implemented. I have one minor suggestion to improve code clarity.

Copy link
Member

@askpt askpt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I wonder what is the impact of that change.

Signed-off-by: Kyle Julian <38759683+kylejuliandev@users.noreply.github.com>
@askpt askpt added this pull request to the merge queue Oct 30, 2025
Merged via the queue into open-feature:main with commit a8d12ef Oct 30, 2025
23 checks passed
@kylejuliandev kylejuliandev deleted the kylej/fix-544 branch November 10, 2025 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling AddContext without registering any Providers throws System.InvalidOperationException

3 participants