From 06f9d5e7d94a2669a92eb76fe91fdb9fca15d59b Mon Sep 17 00:00:00 2001 From: Joe Feser Date: Mon, 29 Jul 2024 18:48:11 -0500 Subject: [PATCH] feat(): add .net6 and .net8 to core project and get it to compile #680 --- global.json | 4 ++-- .../Connection/MetaData/RuntimeVersionInfo.cs | 4 ++++ .../ConnectionPool/SniffingConnectionPool.cs | 2 +- .../ConnectionPool/StaticConnectionPool.cs | 2 +- .../Extensions/UtilExtensions.cs | 2 +- src/OpenSearch.Net/OpenSearch.Net.csproj | 4 +++- .../Formatters/ExceptionFormatter.cs | 13 +++++++++- src/OpenSearch.Net/Transport/PostData.cs | 4 ++-- .../Resolvers/DynamicObjectResolver.cs | 21 ++++++++++------ .../ClientConcepts/LowLevel/PostData.doc.cs | 24 +++++++++++-------- 10 files changed, 54 insertions(+), 26 deletions(-) diff --git a/global.json b/global.json index 650ed0fd4c..10d8bd6753 100644 --- a/global.json +++ b/global.json @@ -1,10 +1,10 @@ { "sdk": { - "version": "6.0.403", + "version": "8.0.107", "rollForward": "latestFeature", "allowPrerelease": false }, "version": "2.0.0", "doc_current": "2.0", "doc_branch": "2.x" -} \ No newline at end of file +} diff --git a/src/OpenSearch.Net/Connection/MetaData/RuntimeVersionInfo.cs b/src/OpenSearch.Net/Connection/MetaData/RuntimeVersionInfo.cs index 0633eb3dce..135a8c1207 100644 --- a/src/OpenSearch.Net/Connection/MetaData/RuntimeVersionInfo.cs +++ b/src/OpenSearch.Net/Connection/MetaData/RuntimeVersionInfo.cs @@ -114,7 +114,11 @@ private static string GetNetCoreVersion() private static bool TryGetVersionFromAssemblyPath(Assembly assembly, out string runtimeVersion) { +#if NET6_0_OR_GREATER + var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); +#else var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); +#endif var netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App"); if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2) { diff --git a/src/OpenSearch.Net/ConnectionPool/SniffingConnectionPool.cs b/src/OpenSearch.Net/ConnectionPool/SniffingConnectionPool.cs index 20d5c09313..9073ffe5a9 100644 --- a/src/OpenSearch.Net/ConnectionPool/SniffingConnectionPool.cs +++ b/src/OpenSearch.Net/ConnectionPool/SniffingConnectionPool.cs @@ -81,7 +81,7 @@ public override void Reseed(IEnumerable nodes) { _readerWriter.EnterWriteLock(); var sortedNodes = SortNodes(nodesArray) - .DistinctBy(n => n.Uri) + .DistinctByInternal(n => n.Uri) .ToList(); InternalNodes = sortedNodes; diff --git a/src/OpenSearch.Net/ConnectionPool/StaticConnectionPool.cs b/src/OpenSearch.Net/ConnectionPool/StaticConnectionPool.cs index 3566a1f46d..12319671ad 100644 --- a/src/OpenSearch.Net/ConnectionPool/StaticConnectionPool.cs +++ b/src/OpenSearch.Net/ConnectionPool/StaticConnectionPool.cs @@ -82,7 +82,7 @@ private void Initialize(IEnumerable nodes, IDateTimeProvider dateTimeProvi } InternalNodes = SortNodes(nodesProvided) - .DistinctBy(n => n.Uri) + .DistinctByInternal(n => n.Uri) .ToList(); LastUpdate = DateTimeProvider.Now(); } diff --git a/src/OpenSearch.Net/Extensions/UtilExtensions.cs b/src/OpenSearch.Net/Extensions/UtilExtensions.cs index da699625ea..b6ca1645c1 100644 --- a/src/OpenSearch.Net/Extensions/UtilExtensions.cs +++ b/src/OpenSearch.Net/Extensions/UtilExtensions.cs @@ -90,7 +90,7 @@ internal static void ThrowIfNull(this T value, string name) where T : class internal static bool IsNullOrEmpty(this string value) => string.IsNullOrEmpty(value); - internal static IEnumerable DistinctBy(this IEnumerable items, Func property) => + internal static IEnumerable DistinctByInternal(this IEnumerable items, Func property) => items.GroupBy(property).Select(x => x.First()); internal static string ToTimeUnit(this TimeSpan timeSpan) diff --git a/src/OpenSearch.Net/OpenSearch.Net.csproj b/src/OpenSearch.Net/OpenSearch.Net.csproj index 38f9dd428b..155b16c0fd 100644 --- a/src/OpenSearch.Net/OpenSearch.Net.csproj +++ b/src/OpenSearch.Net/OpenSearch.Net.csproj @@ -13,12 +13,14 @@ true true - netstandard2.0;netstandard2.1 + netstandard2.0;netstandard2.1;net6.0;net8.0 true true + + diff --git a/src/OpenSearch.Net/Serialization/Formatters/ExceptionFormatter.cs b/src/OpenSearch.Net/Serialization/Formatters/ExceptionFormatter.cs index 03316469f2..acaf64f6b3 100644 --- a/src/OpenSearch.Net/Serialization/Formatters/ExceptionFormatter.cs +++ b/src/OpenSearch.Net/Serialization/Formatters/ExceptionFormatter.cs @@ -75,10 +75,21 @@ private static List> FlattenExceptions(Exception e) private static Dictionary ToDictionary(Exception e, int depth) { var o = new Dictionary(10); + +#if NET8_0_OR_GREATER + //NOTE: This is a workaround for the obsolete warning in .NET 8.0. We need to find a work around and remove this pragma +#pragma warning disable SYSLIB0050 // Type or member is obsolete var si = new SerializationInfo(e.GetType(), new FormatterConverter()); +#pragma warning restore SYSLIB0050 // Type or member is obsolete var sc = new StreamingContext(); +#pragma warning disable SYSLIB0051 // Type or member is obsolete e.GetObjectData(si, sc); - +#pragma warning restore SYSLIB0051 // Type or member is obsolete +#else + var si = new SerializationInfo(e.GetType(), new FormatterConverter()); + var sc = new StreamingContext(); + e.GetObjectData(si, sc); +#endif var helpUrl = si.GetString("HelpURL"); var stackTrace = si.GetString("StackTraceString"); var remoteStackTrace = si.GetString("RemoteStackTraceString"); diff --git a/src/OpenSearch.Net/Transport/PostData.cs b/src/OpenSearch.Net/Transport/PostData.cs index 037d33980a..d955f92bfd 100644 --- a/src/OpenSearch.Net/Transport/PostData.cs +++ b/src/OpenSearch.Net/Transport/PostData.cs @@ -85,8 +85,8 @@ public abstract class PostData public static PostData Bytes(byte[] bytes) => new PostData(bytes); -#if NETSTANDARD2_1 - public static PostData ReadOnlyMemory(ReadOnlyMemory bytes) => new PostData(bytes); +#if NETSTANDARD2_1 || NET6_0_OR_GREATER + public static PostData ReadOnlyMemory(ReadOnlyMemory bytes) => new PostData(bytes.ToArray()); #endif public static PostData String(string serializedString) => new PostData(serializedString); diff --git a/src/OpenSearch.Net/Utf8Json/Resolvers/DynamicObjectResolver.cs b/src/OpenSearch.Net/Utf8Json/Resolvers/DynamicObjectResolver.cs index 3a0a0dda17..4683ee901e 100644 --- a/src/OpenSearch.Net/Utf8Json/Resolvers/DynamicObjectResolver.cs +++ b/src/OpenSearch.Net/Utf8Json/Resolvers/DynamicObjectResolver.cs @@ -301,8 +301,8 @@ private static TypeInfo BuildType(DynamicAssembly assembly, Type type, Func { - FieldInfo fi; - if (!customFormatterLookup.TryGetValue(member, out fi)) return false; + if (!customFormatterLookup.TryGetValue(member, out var fi)) + return false; il.EmitLoadThis(); il.EmitLdfld(fi); @@ -318,8 +318,8 @@ private static TypeInfo BuildType(DynamicAssembly assembly, Type type, Func { - FieldInfo fi; - if (!customFormatterLookup.TryGetValue(member, out fi)) return false; + if (!customFormatterLookup.TryGetValue(member, out var fi)) + return false; il.EmitLoadThis(); il.EmitLdfld(fi); @@ -343,10 +343,12 @@ public static object BuildAnonymousFormatter(Type type, Func nam }.Select(x => nameMutator(x))); // special case for exception, modify - serializationInfo = new MetaType(type, nameMutator, propertyMapper, false); + serializationInfo = new MetaType(type, nameMutator, propertyMapper, false) + { + BestMatchConstructor = null, + ConstructorParameters = [] + }; - serializationInfo.BestMatchConstructor = null; - serializationInfo.ConstructorParameters = new MetaMember[0]; serializationInfo.Members = new[] { new StringConstantValueMetaMember(nameMutator("ClassName"), type.FullName) } .Concat(serializationInfo.Members.Where(x => !ignoreSet.Contains(x.Name))) .Concat(new[] { new InnerExceptionMetaMember(nameMutator("InnerException")) }) @@ -1256,7 +1258,12 @@ internal static class EmitInfo public static readonly MethodInfo GetCustomAttributeJsonFormatterAttribute = ExpressionUtility.GetMethodInfo(() => CustomAttributeExtensions.GetCustomAttribute(default(MemberInfo), default(bool))); public static readonly MethodInfo ActivatorCreateInstance = ExpressionUtility.GetMethodInfo(() => Activator.CreateInstance(default(Type), default(object[]))); + +#if NET5_0_OR_GREATER + public static readonly MethodInfo GetUninitializedObject = ExpressionUtility.GetMethodInfo(() => System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(default(Type))); +#else public static readonly MethodInfo GetUninitializedObject = ExpressionUtility.GetMethodInfo(() => System.Runtime.Serialization.FormatterServices.GetUninitializedObject(default(Type))); +#endif public static readonly MethodInfo GetTypeMethod = ExpressionUtility.GetMethodInfo((object o) => o.GetType()); public static readonly MethodInfo TypeGetGenericArguments = ExpressionUtility.GetPropertyInfo((Type t) => t.GenericTypeArguments).GetMethod; diff --git a/tests/Tests/ClientConcepts/LowLevel/PostData.doc.cs b/tests/Tests/ClientConcepts/LowLevel/PostData.doc.cs index f42fa65b4a..fc93c9b6ae 100644 --- a/tests/Tests/ClientConcepts/LowLevel/PostData.doc.cs +++ b/tests/Tests/ClientConcepts/LowLevel/PostData.doc.cs @@ -1,3 +1,5 @@ +#pragma warning disable IDE1006 // Naming Styles +#pragma warning disable IDE0044 // Add readonly modifier /* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to @@ -64,12 +66,12 @@ public class PostingData public PostingData() { - this.@object = new { my_object = "value" }; - this.collectionOfObjects = Enumerable.Range(0, 5).Select(i => @object).Cast().ToList(); + @object = new { my_object = "value" }; + collectionOfObjects = Enumerable.Range(0, 5).Select(i => @object).Cast().ToList(); var json = "{\"my_object\":\"value\"}"; - this.utf8ObjectBytes = Utf8Bytes(json); - this.utf8BytesOfListOfStrings = Utf8Bytes(string.Join("\n", collectionOfStrings) + "\n"); - this.utf8BytesOfCollectionOfObjects = Utf8Bytes(string.Join("\n", collectionOfObjects.Select(o=> json)) + "\n"); + utf8ObjectBytes = Utf8Bytes(json); + utf8BytesOfListOfStrings = Utf8Bytes(string.Join("\n", collectionOfStrings) + "\n"); + utf8BytesOfCollectionOfObjects = Utf8Bytes(string.Join("\n", collectionOfObjects.Select(o=> json)) + "\n"); } [U] public void ImplicitConversions() @@ -118,9 +120,9 @@ [U] public void ExplicitCreation() * Let's demonstrate how to use the static helper on `PostData` for these: */ - PostData fromObject = PostData.Serializable(@object); - PostData fromListOfString = PostData.MultiJson(collectionOfStrings); - PostData fromListOfObject = PostData.MultiJson(collectionOfObjects); + var fromObject = PostData.Serializable(@object); + var fromListOfString = PostData.MultiJson(collectionOfStrings); + var fromListOfObject = PostData.MultiJson(collectionOfObjects); /** The `Type` property is representative of the original type from which post data is constructed */ fromListOfString.Type.Should().Be(PostType.EnumerableOfString); @@ -139,8 +141,8 @@ [U] public void ExplicitCreation() //hide [U] public async Task WritesCorrectlyUsingBothLowAndHighLevelSettings() { - await this.AssertOn(new ConnectionSettings()); - await this.AssertOn(new ConnectionConfiguration()); + await AssertOn(new ConnectionSettings()); + await AssertOn(new ConnectionConfiguration()); } private async Task AssertOn(IConnectionConfigurationValues settings) @@ -260,3 +262,5 @@ private static async Task PostAssertAsync(PostData postData, byte[] writes, bool } } +#pragma warning restore IDE0044 // Add readonly modifier +#pragma warning restore IDE1006 // Naming Styles