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

fix: Fix ArgumentOutOfRangeException for empty hooks #187

Merged
merged 2 commits into from
Jan 17, 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
21 changes: 20 additions & 1 deletion src/OpenFeature/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,26 @@
/// </para>
/// </summary>
/// <param name="hooks">A list of <see cref="Hook"/></param>
public void AddHooks(IEnumerable<Hook> hooks) => this._hooks.PushRange(hooks.ToArray());
public void AddHooks(IEnumerable<Hook> hooks)
#if NET7_0_OR_GREATER
=> this._hooks.PushRange(hooks as Hook[] ?? hooks.ToArray());
austindrenski marked this conversation as resolved.
Show resolved Hide resolved
#else
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
{
// See: https://github.com/dotnet/runtime/issues/62121
if (hooks is Hook[] array)
{
if (array.Length > 0)
this._hooks.PushRange(array);

return;
}

array = hooks.ToArray();

if (array.Length > 0)
this._hooks.PushRange(array);

Check warning on line 147 in src/OpenFeature/Api.cs

View check run for this annotation

Codecov / codecov/patch

src/OpenFeature/Api.cs#L147

Added line #L147 was not covered by tests
}
#endif

/// <summary>
/// Adds a hook to global hooks list
Expand Down
21 changes: 20 additions & 1 deletion src/OpenFeature/OpenFeatureClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,26 @@
}

/// <inheritdoc />
public void AddHooks(IEnumerable<Hook> hooks) => this._hooks.PushRange(hooks.ToArray());
public void AddHooks(IEnumerable<Hook> hooks)
#if NET7_0_OR_GREATER
=> this._hooks.PushRange(hooks as Hook[] ?? hooks.ToArray());
#else
{
// See: https://github.com/dotnet/runtime/issues/62121
if (hooks is Hook[] array)
{
if (array.Length > 0)
this._hooks.PushRange(array);

return;
}

array = hooks.ToArray();

Check warning on line 123 in src/OpenFeature/OpenFeatureClient.cs

View check run for this annotation

Codecov / codecov/patch

src/OpenFeature/OpenFeatureClient.cs#L123

Added line #L123 was not covered by tests

if (array.Length > 0)
this._hooks.PushRange(array);

Check warning on line 126 in src/OpenFeature/OpenFeatureClient.cs

View check run for this annotation

Codecov / codecov/patch

src/OpenFeature/OpenFeatureClient.cs#L126

Added line #L126 was not covered by tests
}
#endif

/// <inheritdoc />
public IEnumerable<Hook> GetHooks() => this._hooks.Reverse();
Expand Down
7 changes: 7 additions & 0 deletions test/OpenFeature.Tests/OpenFeatureHookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,5 +553,12 @@ public async Task When_Error_Occurs_In_After_Hook_Should_Invoke_Error_Hook()

await featureProvider.DidNotReceive().ResolveBooleanValue("test", false, Arg.Any<EvaluationContext>());
}

[Fact]
public void Add_hooks_should_accept_empty_enumerable()
{
Api.Instance.ClearHooks();
Api.Instance.AddHooks(Enumerable.Empty<Hook>());
}
}
}
Loading