Fix Snowflake Codable decoding for Discord payloads#12
Merged
Conversation
Snowflake<T> previously relied on synthesized Codable, which expects an object shape like {"rawValue": ...}. Discord APIs provide snowflakes as single-value JSON fields, usually strings (and occasionally numbers in inconsistent payloads), causing decode failures in real responses.
What changed:
- Implemented custom Snowflake Codable using a single value container.
- Snowflake decoding now accepts string, UInt64, and non-negative Int64 values.
- Snowflake encoding now emits the canonical Discord wire format (string).
- Fixed PermissionBitset decoding to accept both UInt64 and decimal-string values.
- Updated PermissionBitset encoding to emit decimal strings for Discord compatibility.
- Added regression tests in CodableDecodingTests covering decode+encode for Snowflake and PermissionBitset.
Impact:
- Resolves reported snowflake decode failures against live Discord responses.
- Hardens model decoding against mixed numeric/string payload variations.
This commit addresses multiple reliability defects that could cause interaction loss, runtime instability, or hard crashes under edge input conditions. Interaction and gateway hardening: - Make Interaction.ApplicationCommandData.name optional so component/modal interaction payloads without a command name decode correctly. - Update SlashCommandRouter to require a non-empty command name only for slash dispatch and keep path computation robust when name is absent. - Add INTERACTION_CREATE decode fallback behavior in GatewayClient to emit raw events and, when enabled, gateway decode diagnostics. - Introduce DiscordConfiguration.enableGatewayDecodeDiagnostics (default: false) and add actionable diagnostic logging with event/op/seq context and payload preview. Stability fixes: - Prevent dictionary-mutation-while-iterating hazards in ViewManager one-shot cleanup by routing over a snapshot and unregistering after iteration. - Prevent dictionary-mutation-while-iterating hazards in Cache.removeMessage by iterating over a snapshot. - Remove force-unwrapped UDP port conversions in voice sender/receiver/discovery paths to avoid avoidable runtime crashes on invalid port values. Regression tests added: - InteractionDecodingTests: verifies component interaction decoding without command name and slash decoding with command name. - ViewManagerTests: verifies one-shot views are removed after matching interaction handling. - CacheTests: verifies message removal from per-channel recent cache behavior. - DiscordConfigurationTests: verifies gateway decode diagnostics default and opt-in behavior. Notes: - Environment here does not have Swift toolchain installed, so swift test could not be executed locally in this session.
…in CacheTests - Replaced hardcoded memberwise initializer calls for User and Message with JSON-based construction helpers - Ensures test resilience against future model field changes - Test logic unchanged: validates cache message eviction works correctly - Fixes Swift 6.2 compiler errors on both Windows and macOS CI
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Snowflake previously relied on synthesized Codable, which expects an object shape like {"rawValue": ...}. Discord APIs provide snowflakes as single-value JSON fields, usually strings (and occasionally numbers in inconsistent payloads), causing decode failures in real responses.
What changed:
Impact: