A swarm may enter another creatures space, but how big is that space?
A medium creature occupies one square on the grid, and one square on the 5th edition battle grid represents 5 feet. A creature in a square is not a solid 5X5X5 cube but a moving, dodging, threatening being. At the moment a spell, such as Fire Bolt is cast, for the sake of narrative, it does not have to be at the moment a swarm is necessarily crawling on said creature, but just within the same 5 foot space. Whoever is describing the results of the action (usually the DM) could describe it like this:
You point your finger and concentrate on the swarm. A bolt of fire launches from your fingertip and strikes the ground where the swarm of ants seems the most dense. Roll 1d10 fire damage against the swarm.
Or
The swarm of deadly mosquitoes temporarily disengages from your friend, and you take the opportunity to quickly shoot a bolt of fire into the mass of insects hovering in the air, inches from your ally. Roll 1d10 fire damage against the swarm.
Just because a swarm is within a creature's space doesn't mean you have to visualize (or narrate) that it is on the creature the whole time it is there. It is simply in proximity to the other creature, close enough to attack it.
An Alternative
For the more vindictive DMs out there, you could enforce disadvantage on the attack roll against the swarm, use the cover rules like suggested in the other answer, or even take inspiration from this part of the Fire Bolt spell:
A flammable object hit by this spell ignites if it isn't being worn or carried.
Certain spells may have dangerous environmental effects and, still using Fire Bolt as the example, a dry bush near the creature may catch fire and pose a danger or perhaps a piece of their clothing.
Of note, if you go this direction, is the Sculpt Spells ability of the Evocation Wizard found on pg.117 of the PHB. It allows the wizard to protect a limited amount of allies from dangerous spell effects that he/she casts. This would suggest, to me, that shooting a magical bolt of fire into your allies space should be a dangerous thing to do.
All of these options, though, are house rules and make the system more biased against the players than intended by the designers.