Skip to content

Accept missing, null or empty additionalEmails and phoneNumbers in CustomAlerts add/edit API - #324

Merged
snake14 merged 7 commits into
6.x-devfrom
PG-5220
Dec 2, 2026
Merged

snake14 merged 7 commits into
6.x-devfrom
PG-5220

Conversation

@AltamashShaikh

Copy link
Copy Markdown
Contributor

Description

CustomAlerts.addAlert and CustomAlerts.editAlert required additionalEmails and phoneNumbers, even though both are optional lists. A request without them failed with Please specify a value for additionalEmails. Most HTTP form encoders (PHP http_build_query, browsers, SDKs) drop empty arrays, so a client had no clean way to send "no entries". The only thing that worked was a placeholder entry like additionalEmails[]=, which is not documented.

Root cause: the two parameters had no type and no default, so the API proxy treated them as required. With autosanitize it also treats an empty value (additionalEmails=) as missing.

Fix (API.php):

  • additionalEmails and phoneNumbers are now typed ?array in both methods. Parameter order is unchanged and there is no default, so there is no optional-before-required deprecation. For nullable parameters the core API proxy uses null when the value is missing, so missing, null or empty values now reach the method as null.
  • filterPhoneNumbers() now returns [] for an empty or null list and skips empty or null entries. filterAdditionalEmails() already did this; both now cast entries to string before trim(), so [null] and [''] also mean "no entries".
  • PHPDoc says that missing, empty or null means no entries.
  • The two method signatures are now wrapped over several lines, because the longer editAlert line went over the PHPCS line limit.

Backward compatible: requests that send arrays work as before, including the [''] that the Vue edit form sends. reportMediums is unchanged on purpose: at least one channel is still required. emailMe also still has no default and is not part of this change. As with any optional array parameter in Matomo, a non-array value (for example additionalEmails=a@b.com without []) is now treated as no entries. Before, it caused a PHP TypeError.

Issue No

N/A: reported through partner API integration feedback, no GitHub issue.

Steps to Replicate the Issue

  1. POST module=API&method=CustomAlerts.addAlert&name=Test&idSites[]=1&period=day&emailMe=1&metric=nb_visits&metricCondition=greater_than&metricValue=100&comparedTo=1&reportUniqueId=VisitsSummary_get&reportMediums[]=email&format=JSON plus a token, without additionalEmails or phoneNumbers.
  2. Expected: the alert is created with no additional emails and no phone numbers.
  3. Actual: {"result":"error","message":"Please specify a value for additionalEmails"}. The same happens with additionalEmails=&phoneNumbers=, and for editAlert.

How it was tested

  • New integration tests in tests/Integration/ApiTest.php: test_addAlert_shouldAcceptMissingOrEmptyOptionalListFields and test_editAlert_shouldAcceptMissingOrEmptyOptionalListFields. They call the API through Request::processRequest. Each covers 6 input shapes: missing, [], null, [''], [null] and ''. They failed before the fix (General_PleaseSpecifyValue) and pass after it. The whole ApiTest.php passes (74 tests).
  • PHPCS (repo phpcs.xml) and PHPStan (level 5) pass on API.php.
  • Manual check against a test install: before the fix, the request above and the empty-string variant failed and only the []= workaround worked. After the fix, all three create the alert, and the Manage Alerts page lists them.

QA

Checked in a browser on a test install, following the bug's steps before and after the fix.

Steps:

  1. Set up the CustomAlerts test fixture (tests:setup-fixture) and linked the worktree as plugins/CustomAlerts. 2. Before (original API.php): logged in as superUserLogin in headless Chrome and POSTed CustomAlerts.addAlert via the test proxy (a) without additionalEmails/phoneNumbers, (b) with additionalEmails=&phoneNumbers=, (c) with the additionalEmails[]=&phoneNumbers[]= workaround: (a) and (b) returned error General_PleaseSpecifyValue, (c) created alert 5. 3. After (fixed API.php): the same three requests all created alerts (ids 6, 7, 8). 4. Opened Administration > Personal > Alerts: the page renders and lists the created alerts.

Before the fix: addAlert without additionalEmails/phoneNumbers (or with empty values) fails with 'Please specify a value'; only the []= workaround works

before-1.png

Before the fix: Manage Alerts list only has the alert created with the workaround

before-2.png

After the fix: all three addAlert requests create the alert

after-1.png

After the fix: Manage Alerts lists the newly created QA alerts; page works normally

after-2.png

Checklist

  • [✔] Tested locally or on demo2/demo3?
  • [✔] New test case added/updated?
  • [NA] Are all newly added texts included via translation?
  • [NA] Are text sanitized properly? (Eg use of v-text v/s v-html for vue)
  • [NA] Version bumped?
  • [✖] I have understood, reviewed, and tested all AI outputs before use
  • [✔] All AI instructions respect security, IP, and privacy rules
  • [✔] Documentation updated?

🤖 Generated with Claude Code

Ubuntu and others added 2 commits December 1, 2026 05:06
…ert API

CustomAlerts.addAlert and CustomAlerts.editAlert declared additionalEmails and
phoneNumbers without a type and without a default, so the API proxy rejected
any request that omitted them (as standard form encoders do for empty arrays)
with "Please specify a value". Type them as nullable arrays so a missing,
empty or null value means "no entries", and ignore empty/null entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
….0.6

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent: fixed checks broken by this PR (ci / release-date / Release date).

