kysect / kysect/CommonLib

FromEnumString return unexpected result if input is invalid

Open
#48 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
0
Forks
4
PR merge metrics
No merged PRs in 30d

Description

FirstOrDefault return not null, but default value, so method never throw exeption:

public static T FromEnumString<T>(string value) where T : struct, Enum
{
    value.ThrowIfNull();

    var reflectionAttributeFinder = new ReflectionAttributeFinder();

    IReadOnlyDictionary<T, EnumStringValueAttribute> enumStrings = reflectionAttributeFinder.GetAttributesFromEnumValues<EnumStringValueAttribute, T>();

    KeyValuePair<T, EnumStringValueAttribute>? selectedEnumValue = enumStrings.FirstOrDefault(p => p.Value.StringValue == value);

    if (selectedEnumValue is null)
        throw new ReflectionException($"Cannot find enum values associated to string {value} in {typeof(T).Name}");

    return selectedEnumValue.Value.Key;
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the FromEnumString entry point shown in the issue and inspect how FirstOrDefault is checked when no enum value matches. Add coverage for an invalid string and verify that it throws ReflectionException, while existing valid-value behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.