javaspring-bootswaggeropenapicode-generation

Should i map classes generated from OpenApi generator plugin to Entity classes


i have defined an openapi specification and am currently generating the relevant classes (models) from the specifications in my target folder using the openapi-generator-maven-plugin. I was wondering whether its best practice to map the generated classes to an entity class before persisting the objects in a postgres database or whether i can skip this mapping step and reduce code and maintenance overhead of two classes.

Doing preliminary research


Solution

  • I suggest not doing that. Let's keep the internals (domain entities) and externals (openapi-generated DTOs) of the application separate. It will pay back, when you want to eg. make a small change on the API which shouldn't impact the domain, or vica versa, do some refactoring on the domain entities without breaking the REST API.

    If you are worried about maintenance cost, you may use MapStruct or similar library for automatic mapping between layers, as long as that mapping is (nearly) trivial.