It happens a lot. The gray area is that the medicine isn't patented, just the delivery mechanism.https://pubmed.ncbi.nlm.nih.gov/36495532/
And that mechanism can be tweaked and repeatedly patented.
One of the positive cases for patents on medicines is that they are often chemicals. It's fairly simple to tell whether a chemical is the same or different. "Good fences make good neighbors". You infringe the patent or you don't, and you know that in advance.
But if you want to introduce a similar mechanical device to deliver your definitely patent free generic, now you have to roll the dice that the dozens of patents they've taken out on different delivery mechanism don't affect your approach.