Fixed in master and 5.1 branches.
I've included tests for HHH-11296 Closed with the test case added for this issue. HHH-11296 Closed (already fixed in master; will be fixed in 5.1 shortly).