Page MenuHomePhabricator

Action API returns null for unset bool parameter
Closed, ResolvedPublic

Description

Since rMWc2b15259081b: API: Use ParamValidator library a boolean action API parameter with a definition like [ ApiBase::PARAM_TYPE => 'boolean' ] returns null when unset; previously it returned false. I don't think the old behavior was ever specified so this is not technically a breach of compatibility, but it should be announced if it is intentional / preferred.

Requiring boolean parameters to explicitly specify a default (and issue a warning when that doesn't happen) would also be nice, to help people debug breakage. (E.g. query+growthtasks was broken, although that one was caused by a bool type hint so not exactly hard to debug.)

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

The old code used WebRequest::getCheck and the new code uses ParamValidator which does not even invoke the type class if the value is null. This seems especially confusing for the PresenceBooleanDef (checkbox) type which is documented as

This boolean is considered true if the parameter is present in the request, regardless of value. The only way for it to be false is for the parameter to be omitted entirely.

but actually it will be null when omitted entirely.

Anomie moved this task from Unsorted to Needs Review on the MediaWiki-Action-API board.
Anomie added subscribers: Krinkle, Anomie.

Meh. For good measure I checked Wikimedia-deployed extensions and didn't see anything that would be affected by this. But it seems easy enough to fix.

the new code uses ParamValidator which does not even invoke the type class if the value is null

It always invokes the TypeDef to fetch the value from the request, but doesn't call the validate method if the value is null.

This seems especially confusing for the PresenceBooleanDef (checkbox) type which is documented as

In the Action API, that's the type called "boolean" and is the only boolean type available.

All PresenceBooleanDef::validate() does is cast the value to boolean anyway, so it's not like there's anything really missing by not calling it.

This seems to have no effect on anything. Why do you claim it's a blocker?

Change 570662 had a related patch set uploaded (by Anomie; owner: Anomie):
[mediawiki/core@master] ParamValidator: Default PresenceBooleanDef to false rather than implicitly null

https://gerrit.wikimedia.org/r/570662

Change 570662 merged by jenkins-bot:
[mediawiki/core@master] ParamValidator: Default PresenceBooleanDef to false rather than implicitly null

https://gerrit.wikimedia.org/r/570662

Anomie claimed this task.

This seems especially confusing for the PresenceBooleanDef (checkbox) type which is documented as

In the Action API, that's the type called "boolean" and is the only boolean type available.

How so? ApiBase passes the type unchanged to ParamValidator, which maps boolean to BooleanDef. So I don't think the patch changes anything.

How so? ApiBase passes the type unchanged to ParamValidator, which maps boolean to BooleanDef. So I don't think the patch changes anything.

Look at includes/api/Validator/ApiParamValidator.php, in particular line 46.

Ah, thanks. I missed that ApiMain uses an ApiParamValidator, not a ParamValidator.

@AMooney, I understand that this ticket is resolved. Should it be in the Done column instead of the Waiting for Review one?