-
Notifications
You must be signed in to change notification settings - Fork 184
ZString.Join with HttpStatusCode throws Exception #32
Copy link
Copy link
Closed
Description
ZString 2.2.0
When I use ZString.Join with HttpStatusCode, it throws System.TypeInitializationException.
Normal enum or Flags enum passes unit test like below.
//pass
[Fact]
public void Test_ZStringJoin_Enum()
{
var inputs = new[]
{
SearchOption.AllDirectories,
SearchOption.TopDirectoryOnly,
};
var result = ZString.Join(" or ", inputs);
result.ShouldBe($"{nameof(SearchOption.AllDirectories)} or {nameof(SearchOption.TopDirectoryOnly)}");
}
//pass
[Fact]
public void Test_ZStringJoin_FlagsEnum()
{
var inputs = new[]
{
FileOptions.None,
FileOptions.RandomAccess,
};
var result = ZString.Join(" or ", inputs);
result.ShouldBe($"{nameof(FileOptions.None)} or {nameof(FileOptions.RandomAccess)}");
}But when I use HttpStatusCode, it throws Exception.
//FAIL!!
[Fact]
public void Test_ZStringJoin_HttpStatusCode()
{
var inputs = new[]
{
HttpStatusCode.OK,
HttpStatusCode.Created,
};
var result = ZString.Join(" or ", inputs);
result.ShouldBe($"{nameof(HttpStatusCode.OK)} or {nameof(HttpStatusCode.Created)}");
}
/*
System.TypeInitializationException : The type initializer for 'Cysharp.Text.EnumUtil`1' threw an exception.
---- System.ArgumentException : An item with the same key has already been added. Key: Ambiguous
StackTrace:
EnumUtil`1.TryFormatUtf16(T value, Span`1 dest, Int32& written, ReadOnlySpan`1 _)
Utf16ValueStringBuilder.Append[T](T value)
Utf16ValueStringBuilder.AppendJoinInternal[T](ReadOnlySpan`1 separator, ReadOnlySpan`1 values)
ZString.JoinInternal[T](ReadOnlySpan`1 separator, ReadOnlySpan`1 values)
ZString.Join[T](String separator, T[] values)
UnitTest1.Test_ZStringJoin_HttpStatusCode()
----- Inner Stack Trace -----
Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
Dictionary`2.Add(TKey key, TValue value)
EnumUtil`1.cctor()
*/I think the main cause is Enum.GetValues(typeof(HttpStatusCode)) returns the same value like MultipleChoices, and use them as the key of Dictionary in EnumUtil's constructor.
Maybe need to key existence check?
Thanks.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels