Mandatory keys check fails erroneously for float 0.0 value in civicrm_api3_verify_mandatory()
Overview
The mandatory keys check in function civicrm_api3_verify_mandatory()
fails erroneously if the key exists but it has the floating-point value of 0.0.
Reproduction steps
Using APIv3, create an entity that contains a required key with the floating-point value of 0.0, for example, line_total in a price field element.
Current behaviour
APIv3 throws a Mandatory key(s) missing from params array:
exception.
Expected behaviour
Successful creation.
Environment information
- Browser: Safari
- CiviCRM: 5.29.1
- PHP: 7.3.9_
- CMS: Drupal 7.73
Comments
To fix, in civicrm/api/v3/utils.php, change line 92 from
if (!array_key_exists($key, $params) || (empty($params[$key]) && $params[$key] !== 0 && $params[$key] !== '0')) {
to
if (!array_key_exists($key, $params) || (empty($params[$key]) && $params[$key] !== 0.0 && $params[$key] !== '0')) {
The second version will match on both integer 0 and float 0.0, while the first only matches on integer 0 but not float 0.0.