Skip to content

Commit 950775b

Browse files
fix: Fix ArgumentOutOfRangeException for empty hooks (#187)
Signed-off-by: Austin Drenski <[email protected]>
1 parent a6062fe commit 950775b

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

Diff for: src/OpenFeature/Api.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,26 @@ public FeatureClient GetClient(string name = null, string version = null, ILogge
127127
/// </para>
128128
/// </summary>
129129
/// <param name="hooks">A list of <see cref="Hook"/></param>
130-
public void AddHooks(IEnumerable<Hook> hooks) => this._hooks.PushRange(hooks.ToArray());
130+
public void AddHooks(IEnumerable<Hook> hooks)
131+
#if NET7_0_OR_GREATER
132+
=> this._hooks.PushRange(hooks as Hook[] ?? hooks.ToArray());
133+
#else
134+
{
135+
// See: https://github.com/dotnet/runtime/issues/62121
136+
if (hooks is Hook[] array)
137+
{
138+
if (array.Length > 0)
139+
this._hooks.PushRange(array);
140+
141+
return;
142+
}
143+
144+
array = hooks.ToArray();
145+
146+
if (array.Length > 0)
147+
this._hooks.PushRange(array);
148+
}
149+
#endif
131150

132151
/// <summary>
133152
/// Adds a hook to global hooks list

Diff for: src/OpenFeature/OpenFeatureClient.cs

+20-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,26 @@ public void RemoveHandler(ProviderEventTypes type, EventHandlerDelegate handler)
106106
}
107107

108108
/// <inheritdoc />
109-
public void AddHooks(IEnumerable<Hook> hooks) => this._hooks.PushRange(hooks.ToArray());
109+
public void AddHooks(IEnumerable<Hook> hooks)
110+
#if NET7_0_OR_GREATER
111+
=> this._hooks.PushRange(hooks as Hook[] ?? hooks.ToArray());
112+
#else
113+
{
114+
// See: https://github.com/dotnet/runtime/issues/62121
115+
if (hooks is Hook[] array)
116+
{
117+
if (array.Length > 0)
118+
this._hooks.PushRange(array);
119+
120+
return;
121+
}
122+
123+
array = hooks.ToArray();
124+
125+
if (array.Length > 0)
126+
this._hooks.PushRange(array);
127+
}
128+
#endif
110129

111130
/// <inheritdoc />
112131
public IEnumerable<Hook> GetHooks() => this._hooks.Reverse();

Diff for: test/OpenFeature.Tests/OpenFeatureHookTests.cs

+7
Original file line numberDiff line numberDiff line change
@@ -553,5 +553,12 @@ public async Task When_Error_Occurs_In_After_Hook_Should_Invoke_Error_Hook()
553553

554554
await featureProvider.DidNotReceive().ResolveBooleanValue("test", false, Arg.Any<EvaluationContext>());
555555
}
556+
557+
[Fact]
558+
public void Add_hooks_should_accept_empty_enumerable()
559+
{
560+
Api.Instance.ClearHooks();
561+
Api.Instance.AddHooks(Enumerable.Empty<Hook>());
562+
}
556563
}
557564
}

0 commit comments

Comments
 (0)