Allow API parameter type hints #19984
Labels
c: APIs
For bugs and features in the Matomo HTTP and plugin APIs.
c: Platform
For Matomo platform changes that aren't impacting any of our APIs but improve the core itself.
Major
Indicates the severity or impact or benefit of an issue is much higher than normal but not critical.
Stability
For issues that make Matomo more stable to run for sys admins.
Milestone
Summary
Currently it is not possible to use type hints in API methods. As all parameters are parsed from the request, they are currently always either a string or an array. Adding type hints other then string or array would though currently end up in an error of mismatching types.
Allowing type hinting for API methods would help us solve a plenty of issues around incorrect parameter types. In the past we had a lot issues like that, where e.g. someone passed an array as
period
ordate
parameter (e.g. like #19222), causing the API to throw an uncaught exception, as it was used in a way, where an array couldn't be handled.To implement that we could simply do some parameter validation in the API proxy, based on the type hint we can receive using reflection. Passing that type to
Common::getRequestVar
, which we use, to receive all parameter values should do the trick.Needs to be changed somewhere around here:
matomo/core/API/Proxy.php
Lines 409 to 441 in a01eea3
Note: This issue is not about adding type hints to all our API methods. For now we should only provide the possibility, so we can add it when already working on certain APIs later.
@mattab I've discussed this improvement today with @tsteur and @justinvelluppillai
It would be awesome to include this in Matomo 5. We could in theory also do it in a later release, but if we need to add a type hint in a plugin then, we would need to increase the minimum required version, to the version where this got implemented.
I already had a look at the code, and it should only take around 2 hours to implement that.
The text was updated successfully, but these errors were encountered: