From 6047ecd14911782dde227fcb04eae400fbb6cc34 Mon Sep 17 00:00:00 2001 From: fern-api <115122769+fern-api[bot]@users.noreply.github.com> Date: Thu, 23 May 2024 16:03:06 +0000 Subject: [PATCH] SDK regeneration --- src/Merge.Client.sln | 20 ++--- .../Core/JsonEnumMemberStringEnumConverter.cs | 75 +++++++++++++++++++ src/Merge.Client/Core/RawClient.cs | 6 ++ src/Merge.Client/Merge.Client.csproj | 6 +- src/Merge.Client/Merge.cs | 4 +- 5 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 src/Merge.Client/Core/JsonEnumMemberStringEnumConverter.cs diff --git a/src/Merge.Client.sln b/src/Merge.Client.sln index 65cfa7a8..518ccb09 100644 --- a/src/Merge.Client.sln +++ b/src/Merge.Client.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge.Client", "Merge.Client\Merge.Client.csproj", "{F2B96F3A-F41D-4505-8135-B9C6ECEF44F0}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge.Client", "Merge.Client\Merge.Client.csproj", "{29A3B1E3-46DB-408A-B042-17362CF042CB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge.Client.Test", "Merge.Client.Test\Merge.Client.Test.csproj", "{F6A9FBE2-C5A7-4E9E-BD14-E83357CC258B}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge.Client.Test", "Merge.Client.Test\Merge.Client.Test.csproj", "{D9C80060-EEBD-4670-8CED-62CD0DC0A531}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -16,13 +16,13 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {F2B96F3A-F41D-4505-8135-B9C6ECEF44F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F2B96F3A-F41D-4505-8135-B9C6ECEF44F0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F2B96F3A-F41D-4505-8135-B9C6ECEF44F0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F2B96F3A-F41D-4505-8135-B9C6ECEF44F0}.Release|Any CPU.Build.0 = Release|Any CPU - {F6A9FBE2-C5A7-4E9E-BD14-E83357CC258B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F6A9FBE2-C5A7-4E9E-BD14-E83357CC258B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F6A9FBE2-C5A7-4E9E-BD14-E83357CC258B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F6A9FBE2-C5A7-4E9E-BD14-E83357CC258B}.Release|Any CPU.Build.0 = Release|Any CPU + {29A3B1E3-46DB-408A-B042-17362CF042CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {29A3B1E3-46DB-408A-B042-17362CF042CB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {29A3B1E3-46DB-408A-B042-17362CF042CB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {29A3B1E3-46DB-408A-B042-17362CF042CB}.Release|Any CPU.Build.0 = Release|Any CPU + {D9C80060-EEBD-4670-8CED-62CD0DC0A531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9C80060-EEBD-4670-8CED-62CD0DC0A531}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D9C80060-EEBD-4670-8CED-62CD0DC0A531}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D9C80060-EEBD-4670-8CED-62CD0DC0A531}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/Merge.Client/Core/JsonEnumMemberStringEnumConverter.cs b/src/Merge.Client/Core/JsonEnumMemberStringEnumConverter.cs new file mode 100644 index 00000000..dc8e64d6 --- /dev/null +++ b/src/Merge.Client/Core/JsonEnumMemberStringEnumConverter.cs @@ -0,0 +1,75 @@ +using System.Reflection; +using System.Runtime.Serialization; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Merge.Client; + +#nullable enable + +/// +/// Handles serializing C# enums into the appropriate string values. +/// +public class JsonEnumMemberStringEnumConverter : JsonConverterFactory +{ + private readonly JsonNamingPolicy? _namingPolicy; + private readonly bool _allowIntegerValues; + private readonly JsonStringEnumConverter _baseConverter; + + public JsonEnumMemberStringEnumConverter( + JsonNamingPolicy? namingPolicy = null, + bool allowIntegerValues = true + ) + { + _namingPolicy = namingPolicy; + _allowIntegerValues = allowIntegerValues; + _baseConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues); + } + + public override bool CanConvert(Type typeToConvert) => _baseConverter.CanConvert(typeToConvert); + + public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) + { + var query = + from field in typeToConvert.GetFields(BindingFlags.Public | BindingFlags.Static) + let attr = field.GetCustomAttribute() + where attr != null && attr.Value != null + select (field.Name, attr.Value); + var dictionary = query.ToDictionary(p => p.Item1, p => p.Item2); + if (dictionary.Count > 0) + return new JsonStringEnumConverter( + new DictionaryLookupNamingPolicy(dictionary, _namingPolicy), + _allowIntegerValues + ).CreateConverter(typeToConvert, options); + else + return _baseConverter.CreateConverter(typeToConvert, options); + } +} + +public class JsonNamingPolicyDecorator : JsonNamingPolicy +{ + readonly JsonNamingPolicy? _underlyingNamingPolicy; + + protected JsonNamingPolicyDecorator(JsonNamingPolicy? underlyingNamingPolicy) + { + _underlyingNamingPolicy = underlyingNamingPolicy; + } + + public override string ConvertName(string name) => + _underlyingNamingPolicy?.ConvertName(name) ?? name; +} + +internal class DictionaryLookupNamingPolicy : JsonNamingPolicyDecorator +{ + readonly Dictionary dictionary; + + public DictionaryLookupNamingPolicy( + Dictionary dictionary, + JsonNamingPolicy? underlyingNamingPolicy + ) + : base(underlyingNamingPolicy) => + this.dictionary = dictionary ?? throw new ArgumentNullException(); + + public override string ConvertName(string name) => + dictionary.TryGetValue(name, out var value) ? value : base.ConvertName(name); +} diff --git a/src/Merge.Client/Core/RawClient.cs b/src/Merge.Client/Core/RawClient.cs index a00e9053..f3175482 100644 --- a/src/Merge.Client/Core/RawClient.cs +++ b/src/Merge.Client/Core/RawClient.cs @@ -49,6 +49,12 @@ public async Task MakeRequestAsync(ApiRequest request) // Add the request body to the request if (request.Body != null) { + var serializerOptions = new JsonSerializerOptions + { + Converters = { new JsonEnumMemberStringEnumConverter() }, + // Set other options as required: + WriteIndented = true, + }; httpRequest.Content = new StringContent( JsonSerializer.Serialize(request.Body), Encoding.UTF8, diff --git a/src/Merge.Client/Merge.Client.csproj b/src/Merge.Client/Merge.Client.csproj index 1c832068..eaaf103f 100644 --- a/src/Merge.Client/Merge.Client.csproj +++ b/src/Merge.Client/Merge.Client.csproj @@ -5,7 +5,7 @@ net7.0 enable false - 0.0.6 + 0.0.7 README.md LICENSE https://github.com/merge-api/merge-csharp-client @@ -21,7 +21,7 @@ - - + + diff --git a/src/Merge.Client/Merge.cs b/src/Merge.Client/Merge.cs index 04933f59..94228d4d 100644 --- a/src/Merge.Client/Merge.cs +++ b/src/Merge.Client/Merge.cs @@ -24,8 +24,8 @@ public Merge( { "Authorization", $"Bearer {apiKey}" }, { "X-Account-Token", accountToken }, { "X-Fern-Language", "C#" }, - { "X-Fern-SDK-Name", "Merge.Client" }, - { "X-Fern-SDK-Version", "0.0.6" }, + { "X-Fern-SDK-Name", "merge_fern_api_sdk" }, + { "X-Fern-SDK-Version", "0.0.7" }, }, clientOptions ?? new ClientOptions() );