From 1e68d21b4f79a4b5642194cb593bfcc3b67a6429 Mon Sep 17 00:00:00 2001 From: Kostas Date: Mon, 8 Jan 2024 12:20:21 +0200 Subject: [PATCH] Fix for #380 --- .../Mapping/Visitor/PropertyWalker.cs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs b/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs index fcd72b7415..c95dcb9812 100644 --- a/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs +++ b/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs @@ -205,11 +205,27 @@ private static Type GetUnderlyingType(Type type) if (type.IsArray) return type.GetElementType(); - if (type.IsGenericType && type.GetGenericArguments().Length == 1 - && (type.GetInterfaces().HasAny(t => t == typeof(IEnumerable)) || Nullable.GetUnderlyingType(type) != null)) - return type.GetGenericArguments()[0]; + if (ShouldUnwrapType(type)) + { + var returnType = type.GetGenericArguments()[0]; + if (ShouldUnwrapType(returnType)) // This is needed for types like IEnumerable. + { + return returnType.GetGenericArguments()[0]; + } + return returnType; + } return type; + + bool ShouldUnwrapType(Type ty) + { + return ty.IsGenericType && + ty.GetGenericArguments().Length == 1 + && ( + ty.GetInterfaces().HasAny(t => t == typeof(IEnumerable)) || + Nullable.GetUnderlyingType(ty) != null + ); + } } } }