Skip to content

Commit

Permalink
Better handle null responses (returning acceptable "empty" response)
Browse files Browse the repository at this point in the history
Still there is some case not covered (empty returns_desc and
not null returns), but we aren't looking to them here.
  • Loading branch information
stronk7 committed Sep 20, 2023
1 parent 01b5784 commit 641c3b6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Fixed
- Better behaviour when the external functions return null results, by providing and "acceptable" empty (xmlrpc-compliant) response.

## [1.0.3] - 2023-09-16
### Added
Expand Down
18 changes: 10 additions & 8 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,17 @@ protected function prepare_response() {
// TODO: Remove this branch condition once Moodle 4.1 is out of support.
$validatedvalues = external_api::clean_returnvalue($this->function->returns_desc, $this->returns);
}
$encodingoptions = [
"encoding" => "UTF-8",
"verbosity" => "no_white_space",
// See MDL-54868.
"escaping" => ["markup"]
];
// We can now convert the response to the requested XML-RPC format.
$this->response = xmlrpc_encode_request(null, $validatedvalues, $encodingoptions);
} else if ($this->returns === null) {
$validatedvalues = $this->returns;
}
$encodingoptions = [
"encoding" => "UTF-8",
"verbosity" => "no_white_space",
// See MDL-54868.
"escaping" => ["markup"],
];
// We can now convert the response to the requested XML-RPC format.
$this->response = xmlrpc_encode_request(null, $validatedvalues, $encodingoptions);
} catch (invalid_response_exception $ex) {
$this->response = $this->generate_error($ex);
}
Expand Down
11 changes: 8 additions & 3 deletions tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,20 @@ public function prepare_response_provider() {
'Ennyn Durin, Aran Moria: pedo mellon a minno',
'Mellon!',
'<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><string>Mellon!</string></value>'
. '</param></params></methodResponse>'
. '</param></params></methodResponse>',
],
'Description with non-Latin glyphs' => [
'What biscuits do you have?',
// V Unicode 9! V.
'😂🤵😂 𝒪𝓃𝓁𝓎 𝓉𝒽𝑒 𝒻𝒾𝓃𝑒𝓈𝓉 𝐼𝓉𝒶𝓁𝒾𝒶𝓃 𝒷𝒾𝓈𝒸𝓊𝒾𝓉𝓈 😂🤵😂',
'<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><string>'
. '😂🤵😂 𝒪𝓃𝓁𝓎 𝓉𝒽𝑒 𝒻𝒾𝓃𝑒𝓈𝓉 𝐼𝓉𝒶𝓁𝒾𝒶𝓃 𝒷𝒾𝓈𝒸𝓊𝒾𝓉𝓈 😂🤵😂</string></value></param></params></methodResponse>'
]
. '😂🤵😂 𝒪𝓃𝓁𝓎 𝓉𝒽𝑒 𝒻𝒾𝓃𝑒𝓈𝓉 𝐼𝓉𝒶𝓁𝒾𝒶𝓃 𝒷𝒾𝓈𝒸𝓊𝒾𝓉𝓈 😂🤵😂</string></value></param></params></methodResponse>',
],
'Null returned data' => [
'Some desc is coming!',
null,
'<?xml version="1.0" encoding="UTF-8"?><methodResponse><params/></methodResponse>',
],
];
}

Expand Down

0 comments on commit 641c3b6

Please sign in to comment.