These middle names formed from surnames are certainly potentially helpful, but we can't assume they're necessarily copied from a direct ancestor. In my own family I can think of one case where the surname comes from an uncle by marriage, another where it's from the landlord of the family's tenanted farm.
In your family one which stands out, as it's an unusual name and so hopefully easy to trace, is Densem. John Miles's sister Caroline Dickman Boon has a daughter name Caroline Densem Boon and a witness at his sister Sarah Ann's wedding was James Densem. Although I've never met the name before it turns out there were quite a few in Bath, some of them painters. So perhaps they were just work partners of the Miles family. But then we find James Densem married a Mary Ann Miles, born about 1823 to James and Sarah Miles! This is not John's parents. They baptised several children in Bath and Bathwick from 1813 onwards and so were much older.In the 1851 census they are to be found at Petersburgh Place, the street where your James and Sarah lived earlier and John was born. The census reveals that they came from Maiden Bradley and Hill Deverill in Wiltshire respectively. They married at Hill Deverill in 1805. That raises the interesting question, where were they from 1805 to 1813? Could it be Cheltenham, and they are your James's parents?
On a different tack, your James and Sarah had two sons called Charles, both died young, and then their surviving son John took on Charles for a middle name. That makes it quite likely that James's father was called Charles, though it could come from Sarah's father or just someone else who was important to them.
A very puzzling family, I'm afraid I can't thing of any other approaches.
David