From a8052d69368680106bbd2eb540bbf49ec86ac7be Mon Sep 17 00:00:00 2001 From: Kostas Date: Mon, 8 Jan 2024 15:17:27 +0200 Subject: [PATCH] Fix for #375 - IEnumerable property serialization. Signed-off-by: Kostas --- CHANGELOG.md | 7 ++- .../Mapping/Visitor/PropertyWalker.cs | 8 +++ tests/Tests.Reproduce/PropertyWalkerTests .cs | 54 +++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/Tests.Reproduce/PropertyWalkerTests .cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fa32748ac..eda57329d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,11 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Removed - Removed the `Features` API which is not supported by OpenSearch from the low-level client ([#331](https://github.com/opensearch-project/opensearch-net/pull/331)) + +### Fixed +- Fixed `IEnumerable` property serialization. ([#380](https://github.com/opensearch-project/opensearch-net/pull/375)) + + ### 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 @@ -118,4 +123,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) [Unreleased]: https://github.com/opensearch-project/opensearch-net/compare/v1.5.0...main [1.5.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.4.0...v1.5.0 [1.4.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.3.0...v1.4.0 -[1.3.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.2.0...v1.3.0 \ No newline at end of file +[1.3.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.2.0...v1.3.0 diff --git a/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs b/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs index c95dcb9812..6280a36ef0 100644 --- a/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs +++ b/src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs @@ -227,5 +227,13 @@ bool ShouldUnwrapType(Type ty) ); } } + + 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.Reproduce/PropertyWalkerTests .cs b/tests/Tests.Reproduce/PropertyWalkerTests .cs new file mode 100644 index 0000000000..dd7a0d3ad1 --- /dev/null +++ b/tests/Tests.Reproduce/PropertyWalkerTests .cs @@ -0,0 +1,54 @@ +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using OpenSearch.Client; +using OpenSearch.OpenSearch.Xunit.XunitPlumbing; + +namespace Tests.Reproduce +{ + public class PropertyWalkerTests + { + [U] + public void IEnumerableOfNullablesGetsMappedCorrectly() + { + var result = TestPropertyType>(); + result.Should().Be("integer"); + } + + [U] + public void IListOfNullablesGetsMappedCorrectly() + { + 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; } + } + + } +}