Opened 3 years ago
Closed 3 years ago
#2072 closed Defect (worksforme)
Optional API arguments cannot be skipped
| Reported by: | ewinslow | Owned by: | cash |
|---|---|---|---|
| Priority: | high | Milestone: | Elgg 1.7.2 |
| Component: | Core | Version: | 1.7 |
| Severity: | major | Keywords: | |
| Cc: | brettp, ewinslow | Difficulty: |
Description
I have exposed the following function:
function rest_test($one = 1, $two = 2)
{
return $two/$one;
}
expose_function(
'system.api.test',
'rest_test',
array(
'one' => array('type' => 'int', 'required' => FALSE),
'two' => array('type' => 'int', 'required' => FALSE),
)
);
If I call
...?method=system.api.test&two=1&one=2
it does what you think it should, returns 0.5. However,
...?method=system.api.test&two=1
does not do what you think it should. Instead of returning 1, it still returns two, because the argument for one is completely missing.
Right now to get around this I'm having my users do
?method=api.func&opts[arg1]=val1&opts[arg2]=val2
but this is hardly ideal. It seems so unnecessary when they're already named arguments to have to specify them as an array like that.
Change History (5)
comment:1 Changed 3 years ago by cash
- Owner set to cash
- Priority changed from normal to high
- Severity changed from minor to major
- Status changed from new to assigned
- Type changed from unconfirmed defect to confirmed defect
comment:2 Changed 3 years ago by cash
- Milestone changed from Elgg 1.8 to Elgg 1.7.2
comment:3 Changed 3 years ago by ewinslow
Why not just pass NULL into arguments that weren't specified? Or better yet, allow expose_function to specify a default.
expose_function(
'func.name',
'func_name',
array(
'param1' => array('type' => 'int', 'required' => 'false', 'default' => 10),
'param2' => array('type' => 'string', 'required' => 'false', 'default' => ''),
'param3' => array('type' => 'array', 'required' => 'false', 'default' => array()),
),
)
You could even default 'required' to FALSE if 'default' is specified.
comment:4 Changed 3 years ago by cash
I agree!
comment:5 Changed 3 years ago by cash
- Resolution set to worksforme
- Status changed from assigned to closed
- Type changed from confirmed defect to unconfirmed defect
Huh, I should have checked the code before commenting on this. It already works with default parameters. I added 8 months ago and forgot that I did this.

There is no way to call your function in php only specifying the second parameter.
To support this kind of use of optional parameters, we would have to change all exposed methods so that they support a single parameter similar to the new elgg_get_entities() function.
This is very important so I'll give it some more thought. If anyone has a suggestion, please share. Right now I'm leaning toward adding a version parameter to expose_function. If a developer specifies the new version, the function must support the parameters passed as an associative array. Otherwise, it works as before. This would allow us to support your usage while not breaking everyone else's code. I'd deprecate the old usage after a few releases.