OpenAPITools / OpenAPITools/openapi-generator

Make it possible to disable generation of swagger annotations

Open
#8,719 10 comments 27 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

We are using openapi-generator-maven-plugin to generate Java classes from swagger files. We are not using swagger but Spring rest template + jackson for triggering REST services.

Code generation works great but unfortunately openapi-generator-maven-plugin always includes swagger annotations in the code. In order for being able to compile the code we have to use the com.google.code.maven-replacer-plugin:replacer maven plugin to get rid of the annoations - otherwise the code does not compile since we don't have any swagger dependencies included (and also can't add them for technical reasons).

Would be great if there would be an option to disable the generation of the annotations since we don't need them.

This is the plugin config we are using

<plugin>
	<groupId>org.openapitools</groupId>
	<artifactId>openapi-generator-maven-plugin</artifactId>
	<version>5.0.1</version>
	<executions>
		<execution>
			<id>security</id>
			<phase>generate-sources</phase>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<inputSpec>${project.basedir}/src/main/yaml/xxx.json</inputSpec>
				<output>${project.build.directory}/generated-sources/openapi/xxx/src</output>
				<modelPackage>xxx</modelPackage>
				<generatorName>java</generatorName>
				<library>resttemplate</library>
				<addCompileSourceRoot>true</addCompileSourceRoot>
				<generateApis>false</generateApis>
				<generateModelDocumentation>false</generateModelDocumentation>
				<generateModelTests>false</generateModelTests>
				<generateSupportingFiles>false</generateSupportingFiles>
				<configOptions>
					<serializableModel>true</serializableModel>
					<dateLibrary>legacy</dateLibrary>
				</configOptions>
			</configuration>
		</execution>
	</executions>
</plugin>	

This is the workaround we are using

<plugin>
	<groupId>com.google.code.maven-replacer-plugin</groupId>
	<artifactId>replacer</artifactId>
	<executions>
		<execution>
			<id>removeUnusedAnnotationImports</id>
			<phase>process-resources</phase>
			<goals>
				<goal>replace</goal>
			</goals>
			<configuration>
				<basedir>${project.basedir}</basedir>
				<includes>
					<include>target/generated-sources/openapi/**/*.java</include>
				</includes>
				<regex>false</regex>
				<replacements>
					<replacement>
						<token>import io.swagger.annotations.ApiModel;</token>
						<value />
					</replacement>
					<replacement>
						<token>import io.swagger.annotations.ApiModelProperty;</token>
						<value />
					</replacement>
				</replacements>
			</configuration>
		</execution>
		<execution>
			<id>removeUnusedAnnotations</id>
			<phase>process-resources</phase>
			<goals>
				<goal>replace</goal>
			</goals>
			<configuration>
				<basedir>${project.basedir}</basedir>
				<includes>
					<include>target/generated-sources/openapi/**/*.java</include>
				</includes>
				<replacements>
					<replacement>
						<token>@ApiModel.*\)</token>
						<value />
					</replacement>
				</replacements>
			</configuration>
		</execution>
	</executions>
</plugin>				

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the openapi-generator-maven-plugin configuration and the Java generator using the resttemplate library, then inspect the generated model output for Swagger imports and annotations. Done means a configuration option disables those annotations and the generated Java classes compile without Swagger dependencies.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.