Reputation: 43
I am using OpenApi generator to create server code (jaxrs-jersey). I want to prevent certain classes from being generated.
In my specific case, for each api I want only some of the classes to be generated.
I know you can create a custom generator, but most of code generated by the default jaxrs-jersey generator is good for me, so I rather keep using the default one.
Is there a way to accomplish it ?
Thanks.
Upvotes: 2
Views: 5831
Reputation: 149
According to documentation in https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#bringing-your-own-models
Sometimes you don't want a model generated. In this case, you can simply specify an import mapping to tell the codegen what not to create. When doing this, every location that references a specific model will refer back to your classes. Note, this may not apply to all languages...
To specify an import mapping, use the --import-mappings argument and specify the model-to-import logic as such:
--import-mappings Pet=my.models.MyPet
Or for multiple mappings:
--import-mappings Pet=my.models.MyPet,Order=my.models.MyOrder
or
--import-mappings Pet=my.models.MyPet --import-mappings Order=my.models.MyOrder
I applied the above like following in maven plugin:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.3.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<importMappings>Comment=com.example.project.Comment</importMappings>
<generatorName>java</generatorName>
<library>resttemplate</library>
</configuration>
</execution>
</executions>
</plugin>
So in my example, I have a class named "Comment" that is auto generated incorrectly by openapi generator. So I created the Comment class myself, into the above path "com.example.Comment" in importMappings field. And I passed it as a parameter into the importMappings field just like above. After above setup, Comment class is not generated by openapi-generator anymore and my Comment class is not overriden
Upvotes: 2