ci / release-date failed: the PR bumped plugin.json to 6.0.6 with a new undated 6.0.6 changelog entry while 6.0.5 (translations) is also undated and not yet tagged/released, so the weekly release would skip 6.0.5.
Fixed as the check suggests: plugin.json is back to 6.0.5 and the fix's changelog line is merged into the unreleased '* 6.0.5' entry, so 6.0.5 is the only undated entry and matches plugin.json (6.0.5 is untagged).
Verified locally: tests/Integration/ApiTest.php OK (74 tests, 228 assertions) against the worktree; PHPCS clean on API.php and tests/Integration/ApiTest.php.

Changed:

  • CHANGELOG.md
  • plugin.json

@AltamashShaikh
AltamashShaikh marked this pull request as ready for review December 1, 2026 07:24
@AltamashShaikh
AltamashShaikh requested a review from a team December 1, 2026 07:24

@snake14 snake14 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing, null and empty additionalEmails / phoneNumbers are now accepted by both addAlert and editAlert. A couple of things to check:

Medium

  1. A non-array value such as additionalEmails=a@example.com (no []) now gets a success response, but the alert is saved without that recipient, and on editAlert the stored list is erased. Before, the request failed, so the client knew. Silently dropping a recipient is worse than that error, so could a value that is present but not an array be rejected with an error naming the parameter, with a test case for it?

Low

  1. The $phoneNumber === '' check in filterPhoneNumbers() can't change the result, since in_array('', $availablePhoneNumbers) is already false on PHP 8, so it can be dropped.

…ditAlert

Auto-sanitized API parameters fall back to null when the request holds a
value of the wrong type, so additionalEmails=a@example.com was answered
with success while the recipient was dropped (and erased on editAlert).
Such a value is now rejected with HTTP 400 naming the parameter, covered
by new integration tests for both methods and both parameters. Also drop
the empty-string check in filterPhoneNumbers(), which could not change
the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent, replying to @snake14:

Missing, null and empty additionalEmails / phoneNumbers are now accepted by both addAlert and editAlert. A couple of things to check:

Medium

  1. A non-array value such as additionalEmails=a@example.com (no []) now gets a success response, but the alert is saved without that recipient, and on editAlert the stored list is erased. Before, the request failed, so the client knew. Silently dropping a recipient is worse than that error, so could a value that is present but not an array be rejected with an error naming the parameter, with a test case for it?

Both points fixed. Plugin API parameters are auto-sanitized, so a value of the wrong type reached the method as null and was saved as no entries; addAlert and editAlert now reject a present non-array additionalEmails or phoneNumbers with HTTP 400 (General_InvalidValueForParameter naming the parameter), while missing, null and empty values are still accepted. New integration tests cover both methods and both parameters and check that no alert is created and the edited alert keeps its stored recipients. The redundant $phoneNumber === '' check in filterPhoneNumbers() is removed.

@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent: pushed edcbf20 for the review feedback.

addAlert and editAlert now reject a non-array additionalEmails or phoneNumbers with HTTP 400 instead of silently saving no recipients (auto-sanitized parameters turned such values into null).
Added integration tests for that case on both methods, updated the docblocks and the existing 6.0.5 changelog text, and removed a redundant empty-string check in filterPhoneNumbers().

Changed:

  • API.php
  • CHANGELOG.md
  • tests/Integration/ApiTest.php

@snake14 snake14 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing and empty lists are accepted, and a non-array additionalEmails or phoneNumbers is now rejected with HTTP 400, including inside a bulk request. One item to check:

Low

  1. The two new shouldFail_IfOptionalListFieldIsNotAnArray tests catch \Exception and only check the message, so a plain Exception would pass too. Asserting BadRequestException and code 400, like test_addAlert_ShouldFail_IfEmptyName does, would pin the HTTP 400 the changelog promises.

Ubuntu and others added 2 commits December 2, 2026 02:10
…ests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…irect API tests

Request::processRequest() replaces the exception by a plain Exception without
code in tests, so the end-to-end tests keep checking the message and stored
state, while new direct API calls assert the exception class and code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent, replying to @snake14:

Missing and empty lists are accepted, and a non-array additionalEmails or phoneNumbers is now rejected with HTTP 400, including inside a bulk request. One item to check:

Low

  1. The two new shouldFail_IfOptionalListFieldIsNotAnArray tests catch \Exception and only check the message, so a plain Exception would pass too. Asserting BadRequestException and code 400, like test_addAlert_ShouldFail_IfEmptyName does, would pin the HTTP 400 the changelog promises.

Added direct API tests for addAlert and editAlert that send the non-array value in the request and expect BadRequestException with code 400 and the General_InvalidValueForParameter message, like test_addAlert_ShouldFail_IfEmptyName. The two existing end-to-end tests still go through Request::processRequest(), which in tests replaces the exception by a plain Exception without code, so they keep checking the message and that nothing was saved; a comment now explains this.

@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

Auto-fix agent: pushed cb7efa1 for the review feedback.

Tests only: new direct API tests pin BadRequestException and HTTP 400 when additionalEmails or phoneNumbers is sent as a non-array to addAlert or editAlert.
The end-to-end Request::processRequest() tests keep checking the message and stored state, since that path drops the exception class and code in tests.

Changed:

  • tests/Integration/ApiTest.php

snake14
snake14 previously approved these changes Dec 2, 2026

@lincolnops lincolnops left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged 6.x-dev and resolved the CHANGELOG.md conflict; the pull request's own changes are as approved before, so the approval stands.

@snake14
snake14 merged commit be34cc7 into 6.x-dev Dec 2, 2026
24 checks passed
@snake14
snake14 deleted the PG-5220 branch December 2, 2026 02:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants