Skip to content

Commit

Permalink
fix: Add converter for integration types config when writing manually
Browse files Browse the repository at this point in the history
  • Loading branch information
VelvetToroyashi committed May 11, 2024
1 parent 4e6114f commit 4b880e8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
using Remora.Discord.API.Rest;
using Remora.Discord.API.VoiceGateway.Commands;
using Remora.Discord.API.VoiceGateway.Events;
using Remora.Rest.Core;

Check warning

Code scanning / InspectCode

Redundant using directive Warning

Using directive is not required by the code and can be safely removed
using Remora.Rest.Extensions;
using Remora.Rest.Json;
using Remora.Rest.Json.Policies;
Expand Down Expand Up @@ -1228,6 +1229,8 @@ private static JsonSerializerOptions AddOAuth2ObjectConverters(this JsonSerializ

options.AddDataObjectConverter<IApplicationOAuth2InstallParams, ApplicationOAuth2InstallParams>();

options.Converters.Insert(0, new EnumKeyDictionaryConverterFactory.EnumKeyDictionaryConverterInner<ApplicationIntegrationType, IApplicationIntegrationTypeConfig>(options));

return options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,24 @@ public override bool CanConvert(Type typeToConvert)
options
);

private class EnumKeyDictionaryConverterInner<TEnumKey, TValue> : JsonConverter<IReadOnlyDictionary<TEnumKey, TValue>>
/// <inheritdoc />
internal class EnumKeyDictionaryConverterInner<TEnumKey, TValue> : JsonConverter<IReadOnlyDictionary<TEnumKey, TValue>>
where TEnumKey : struct, Enum
{
private readonly JsonConverter<TValue> _valueConverter;
private readonly JsonConverter<TEnumKey> _keyConverter;

/// <summary>
/// Initializes a new instance of the <see cref="EnumKeyDictionaryConverterInner{TEnumKey, TValue}"/> class.
/// </summary>
/// <param name="options">The serializer options.</param>
public EnumKeyDictionaryConverterInner(JsonSerializerOptions options)
{
_valueConverter = (JsonConverter<TValue>)options.GetConverter(typeof(TValue));
_keyConverter = new StringEnumConverter<TEnumKey>(asInteger: true);
}

/// <inheritdoc />
public override IReadOnlyDictionary<TEnumKey, TValue> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
Expand Down Expand Up @@ -104,6 +110,7 @@ public override IReadOnlyDictionary<TEnumKey, TValue> Read(ref Utf8JsonReader re
return result;
}

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, IReadOnlyDictionary<TEnumKey, TValue> value, JsonSerializerOptions options)
{
writer.WriteStartObject();
Expand Down

0 comments on commit 4b880e8

Please sign in to comment.