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

ZString.Join with HttpStatusCode throws Exception #32

Closed
hygehyge opened this issue Aug 18, 2020 · 5 comments
Closed

ZString.Join with HttpStatusCode throws Exception #32

hygehyge opened this issue Aug 18, 2020 · 5 comments

Comments

@hygehyge
Copy link

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.

@neuecc
Copy link
Member

neuecc commented Aug 19, 2020

thanks, it is a bug, will fix soon.

@github-actions
Copy link
Contributor

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@neuecc
Copy link
Member

neuecc commented Nov 25, 2020

check

@neuecc neuecc reopened this Nov 25, 2020
@github-actions github-actions bot closed this as completed Dec 3, 2020
@neuecc neuecc reopened this Dec 3, 2020
@neuecc neuecc reopened this Dec 11, 2020
@neuecc
Copy link
Member

neuecc commented Dec 11, 2020

wait

@neuecc
Copy link
Member

neuecc commented Jan 14, 2021

thanks, fixed at 2.3.0.

@neuecc neuecc closed this as completed Jan 14, 2021
ThangwLee pushed a commit to WolffunGame/ZString that referenced this issue May 26, 2023
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

No branches or pull requests

2 participants