Make Payload case-insensitive (Fixes #108)#113
Open
russ wants to merge 1 commit into
Open
Conversation
Crystal's JSON parsing is case-sensitive, so a payload like
`{"Command": "Subscribe"}` failed two ways:
1. `Payload.from_json` raised `Missing JSON attribute: command` because the
key was `Command` rather than `command`.
2. Even once parsed, `Connection#receive` dispatched on `payload.command ==
"subscribe"`, so a `Subscribe` value never matched.
Fix both:
- `Payload.from_json` normalizes the top-level keys (command/identifier/data)
to lowercase before deserializing. Nested values — channel names and the
identifier/data blobs — are left exactly as sent, since those stay
case-sensitive.
- `Connection#receive` compares the command case-insensitively.
Adds a Payload unit spec for mixed-case top-level keys and an end-to-end
`#receive` spec exercising a `{"Command": "Subscribe", ...}` payload.
Fixes cable-cr#108
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BMW1EsdCMExBhm3tg7TQhs
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.
Problem
Fixes #108.
Crystal's JSON parsing is case-sensitive, so a payload like
{"Command": "Subscribe"}failed two separate ways:Payload.from_jsonraisedMissing JSON attribute: commandbecause the key wasCommand, notcommand.Connection#receivedispatched onpayload.command == "subscribe", so aSubscribevalue never matched.Fix
Payload.from_jsonnormalizes the top-level keys (command/identifier/data) to lowercase before deserializing. Only the top-level keys are touched — the nested values (channel names and the identifier/data blobs) are left exactly as sent, since those stay case-sensitive (e.g. the channel name still has to match the registered channel class). Invalid payloads still raiseJSON::SerializableError/JSON::ParseExceptionas before, so theCable::Handlererror path is unchanged.Connection#receivecompares the command case-insensitively (payload.command.downcase).Tests
Payloadunit spec parsing mixed-case top-level keys ("Command","Identifier","DATA") and confirmingcommand,channel,data, andactionall resolve.#receivespec sending{"Command": "Subscribe", "Identifier": ...}and asserting it confirms the subscription.Both specs fail on
masterwith the exactMissing JSON attribute: commandfrom the issue, and pass with this change. Full suite is green locally on Crystal 1.10.0 and current stable.Note
The
nightlyCI job iscontinue-on-error: trueand currently fails repo-wide because ameba won't compile against Crystal nightly (undefined method 'next_string_array_token') — it fails duringshards install, before any project code, so it's unrelated to this change.🤖 Generated with Claude Code