Skip to content

Commit 1b5cd58

Browse files
committed
update objecttodictionary to accomodate for enums
1 parent ec0b73a commit 1b5cd58

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Internal/Helpers/PowertoolsLoggerHelpers.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Collections.Generic;
1718
using System.Linq;
19+
using System.Reflection;
20+
using System.Text.Json.Serialization;
1821

1922
namespace AWS.Lambda.Powertools.Logging.Internal.Helpers;
2023

@@ -38,18 +41,31 @@ internal static object ObjectToDictionary(object anonymousObject)
3841
return new Dictionary<string, object>();
3942
}
4043

41-
if (anonymousObject.GetType().Namespace is not null)
44+
var type = anonymousObject.GetType();
45+
46+
if (type.IsEnum)
47+
{
48+
return anonymousObject;
49+
}
50+
51+
if (type.Namespace != null && !type.IsEnum)
4252
{
4353
return anonymousObject;
4454
}
4555

46-
return anonymousObject.GetType().GetProperties()
56+
return type.GetProperties()
4757
.Where(prop => prop.GetValue(anonymousObject, null) != null)
4858
.ToDictionary(
4959
prop => prop.Name,
5060
prop => {
5161
var value = prop.GetValue(anonymousObject, null);
52-
return value != null ? ObjectToDictionary(value) : string.Empty;
62+
if (value == null)
63+
return string.Empty;
64+
65+
if (value.GetType().IsEnum)
66+
return value;
67+
68+
return ObjectToDictionary(value);
5369
}
5470
);
5571
}

0 commit comments

Comments
 (0)