There were two issues here. Fixed via:
https://github.com/discourse/discourse/commit/002e1f560a657bcc85d6668689da51ef46ab8f2c
and
https://github.com/discourse/discourse/commit/c6409fd2cc98bf53caddbbcfc81359dbd4a820c2
Thanks for reporting this issue @mbauman