Search code examples
javaopenapi-generator

Using my own model library in openapi generated client


I have a Java user-service microservice composed of two sub-modules, one for the API itself (springboot, exposing the controllers) and one for the model (java library containing all the model classes: User, Company...). The model library is used by other consumer microservices as a dependency. I'm trying to use an openapi generated client to consume the user-service. I generated the Java client with this command line:

openapi-generator generate -i openapi.yaml \
  -o ./generated_client/  \
  -g java  \
  --additional-properties=library=webclient

but I see that a full new model has been generated inside the client. I want instead that my model library is used. I see that import mappings can be specified, so I tried the following command line:

openapi-generator generate -i openapi.yaml \
  -o ./generated_client/ \ 
  -g java \
  --import-mappings User=com.mypackage.userservice.model.entity.user.User \
  --additional-properties=library=webclient

Now the import is correct: the correct User class is used in generated code.

The problem is that now the generated POM is missing a dependency... What should I do now to be able to compile and build the generated code?

I need to "point" to the model library inside the POM... but I don't think that manually modifying the POM would be correct. Is there a way to instruct openapi-generator to add the needed external dependency to the generated POM?

Update:

I'm trying to resolve the dependency setting a parent pom... but the parent POM can't be the model library itself, because its packaging is "jar"...

openapi-generator generate -i openapi.yaml \
  -o ./generated_client/ \ 
  -g java \ 
  --import-mappings=User=com.package.userservice.model.entity.user.User \
  --additional-properties=library=webclient,groupId=com.package,parentArtifactId=user-service-pom,parentGroupId=com.package,parentVersion=1.87.182

Solution

  • What you are looking for is templating. The pom that is generated by the java generator depends on which library you have specified in your generation command. Since you are using the webclient, this template is used (Note: you can also get this and all the other templates using the cli command openapi-generator author template -g java --library webclient). You can copy the pom.mustache template into your own templates folder, add the necessary new dependencies, and then add the template folder to your generator command.

    In order to add the template folder to your generator, you will need to add the -t flag to your generation command, and then specify the directory location of the new template. For example, -t ~/.openapi-generator/templates/Java would use the Java folder, inside the templates folder, inside the .openapi-generator folder.