From afa7c7c928decdeac66feabd64c04732e1b57fe7 Mon Sep 17 00:00:00 2001 From: DumboJetEngine Date: Thu, 11 Jan 2024 00:14:34 +0200 Subject: [PATCH] Fix for `IEnumerable` property mapping (#503) Fixes #380 Signed-off-by: Kostas --- CHANGELOG.md | 5 ++ .../Mapping/Visitor/PropertyWalker.cs | 20 ++++- tests/Tests/Mapping/PropertyWalkerTests.cs | 84 +++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 tests/Tests/Mapping/PropertyWalkerTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 92b97d81c5..772cf5df87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Fixed - Fix `HttpConnection.ConvertHttpMethod` to support `Patch` method ([#489](https://github.com/opensearch-project/opensearch-net/pull/489)) + +### Fixed +- Fixed `IEnumerable` property mapping. ([#503](https://github.com/opensearch-project/opensearch-net/pull/503)) + + ### Dependencies - Bumps `Microsoft.CodeAnalysis.CSharp` from 4.2.0 to 4.6.0 - Bumps `NSwag.Core.Yaml` from 13.19.0 to 13.20.0 diff --git a/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs b/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs index fcd72b7415..5734b2d21b 100644 --- a/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs +++ b/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs @@ -205,11 +205,25 @@ 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; } + + private static bool ShouldUnwrapType(Type ty) => + ty.IsGenericType && + ty.GetGenericArguments().Length == 1 && + ( + ty.GetInterfaces().HasAny(t => t == typeof(IEnumerable)) || + Nullable.GetUnderlyingType(ty) != null + ); } } diff --git a/tests/Tests/Mapping/PropertyWalkerTests.cs b/tests/Tests/Mapping/PropertyWalkerTests.cs new file mode 100644 index 0000000000..4ea85b68dd --- /dev/null +++ b/tests/Tests/Mapping/PropertyWalkerTests.cs @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: Apache-2.0 +* +* The OpenSearch Contributors require contributions made to +* this file be licensed under the Apache-2.0 license or a +* compatible open source license. +*/ + +using System; +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using OpenSearch.Client; +using OpenSearch.OpenSearch.Xunit.XunitPlumbing; + +namespace Tests.Mapping +{ + public class PropertyWalkerTests + { + [U] + public void BoolGetsMappedCorrectly() + { + var result = TestPropertyType(); + result.Should().Be("boolean"); + } + + [U] + public void StringGetsMappedCorrectly() + { + var result = TestPropertyType(); + result.Should().Be("text"); + } + + + [U] + public void DateTimeGetsMappedCorrectly() + { + var result = TestPropertyType(); + result.Should().Be("date"); + } + + [U] + public void IEnumerableOfNullableGetsMappedCorrectly() + { + var result = TestPropertyType>(); + result.Should().Be("integer"); + } + + [U] + public void IListOfNullableGetsMappedCorrectly() + { + var result = TestPropertyType>(); + result.Should().Be("integer"); + } + + [U] + public void IEnumerableOfValueTypesGetsMappedCorrectly() + { + var result = TestPropertyType>(); + result.Should().Be("integer"); + } + + [U] + public void IListOfValueTypesGetsMappedCorrectly() + { + var result = TestPropertyType>(); + result.Should().Be("integer"); + } + + private static string TestPropertyType() + { + var walker = new PropertyWalker(typeof(Test), null, 0); + var properties = walker.GetProperties(); + var result = properties.SingleOrDefault(); + return result.Value?.Type; + } + + + private class Test + { + public T Values { get; set; } + } + + } +}