I am trying to use Swagger Codegen to generate model classes for my spring boot project. I found some references online and included the following plugin in my pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/Contract-v1.yml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>${project.groupId}.swagger.api</apiPackage>
<modelPackage>${project.groupId}.swagger.model</modelPackage>
<supportingFilesToGenerate>
ApiUtil.java
</supportingFilesToGenerate>
<configOptions>
<sourceFolder>src/main/java/</sourceFolder>
<delegatePattern>true</delegatePattern>
<interfaceOnly>true</interfaceOnly>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
I run mvn install
and the classes are generated in the directory /target/generated-sources/openapi
. But I am unable to import these generated classes in my REST controller class.
My understanding is that the <modelPackage>
field is used to identify the package in which the generated classes have to be placed. Am I right about this?
Even though these generated classes are in the right package, since they are not in the src/main/java
, i am probably not able to import them in other classes.
Is there way to get these generated classes under the src/main/java
directory or am I missing something in the maven config due to which these files are unavailable to other classes ?
I also wanted to achieve what you were trying and although I fumbled initially I figured it out eventually, I did not have to do anything special. One caveat is that I used swagger-codegen-maven-plugin
instead of the openapi-generator-maven-plugin
.
Here is how my configuration look like on the day of latest version
swagger-codegen-maven-plugin
version 3.0.35
. When I ran mvn compile
all my classes were generated in the target folder and src main java was marked as sources folder. I was directly able to implement my own controller directly in the project by referencing the generated code API. Tbh the documentation of swagger-codegen-maven-plugin
is not the best , I had to look at several places to make sense. Adding some references
General Configuration parameters: https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin
Config Options: https://github.com/swagger-api/swagger-codegen/issues/7795
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.35</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/api-docs/swagger.yaml</inputSpec>
<generateApis>true</generateApis>
<generateModels>true</generateModels>
<generateSupportingFiles>false</generateSupportingFiles>
<language>spring</language>
<configOptions>
<basePackage>com.company.project</basePackage>
<apiPackage>com.company.project.api</apiPackage>
<modelPackage>com.company.project.model</modelPackage>
<interfaceOnly>true</interfaceOnly>
<sourceFolder>src/main/java</sourceFolder>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
<dateLibrary>java11</dateLibrary>
<ignoreUnknownJacksonAnnotation>true</ignoreUnknownJacksonAnnotation>
<notNullJacksonAnnotation>true</notNullJacksonAnnotation>
<dateLibrary>false</dateLibrary>
<useBeanValidation>true</useBeanValidation>
<serializableModel>true</serializableModel>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>