From my understanding it only happened cos the fans (and many In the comments are disgusted they rooted for it. Without seeing how wrong it was)
Ezra was a big red flag, he liked minors and knew aria was one..and before her loved Alison. Hint why he became a teacher.
My understanding is the actual source material made it known his acts was criminal. Etc.
But the TV show actually played a long with 2000 and 90s tropes of normalizing and embracing problematic stuff, this wasn't the only show and won't be the last time, it still happens (Riverdale with miss grundy and Archie hooking up).