I am using openapi-generator-maven-plugin to generate model sources. With my current configuration it generates methods called fromJson and toJson for every model class.
Is there a way to configure the plugin, so it won't generate this Json methods?
I don't need them and they bring dependencies that I don't want in my project.
Here is my current config of the plugin
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.3.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>java</generatorName>
<language>Java</language>
<output>${project.build.directory}/generated-sources/swagger</output>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<!-- Add custom annotation for model sources to achieve builder pattern -->
<additionalModelTypeAnnotations>#lombok.experimental.SuperBuilder #lombok.AllArgsConstructor</additionalModelTypeAnnotations>
</configOptions>
<modelPackage>my.package</modelPackage>
<!-- Only generate the model since we need it for deserialization -->
<generateApis>false</generateApis>
<generateApiDocumentation>false</generateApiDocumentation>
<generateApiTests>false</generateApiTests>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModels>true</generateModels>
</configuration>
</execution>
</executions>
</plugin>
The fromJson and toJson methods are generated when the Java generator library is set to okhttp-gson or retrof. If not defined, the openapi-generator uses okhttp-gson by default.
You can view the different JSON processors used by examining the library config options. The majority of them use Jackson, but the default one uses gson. You can also set your serializationLibrary to jackson, but this will only effect some library generators; not all.
So, if you set your library to whichever is appropriate to your project, the unwanted toJson and fromJson methods will disappear, as will the unwanted dependencies.
Related
I am working on generation of model classes by Open API Maven plugin which I need to use in my business logic. The api is generated by other team and given to us. It has lot of model classes specification but I need only generation of 3 model classes from the api.yaml template
How this can be achieved?
The plugin configuration
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-java-client</id>
<configuration>
<inputSpec>
${project.build.directory}/../../../../../../../../../component/service/app/src/main/resources/api/v1/api-v1.yaml
</inputSpec>
**<modelPackage>com.example.editing.model</modelPackage>**
<generateSupportingFiles>false</generateSupportingFiles>
<generateApis>false</generateApis>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
</configuration>
</execution>
</executions>
</plugin>
According to the github readme of the openapi-generator-maven-plugin, there is a configuration option called modelsToGenerate which does exactly what you want:
A comma separated list of models to generate. All models is the default.
I'm a newcomer for graphql java client development. Recently I meet a problem that how to generate a java entity through the graphql schema definition. Since in my side, the schema is always changing by another side, so I want a method to generate the java entity class automatically instead of reading the schema manually then change the code. If there are any practices on such a requirement? Thanks.
You could look into the following maven plugin. It would help you generate Java classes from your .graphqls files.
Assuming you have only one file defining your GraphQL model, named myGraphqls, I would use this plugin as follow:
<build>
<plugins>
<plugin>
<groupId>io.github.kobylynskyi</groupId>
<artifactId>graphql-codegen-maven-plugin</artifactId>
<version>1.2.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<graphqlSchemaPaths>
<graphqlSchemaPath>${project.basedir}/src/main/resources/graphql/myGraphqls.graphqls</graphqlSchemaPath>
</graphqlSchemaPaths>
<outputDir>${project.build.directory}/generated-sources/graphql</outputDir>
<packageName>com.graphql.model</packageName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
After running maven, you would be able to find in the generated-sources folder, a folder named graphql with the classes generated under the package com.graphql.model.
We have created a Yaml file using Swagger editor for our APIs specification which includes Base URL, endpoint, Request, Response and Header information etc.. Now I want to implement RESTful web service for these APIs. For that I am thinking of generating my Request and Response Java classes from this Yaml file and was looking for some kind of code generator, preferably a maven plugin/dependency which I could use in my Maven project. I came across this rest client with swagger which talks about using the swagger-codegen Maven plugin, but this is to generate the client which I believe is about generating the client code to consume these RESTful APIs, however my need is to generate classes to be used for service implementation. I will be using Java and Spring framework.
My question is what are the best practices for implementing the RESTful web services in Java when we have Yaml file (API spec created using Swagger editor) and which code generation tools/plugins are available to be used.
EDIT: Just came across this Server stub generator HOWTO, looking further into it.
Swagger-codegen maven plugin is a good option but I suggest you to use jhipster to generate your java project. It generates projects with latest tech stack including spring framework. You can select API-First development in your case. I used it and it was very efficient. You already have Yaml file. Put it in src/main/resources/swagger/api.yml and run
./mvnw generate-sources
All java codes will be generated.
Using swagger-codegen-maven plugin like the following
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.29</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${basedir}/src/main/resources/swagger/project.yaml</inputSpec>
<language>java</language>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<library>resteasy</library>
</configOptions>
</configuration>
</execution>
</executions>
or using openapi-generator-maven-plugin
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.3.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/swagger/project.yaml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>where api package is to be rendered</apiPackage>
<modelPackage> model package </modelPackage>
<supportingFilesToGenerate>
Any supporting files needed </supportingFilesToGenerate>
<configOptions>
<delegatePattern>true</delegatePattern>
</configOptions>
</configuration>
</execution>
</executions>
Is there a way how to generate controller Spring MVC code from Swagger/OpenAPI specification?
I know Swagger can be generated from existing Spring code but is this possible the other way round ?
You are basically looking for generation of swagger server-side code. If you would like to generate it while you are building your application and if you are using maven you can use the following plugin:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${swagger.codegen.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${swagger.yaml.file}</inputSpec>
<language>spring</language>
<configOptions>
<sourceFolder>${swagger.generated.sourcepath}</sourceFolder>
<!-- <interfaceOnly>true</interfaceOnly> -->
<dateLibrary>java8</dateLibrary>
</configOptions>
<typeMappings>
<typeMapping>OffsetDateTime=Instant</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.Instant</importMapping>
</importMappings>
<modelPackage>${project.groupId}.${project.artifactId}.swagger.model</modelPackage>
<apiPackage>${project.groupId}.${project.artifactId}.swagger.api</apiPackage>
<invokerPackage>${project.groupId}.${project.artifactId}.swagger.invoker</invokerPackage>
</configuration>
</execution>
</executions>
</plugin>
Please note the commented part interfaceOnly if set to true, it will only create the APIs class with default as NOT_IMPLEMENTED and you would have to write the implementation.
Add following dependency:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.annotations.version}</version>
<scope>compile</scope>
</dependency>
I used the following properties:
<properties>
<swagger.codegen.version>2.4.1</swagger.codegen.version>
<swagger.yaml.file>${project.basedir}/swagger.yaml</swagger.yaml.file>
<swagger.annotations.version>1.5.21</swagger.annotations.version>
<swagger.generated.sourcepath>src/main/java</swagger.generated.sourcepath>
</properties>
The other static approach that would require the generation of controller manually when there is a change in the swagger file would be to use swagger editor.
Yes it is possible using swagger codegen from the command line or using swagger editor.
With jOOQ, I may want to combine using the jOOQ code generator with Maven and a custom generator strategy. It looks as though this can be done as such (leaving out irrelevant parts):
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>2.2.2</version>
<!-- The plugin should hook into the generate goal -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generator>
<name>org.jooq.util.DefaultGenerator</name>
<!-- But the custom strategy is not yet compiled -->
<strategy>
<name>com.example.MyStrategy</name>
</strategy>
</generator>
</configuration>
</plugin>
The above configuration depicts the problem. jOOQ's code generator hooks into the generate goal of the Maven lifecycle, which takes place before the compile goal of the lifecycle. For code generation, however, it needs a pre-compiled custom strategy class, or I will get a ClassNotFoundException. How can this be resolved with Maven? Can I compile a single class before executing the generate goal?
A much better solution is to split the project into two modules. One contains the strategy and the other the rest.
Using modules, you can compile the strategy in an independent step and then use that module in the plugin:
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>2.2.2</version>
...your config goes here...
<dependencies>
list your strategy module here
</dependencies>
</plugin>