capella-collab-manager
capella-collab-manager copied to clipboard
Snake case to Camel case mapper for types
Currently, we sometimes have to maintain two different types in the frontend, one that maps the backend response containing snake case keys (e.g., project_slug) and another camelCase conform type for the semantically same type. This is very inconvenient as it decreases the maintainability by including unnecessary types and also led to the need of simple but annoying mapper functions (i.e., simply mapping the snake case to camel case type).
One solution for that can be seen here: CamelCase Models with FastAPI and Pydantic. Here the mapping is achieved by simply creating another abstraction between the BaseModel
and the actual pydantic types that transforms the field name to camel case. This would obviously be a breaking change, as our return objects are then in camel case but this should't be a problem. The advantage of this approach is, that we could iteratively apply it to our models, as we just have to change BaseModel
to something like CamelCaseModel
(or CamelCaseBaseModel
) so we don't have to adjust all models in the frontend at once.
Another solution could be to add this mapping as an interceptor in the frontend. The advantage of this approach would be that we keep the snake case responses but on the other side it would then be harder to make the transition towards fully camel case types iteratively. In addition, I think that the backend solution is easier to implement but I didn't check concrete solutions for the frontend up to now.
I don't the like the approach to specify an alias for each element, but the automatic alias generator looks good to me.
However, I prefer the frontend solution. Reason is that we may want to use the API with Python in the future. The Python client would have to use camelCase then or transform it, which is also not very convenient.