Generated with the help of an LLM.
Problem/Motivation
The completion endpoint of Moodle Sync Completion (POST /api/moodle-sync-completion) expects an object with one entry per course node id, each entry an object with an optional total and an optional users object. Data of another shape is not checked: an entry that is not an object, such as {"12": 1} or {"userid": 1, "courseid": 1}, makes array_key_exists() throw a TypeError (line 72 of MoodleCompletionRestResource.php), and the endpoint answers HTTP 500 with Drupal's error page instead of telling the caller what is wrong.
Steps to reproduce
- Enable Moodle Sync Completion and its REST resource with an API key.
- Send
{"12": 1}with a valid key:curl -X POST -H 'Content-Type: application/json' -H 'apikey: …' -d '{"12":1}' https://example.com/api/moodle-sync-completion?_format=json - The answer is 500; the log shows TypeError: array_key_exists(): Argument #2 ($array) must be of type array, int given.
Proposed resolution
Check the shape of the decoded body before anything is changed and answer 400 with a message that names the wrong part, e.g. Invalid data: the entry of course 12 is not an object. The empty body of Moodle's connection test keeps its answer. A static invalidData() method makes the check testable; a unit test covers the accepted shapes and every refused one.
Remaining tasks
- Review the merge request.
User interface changes
None.
API changes
The endpoint answers 400 instead of 500 to data of another shape. Data of the expected shape is processed as before.
Data model changes
None.
Issue fork moodle_sync-3638136
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
daniel.pernold commentedMerged into main and released with 1.1.7.