…tion when deserializing the json response from Discord
Make sure you familiarize yourself with our contributing guidelines. (delete this line afterwards)
This pull request was written by AI
This pull request was assisted by AI, but you wrote the final code
This pull request did not involve AI in any way
Summary
GetStickerPacksAsync() threw exception when used. Now it does not throw an exception.
The issue was with the json deserialization. The response from discord's API did not match the DiscordMessageStickerPack object.
Details
Stack trace from my own application:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.UInt64,DSharpPlus.Entities.DiscordMessageSticker]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'sticker_packs[0].stickers', line 1, position 216.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
at Newtonsoft.Json.Linq.JToken.ToObject[T](JsonSerializer jsonSerializer)
at DSharpPlus.Net.Serialization.DiscordJson.ToDiscordObject[T](JToken token) in /app/DSharpPlus-Repo/DSharpPlus/Net/Serialization/DiscordJson.cs:line 64
at DSharpPlus.Net.DiscordApiClient.GetStickerPacksAsync() in /app/DSharpPlus-Repo/DSharpPlus/Net/Rest/DiscordApiClient.cs:line 868
at LundBot.Services.WelcomeMessageService.GetWelcomeStickersAsync() in /app/src/Services/WelcomeMessageService.cs:line 126
at LundBot.Services.Discord.DiscordInteractionService.HandleWelcomeInteractionAsync(DiscordUser user, DiscordChannel channel) in /app/src/Services/Discord/DiscordInteractionService.cs:line 101
at LundBot.Services.Discord.DiscordInteractionService.HandleComponentInteractionAsync(ComponentInteractionCreateEventArgs e) in /app/src/Services/Discord/DiscordInteractionService.cs:line 87
at LundBot.Services.BotService.OnComponentInteractionCreated(DiscordClient sender, ComponentInteractionCreateEventArgs e) in /app/src/Services/BotService.cs:line 107
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…tion when deserializing the json response from Discord
Make sure you familiarize yourself with our contributing guidelines. (delete this line afterwards)
Summary
GetStickerPacksAsync() threw exception when used. Now it does not throw an exception.
The issue was with the json deserialization. The response from discord's API did not match the DiscordMessageStickerPack object.
Details
Stack trace from my own application:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.UInt64,DSharpPlus.Entities.DiscordMessageSticker]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'sticker_packs[0].stickers', line 1, position 216. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer) at Newtonsoft.Json.Linq.JToken.ToObject[T](JsonSerializer jsonSerializer) at DSharpPlus.Net.Serialization.DiscordJson.ToDiscordObject[T](JToken token) in /app/DSharpPlus-Repo/DSharpPlus/Net/Serialization/DiscordJson.cs:line 64 at DSharpPlus.Net.DiscordApiClient.GetStickerPacksAsync() in /app/DSharpPlus-Repo/DSharpPlus/Net/Rest/DiscordApiClient.cs:line 868 at LundBot.Services.WelcomeMessageService.GetWelcomeStickersAsync() in /app/src/Services/WelcomeMessageService.cs:line 126 at LundBot.Services.Discord.DiscordInteractionService.HandleWelcomeInteractionAsync(DiscordUser user, DiscordChannel channel) in /app/src/Services/Discord/DiscordInteractionService.cs:line 101 at LundBot.Services.Discord.DiscordInteractionService.HandleComponentInteractionAsync(ComponentInteractionCreateEventArgs e) in /app/src/Services/Discord/DiscordInteractionService.cs:line 87 at LundBot.Services.BotService.OnComponentInteractionCreated(DiscordClient sender, ComponentInteractionCreateEventArgs e) in /app/src/Services/BotService.cs:line 107Changes proposed
Fixing the DiscordMessageStickerPack object.
Notes
Any additional notes go here.
Proof of test:
