tempest-framework icon indicating copy to clipboard operation
tempest-framework copied to clipboard

Support serializing DTOs for front-end consumption

Open innocenzi opened this issue 4 months ago • 3 comments

Here is the setup. I have a user model with a DTO, which gets serialized properly to the database, and casted back as well. This works and it looks like that:

{
  "full_name": "Jon Doe"
  "metadata": "{\"type\":\"metadata\",\"data\":{\"theme\":\"dark\"}}"
}

However, I have another serialization context. Instead of serializing for the database, I want to serialize for front-end consumption. This means that the DTO should be serialized as an actual array, not as a JSON string. This is what I need, instead of the above:

{
  "full_name": "Jon Doe"
  "metadata": {
    "type": "metadata",
    "data": {
      "theme": "dark",
    }
  }
}

I assume I could do map($objectFetchedFromDatabase)->with(SomeDedicatedMapper::class)->do(), but I don't think that's ideal. Imagine having to do that for every API or Inertia endpoint.

So maybe the specific JSON-string mapping (example 1) should only be done in a database context, and the normal mapping (example 2) should be the default behavior.

innocenzi avatar Aug 17 '25 16:08 innocenzi

@brendt do you think we can tackle this next? It's a pretty important topic imo, bigger than it seems, and might imply breaking changes (or not)

innocenzi avatar Oct 13 '25 08:10 innocenzi

Yes, we can. Do you mean you want me to look at it?

brendt avatar Oct 17 '25 06:10 brendt

Yes, I haven't worked too much on the mapper and your insight on this topic (the way to solve this) would be welcome

innocenzi avatar Oct 17 '25 12:10 innocenzi