Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Merge.Client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
75 changes: 75 additions & 0 deletions src/Merge.Client/Core/JsonEnumMemberStringEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -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

/// <summary>
/// Handles serializing C# enums into the appropriate string values.
/// </summary>
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<EnumMemberAttribute>()
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<string, string> dictionary;

public DictionaryLookupNamingPolicy(
Dictionary<string, string> 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);
}
6 changes: 6 additions & 0 deletions src/Merge.Client/Core/RawClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public async Task<ApiResponse> 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,
Expand Down
6 changes: 3 additions & 3 deletions src/Merge.Client/Merge.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<NuGetAudit>false</NuGetAudit>
<Version>0.0.6</Version>
<Version>0.0.7</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/merge-api/merge-csharp-client</PackageProjectUrl>
Expand All @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE" Pack="true" PackagePath=""/>
</ItemGroup>
<None Include="..\..\LICENSE" Pack="true" PackagePath=""/>
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/Merge.Client/Merge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down