Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-19756

REST API cannot handle {} values (empty associative arrays)

    Details

    • Type: Bug
    • Status: Open
    • Priority: Trivial
    • Resolution: Unresolved
    • Affects Version/s: 4.7.14
    • Fix Version/s: None
    • Component/s: CiviCRM API
    • Labels:
      None
    • Versioning Impact:
      Minor (add functionality in backwards-compatible manner)
    • Documentation Required?:
      Developer Doc
    • Funding Source:
      Needs Funding
    • Verified?:
      No

      Description

      Suppose civicrm_api() returns any value with array().

      For example: civicrm_api3('Mailing','get',...) might return a $result where template_options is array().

      Now suppose we call this via REST. This runs civicrm_api and passes the output to json_encode for encoding. Since PHP treats numeric arrays and associative arrays as the same thing, it is ambiguous whether json_encode should output that as "[]" (empty JSON numeric array) or as "{}" (empty JSON associative array).

      This leads to subtle buggy symptoms. Continuing our example where "template_options" is misconstrued as "[]" instead of "{}", the client will happily parse this, and JS code can even work with it. The statement mailing.template_options.foo = "bar"; would execute because "template_options" is an object of type Array. However, it cannot be serialized back to JSON correctly – the "foo" property will be ignored (because JS Array's are supposed be converted to JSON "[]").

      I'm really not sure how to fix this generally – one way or another, it feels like we'd need to have special-case logic or metadata (either on the PHP-side for encoding or the JS-side for decoding) which says "field #1 is intended to be numeric-array, and field #2 is intended to be associative-array".

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              timotten Tim Otten
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: