Skip to content

Commit

Permalink
Fix for opensearch-project#375 - IEnumerable<int?> property serializa…
Browse files Browse the repository at this point in the history
…tion.

Signed-off-by: Kostas <[email protected]>
  • Loading branch information
DumboJetEngine committed Jan 8, 2024
1 parent ab7fd4f commit e60cc43
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<int?>` property serialization. ([#375](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
Expand Down Expand Up @@ -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
[1.3.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.2.0...v1.3.0
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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ public QueryContainer Name(string name)
/// <returns></returns>
public QueryContainer Strict(bool strict)
{
var visitor = new QueryNodeModifierVisitor((query, ctx) => query.IsStrict = true);
var visitor = new QueryNodeModifierVisitor((query, ctx) => query.IsStrict = strict);
Accept(visitor);
return this;
}

/// <summary>
/// Applies the `verbatim` attribute to the query container and all child sub-queries.
/// </summary>
/// <param name="strict"></param>
/// <param name="verbatim"></param>
/// <returns></returns>
public QueryContainer Verbatim(bool strict)
public QueryContainer Verbatim(bool verbatim)
{
var visitor = new QueryNodeModifierVisitor((query, ctx) => query.IsVerbatim = true);
var visitor = new QueryNodeModifierVisitor((query, ctx) => query.IsVerbatim = verbatim);
Accept(visitor);
return this;
}
Expand Down
54 changes: 54 additions & 0 deletions tests/Tests.Reproduce/PropertyWalkerTests .cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<int?>>();
result.Should().Be("integer");
}

[U]
public void IListOfNullablesGetsMappedCorrectly()
{
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 e60cc43

Please sign in to comment.