I've added a test case to reproduce this issue and pushed to 5.0, 5.1, and master branches.
The fix for this issue in 5.1 and master branches was tracked by a different jira (not sure which one).