Skip to content

Commit

Permalink
Fix for IEnumerable<int?> property mapping ( opensearch-project#380 )
Browse files Browse the repository at this point in the history
Signed-off-by: Kostas <ghost0002001@hotmail.com>
(cherry picked from commit 14c8af6)
  • Loading branch information
DumboJetEngine committed Jan 11, 2024
1 parent 7880420 commit 45f265f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Deprecated
- Deprecated the low-level `IndexTemplateV2` APIs in favour of the new `ComposableIndexTemplate` APIs ([#454](https://github.com/opensearch-project/opensearch-net/pull/454))


### Fixed
- Fixed `IEnumerable<int?>` property mapping. ([#503](https://github.com/opensearch-project/opensearch-net/pull/503))


### Dependencies
- Bumps `FSharp.Data` from 6.2.0 to 6.3.0
- Bumps `BenchMarkDotNet` from 0.13.7 to 0.13.10
Expand Down
20 changes: 17 additions & 3 deletions src/OpenSearch.Client/Mapping/Visitor/PropertyWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int?>.
{
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
);
}
}
84 changes: 84 additions & 0 deletions tests/Tests/Mapping/PropertyWalkerTests.cs
Original file line number Diff line number Diff line change
@@ -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<bool>();
result.Should().Be("boolean");
}

[U]
public void StringGetsMappedCorrectly()
{
var result = TestPropertyType<string>();
result.Should().Be("text");
}


[U]
public void DateTimeGetsMappedCorrectly()
{
var result = TestPropertyType<DateTime>();
result.Should().Be("date");
}

[U]
public void IEnumerableOfNullableGetsMappedCorrectly()
{
var result = TestPropertyType<IEnumerable<int?>>();
result.Should().Be("integer");
}

[U]
public void IListOfNullableGetsMappedCorrectly()
{
var result = TestPropertyType<IList<int?>>();
result.Should().Be("integer");
}

[U]
public void IEnumerableOfValueTypesGetsMappedCorrectly()
{
var result = TestPropertyType<IEnumerable<int>>();
result.Should().Be("integer");
}

[U]
public void IListOfValueTypesGetsMappedCorrectly()
{
var result = TestPropertyType<IList<int>>();
result.Should().Be("integer");
}

private static string TestPropertyType<T>()
{
var walker = new PropertyWalker(typeof(Test<T>), null, 0);
var properties = walker.GetProperties();
var result = properties.SingleOrDefault();
return result.Value?.Type;
}


private class Test<T>
{
public T Values { get; set; }
}

}
}

0 comments on commit 45f265f

Please sign in to comment.