Closed repomaa closed 9 years ago
here's a link to the macro in question: https://github.com/jreinert/ddp-crystal/blob/master/src/ddp/message.cr#L8-L20
Aside: there is no need to parse the JSON first and assert it's a Hash, then have yaml_mapping
parse the JSON again. yaml_mapping
will parse and validate the JSON at the same time.
I understand the problem is to extract the message type and then parse as different types, but you may have an intermediary type that uses the pull parser directly (eg: Message.new(pull : YAML::PullParser)
would do the dispatch and return the correct type, that doesn't have to be a Message
).
The actual bug seems to be YAML::PullParser#to_json
which should be defined as YAML::PullParser#to_json(io)
, but I don't really understand why you would end up serializing a YAML::PullParser :confused:
@ysbaddaden s/yaml/json/ig
:P
Your overload in https://github.com/jreinert/ddp-crystal/blob/master/src/ddp/messages/connect.cr#L16 overrides the def initialize(pull)
overload from the json_mapping
, since all attributes are optional and unrestricted it matches Connected.new(pull)
with pull
being a JSON::PullParser
, so @session
becomes String|Nil|JSON::PullParser
. Adding a type restriction like String?
resolves the issue.
It's hard to reproduce. This happens in my repo ddp-crystal when specs are run. This however only works with my branch polymorphic-macro-methods see #1592. since polymorphic macro return types are needed here. I'd be very glad if someone would take the time to checkout the repo and my crystal branch to help me isolate the bug.