discordgo icon indicating copy to clipboard operation
discordgo copied to clipboard

Deserialization of discord template into DiscordTemplate struct fails

Open merlinfuchs opened this issue 1 year ago • 4 comments

Deserializing a discord template into the DiscordTemplate struct doesn't work as it's using the standard Guild struct for the serialized_source_guild field. See https://github.com/bwmarrin/discordgo/blob/a90485df0cd41873bc381e79f8fb6bb99c05f48b/structs.go#L1103

This fails because all IDs in the discord template data are integers instead of strings.

merlinfuchs avatar Jun 22 '23 21:06 merlinfuchs

Since this is a bug with the Discord API (snowflakes must always be returned as strings in the HTTP API), I have created an issue on their side: https://github.com/discord/discord-api-docs/issues/6609

In the meantime, discordgo may want to consider using json.Number as the type for snowflake fields, since json.Unmarshal is capable of decoding both numbers and strings into fields of this type. Other fixes may resut in runtime errors if/when Discord fixes the return type in the API.

kagadar avatar Jan 10 '24 08:01 kagadar

This has always been the case for templates, so I honestly don't think it's a bug. The snowflakes in guild templates are just serial numbers starting at 0. Changing it now would be a breaking change in the Discord API which won't happen until the next major version. Let's wait for a response from Discord staff on the issue tho

merlinfuchs avatar Jan 10 '24 10:01 merlinfuchs

This has always been the case for templates, so I honestly don't think it's a bug. The snowflakes in guild templates are just serial numbers starting at 0. Changing it now would be a breaking change in the Discord API which won't happen until the next major version. Let's wait for a response from Discord staff on the issue tho

I agree it would be a breaking change, but it's still a bug, since the field is snowflake:

Because Snowflake IDs are up to 64 bits in size (e.g. a uint64), they are always returned as strings in the HTTP API to prevent integer overflows in some languages

Returning an int breaks the invariant that snowflake is always a string.

I don't know what the policy on breaking changes is in this project, but I'll have a look into changing snowflake fields here to json.Number.

kagadar avatar Jan 10 '24 10:01 kagadar

Returning an int breaks the invariant that snowflake is always a string.

I don't know what the policy on breaking changes is in this project, but I'll have a look into changing snowflake fields here to json.Number.

I don't think we need to update all snowflake fields to support number values. Because these fields aren't actually snowflakes (see the attachment) and this behavior occurs only in the serialized guild object.

image

FedorLap2006 avatar Feb 16 '24 14:02 FedorLap2006