prolic / HumusAmqp

PHP 7.4 AMQP library
https://humusamqp.readthedocs.io/
MIT License
76 stars 17 forks source link

stdClasses are not allowed for JsonRpcResponse #67

Closed eyudkin closed 6 years ago

eyudkin commented 6 years ago

For now there is a condition:

JsonRpcResponse payload 'must only contain arrays and scalar values' It looks incorrect because of json encoding/decoding at php side. I can arrange with my RPC-api clients, that API will return structure like:

{
  "messages: [] // list (array)
  "context": {}, // object (key-valued)
}

But if I'll will use only arrays and will want to return an empty object:

function myDeliveryCallback($request) {
  return [
    'messages' => [], // want empty array
    'context' => [], // want empty object
  ];
}

It will return me json:

{
  "messages":[], // empty array
  "context":[] // empty array too! But clients will await empty object from me.
}

So, looks like with current assertion I can not return empty object which will cause problems for clients, written on languages with strict typing (golang/c/etc).

I think, best solution is to allow stdClasses, because they are natively mapping to json-objects i.e. empty objects in empty stdClass case.

prolic commented 6 years ago

I suggest you do the following:

function myDeliveryCallback($request) {
  return [
    'messages' => [], // want empty array
    'context' => null, // null can be treated as empty object
  ];
}