Skip to content

Fix Snowflake Codable decoding for Discord payloads#12

Merged
M1tsumi merged 3 commits intomainfrom
fix/snowflake-codable-decoding
Mar 20, 2026
Merged

Fix Snowflake Codable decoding for Discord payloads#12
M1tsumi merged 3 commits intomainfrom
fix/snowflake-codable-decoding

Conversation

@M1tsumi
Copy link
Copy Markdown
Owner

@M1tsumi M1tsumi commented Mar 19, 2026

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:

  • 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.

M1tsumi added 3 commits March 19, 2026 19:49
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
@M1tsumi M1tsumi merged commit a73c50c into main Mar 20, 2026
2 checks passed
@M1tsumi M1tsumi deleted the fix/snowflake-codable-decoding branch March 20, 2026 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant