twingly / twingly-amqp

:bus: Ruby gem for RabbitMQ subscribing and publishing
0 stars 0 forks source link

Publishing an Array raises error or yields unexpected result #71

Closed roback closed 5 years ago

roback commented 5 years ago

The problem is that Array responds to #to_h: https://github.com/twingly/twingly-amqp/blob/f0484795f528822f158df3314eb218df25de9379/lib/twingly/amqp/default_exchange_publisher.rb#L15-L21

For example, if you want to publish the message [["1", "2"],["3", "4"]] as JSON you would end up with {"1":"2","3":"4"} which may not be what you would expect.

Trying to publish a message with the following structure [{type: "insert"},{type: "update"}] just raises an error: twingly/amqp/default_exchange_publisher.rb:18:in 'to_h': wrong element type Hash at 0 (expected array) (TypeError).

Same problem in both publisher classes (DefaultExchangePublisher, TopicExchangePublisher).

roback commented 5 years ago

This is something we would need in https://github.com/twingly/zambezi/blob/466b2bbe307a9362eccd2a22aae6e385b8843ee7/script/lib/queue_documents.rb#L168-L175

roback commented 5 years ago

For example, if you want to publish the message [["1", "2"],["3", "4"]] as JSON you would end up with {"1":"2","3":"4"} which may not be what you would expect.

I previously failed to notice that we have some tests that ensure that arrays gets converted to hashes, so this isn't really unexpected behaviour :)

https://github.com/twingly/twingly-amqp/blob/738123d021f9103914037eabe94da615533e9869/spec/integration/twingly/amqp/default_exchange_publisher_spec.rb#L16-L31

roback commented 5 years ago

Not closed in #73 (I accidentally wrote "fix" before the issue number in the PR description 🤦‍♂️ )