Patch written by Simon. I have only added a testcase. (cherry picked from commit 364258e0)
mentioned in issue #18620