Why would they lie?
If Mary didn’t even explain the circumstances of Jesus’ conception and Incarnation to Joseph, probably the one person who had the greatest right to know— to the point where God had to send an angel to explain to him what was going on— why would anyone else be considered to deserve the technically-correct version of Jesus’ origins?
Betrothal was a much more powerful, legally-binding act in their society than engagement is in ours. And in a clan-based society without DNA testing, being raised by a man was betrothed/married to the woman, I’d expect that to be sufficient for being considered his son.
Once upon a time, I had a teacher who had been adopted. She always referred to her mom as the woman who raised her. There was another woman who had given birth to her and gave her away five minutes later, but she didn’t consider her “mom” in her head. She was just someone who was biologically related to her, but wasn’t her “real mom”, because that role was earned.
Obviously, God vs Joseph isn’t a similar dynamic, but it’s a real-world example of how someone can honestly represent themselves as so-and-so’s child, even if there isn’t any common genetics between them.
On an interesting side note, you’ll notice that there are two different genealogies in the Bible. Both Mary and Joseph were descended from David, but Joseph was descended from a more kingly branch than Mary was. So Jesus, through Mary, was descended from David, but Jesus, through Joseph, was descended through the primary royal line. But there had also been the Curse of Jeconiah, where God extinguishes his line, and says no one from his lineage will ever sit the throne again. So, paradoxically, the curse is able to stay intact, yet Jesus is still able to lay claim to that kingly lineage through Joseph, while still being genetically related to David through Mary.