Skip to content

Commit

Permalink
Merge pull request #2363 from FirelyTeam/feature/2338-fix-nullref-in-…
Browse files Browse the repository at this point in the history
…enumutil

2338 Fix nullref exception in EnumUtility
  • Loading branch information
marcovisserFurore authored Jan 11, 2023
2 parents eab8b62 + e239db9 commit 16ce37f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/Hl7.Fhir.Base/Utility/EnumUtility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/*
#nullable enable

/*
* Copyright (c) 2014, Firely ([email protected]) and contributors
* See the file CONTRIBUTORS for details.
*
Expand All @@ -9,11 +11,8 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Concurrent;

#nullable enable

namespace Hl7.Fhir.Utility
{
/// <summary>
Expand Down Expand Up @@ -43,13 +42,13 @@ public static string GetDocumentation(this Enum e) =>
/// <summary>
/// Finds an enumeration value from <paramref name="enumType"/> where the literal is the same as <paramref name="rawValue"/>.
/// </summary>
public static Enum? ParseLiteral(string rawValue, Type enumType, bool ignoreCase = false)
public static Enum? ParseLiteral(string? rawValue, Type enumType, bool ignoreCase = false)
=> getEnumMapping(enumType).ParseLiteral(rawValue, ignoreCase);

/// <summary>
/// Finds an enumeration value from enum <typeparamref name="T"/> where the literal is the same as <paramref name="rawValue"/>.
/// </summary>
public static T? ParseLiteral<T>(string rawValue, bool ignoreCase = false) where T : struct
public static T? ParseLiteral<T>(string? rawValue, bool ignoreCase = false) where T : struct
=> (T?)(object?)ParseLiteral(rawValue, typeof(T), ignoreCase);

/// <summary>
Expand Down Expand Up @@ -88,20 +87,15 @@ public string GetLiteral(Enum value) =>
? throw new InvalidOperationException($"Should only pass enum values that are member of the given enum: {value} is not a member of {Name}.")
: result;

public Enum? ParseLiteral(string literal, bool ignoreCase)
public Enum? ParseLiteral(string? literal, bool ignoreCase)
{
Enum? result;
if (literal is null) return null;

if (ignoreCase)
{
_lowercaseLiteralToEnum.TryGetValue(literal.ToLowerInvariant(), out result);
}
else
{
_literalToEnum.TryGetValue(literal, out result);
}
var success = ignoreCase
? _lowercaseLiteralToEnum.TryGetValue(literal.ToLowerInvariant(), out Enum? result)
: _literalToEnum.TryGetValue(literal, out result);

return result;
return success ? result : null;
}

public static EnumMapping Create(Type enumType)
Expand Down Expand Up @@ -134,4 +128,4 @@ static string getEnumName(Type t)
}
}

#nullable restore
#nullable restore
8 changes: 8 additions & 0 deletions src/Hl7.Fhir.Support.Tests/Utility/EnumMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* available at https://raw.githubusercontent.com/FirelyTeam/firely-net-sdk/master/LICENSE
*/

using FluentAssertions;
using Hl7.Fhir.Utility;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
Expand Down Expand Up @@ -137,5 +138,12 @@ enum X
a,
b
}

[TestMethod]
public void NullLiteralHandling()
{
EnumUtility.ParseLiteral<TestAdministrativeGender>(null).Should().BeNull();
EnumUtility.ParseLiteral<TestAdministrativeGender>(null, ignoreCase: true).Should().BeNull();
}
}
}

0 comments on commit 16ce37f

Please sign in to comment.