This commit applies the changes made in 68b55ad to 1.2.x (it was originally only made in 1.0.x and master). It also adds some tests. Closes gh-3803
68b55ad