Skip to content

ZString.Join with HttpStatusCode throws Exception #32

@hygehyge

Description

@hygehyge

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions