I’ve never heard of birth control referred to as Onanism (though that may be a Catholic thing), but I’ve seen some early 20C books that refer to masturbation that way.
However, by your logic, Uriah’s refusal to go “down to his house” in 2 Samuel 11 should also be grounds for death, since he had an opportunity to sleep with his wife (and thus potentially create a child) but did not take it. Furthermore, under Mosaic law Onan was specifically commanded to marry his brother’s widow to carry on the family name. There is no indication in the text that the specific law he broke was or is intended to apply to all people, let alone that it is some pronouncement on birth control.
Onanism is usually defined as the improper use of the sexual faculties; that is, for sexual pleasure without being open to the possibility of life.