Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unused import org.openapitools.jackson.nullable.JsonNullable from generated model #2901

Closed
pellyadolfo opened this issue May 15, 2019 · 28 comments · Fixed by #6154
Closed

Comments

@pellyadolfo
Copy link

pellyadolfo commented May 15, 2019

Bug Report Checklist

  • [ X] Have you provided a full/minimal spec to reproduce the issue?
  • [ X] Have you validated the input using an OpenAPI validator (example)?
  • [ X] What's the version of OpenAPI Generator used?
  • [ X] Have you search for related issues/PRs?
  • [X ] What's the actual output vs expected output?
  • [X ] [Optional] Bounty to sponsor the fix (example)
Description

When I generate the model there is an unused import org.openapitools.jackson.nullable.JsonNullable; in every single DTO. It forces me to add a dependency.

	<dependency>
	    <groupId>org.openapitools</groupId>
	    <artifactId>jackson-databind-nullable</artifactId>
	    <version>0.1.0</version>
	</dependency>

Unfortunately this dependency must be approved in the company so the model is unusable.

Is there any way to get rid of this unused import? Configuration is below:

		<plugin>
		    <groupId>org.openapitools</groupId>
		    <artifactId>openapi-generator-maven-plugin</artifactId>
		    <version>4.0.0</version>
		    <executions>
		        <execution>
		            <goals>
    			        <goal>generate</goal>
    			    </goals>
        			<configuration>
				<inputSpec>${maven.multiModuleProjectDirectory}/api/target/generated/swagger-api-spec/swagger.json</inputSpec>	
					<language>spring</language>
					<library>spring-boot</library>
					<model-name-prefix>Cqrs</model-name-prefix>
					<modelPackage>com.anthem.specialty.provider.springapi.model</modelPackage>
					<apiPackage>com.anthem.specialty.provider.springapi.api</apiPackage>
					<invokerPackage>com.anthem.specialty.provider.springapi.invoker</invokerPackage>
        			    <skipValidateSpec>true</skipValidateSpec>
					<generateSupportingFiles>true</generateSupportingFiles>
					<supportingFilesToGenerate>
						ApiApi.java,ApiApiControler.java,ApiUtil.java
					</supportingFilesToGenerate>
        			    <configOptions>
					<sourceFolder>src/gen/java/main</sourceFolder>
					<java8>false</java8>
					<dateLibrary>java8</dateLibrary>
					<interfaceOnly>false</interfaceOnly>
					<groupId>com.company</groupId>
					<artifactId>${project.artifactId}</artifactId>
					<artifactVersion>${project.version}</artifactVersion>
        		    		</configOptions>
        			</configuration>
	        	</execution>
		    </executions>
		</plugin>	
openapi-generator version
		    <groupId>org.openapitools</groupId>
		    <artifactId>openapi-generator-maven-plugin</artifactId>
		    <version>4.0.0</version>
OpenAPI declaration file content or url

probably convenient enhancement

Command line used for generation

maven plugin

Steps to reproduce

run the maven clean install

Related issues/PRs

a few similar issues

Suggest a fix

provide a mechanism to remove unused imports

@pellyadolfo pellyadolfo changed the title remove unused import org.openapitools.jackson.nullable.JsonNullable remove unused import org.openapitools.jackson.nullable.JsonNullable from generated model May 15, 2019
@swaroopshere
Copy link

This issue is affecting us as well. Is there an ETA for fixing this? Alternatively, is there a workaround by which we dont need to include the dependency?

@wing328
Copy link
Member

wing328 commented Aug 8, 2019

What about using customized template with the -t option for the time being?

@swaroopshere
Copy link

Thank you for your response @wing328 . Could you please elaborate on what you meant? Do you mean i could create a custom template that does not include the dependency and use the template while building? (I use open-api generator maven plugin)

@Rugal
Copy link
Contributor

Rugal commented Aug 8, 2019

This affect my project as well.

@bilak
Copy link
Contributor

bilak commented Sep 5, 2019

@wing328 can you please tell me which template is it? I create PR with fix.

@bilak
Copy link
Contributor

bilak commented Sep 6, 2019

as workaround while new version is published create directory ${maven.multiModuleProjectDirectory}/openapi/templates/JavaSpring add <templateDirectory>${maven.multiModuleProjectDirectory}/openapi/templates/JavaSpring</templateDirectory> to openapi-generator-maven-plugin config and include there this file

@macjohnny
Copy link
Member

you could also post-process the generated files with a script and remove the unused import by search/replace.

@LBoraz
Copy link

LBoraz commented Oct 29, 2019

bug still there in generator 4.1.3

@jmini
Copy link
Member

jmini commented Oct 30, 2019

you could also post-process the generated files

I agree with @macjohnny, post processing the generated file might be a temporary valid solution, but you do not need to do it by hand.

In #3461 (comment) I have indicated the google-java-format project that can do this (there are gradle and maven plugins).

In my main project I rely on Eclipse IDE tasks (format code and organize imports) to do it (but there OpenAPI-Generator is used inside a custom plugin)


About the root issue:

I see you are using the spring generator. There were similar discussions with the java generator (that generates java clients). According to @bkabrda in #3474 (comment), the imports are only added when needed.

A possible way to solve this issue without post-processing step is to understand what is different between the two generators (templates or logic in the code-gen layer) and to align them.

@bkabrda
Copy link
Contributor

bkabrda commented Oct 30, 2019

Yeah, the imports are only added when necessary, so it should also be possible to only put this dependency in pom.xml iff it's imported at least once somewhere. The patch to do that shouldn't be too hard, but unfortunately I don't have any spare time ATM - if someone wants to work on that, I'd be happy to provide guidance though.

@macjohnny
Copy link
Member

@nikodemin this issue will not magically disappear as long as there is no one that wants to contribute a fix.

@Patouche
Copy link
Contributor

@macjohnny : Yes, you're right. But what should we do ?

Today, it's the second times I'm in the same situation as @pellyadolfo describe in his first comment :

Unfortunately this dependency must be approved in the company so the model is unusable.

Maybe, its should be a good idea to add property like enableJacksonNullable to remove this dependency ? It will prevent having a dependency in the pom.xml which is not required (or approved). IMHO, I think it will be the best option ... and maybe the easiest.

I can try to work on this if you want.

@macjohnny
Copy link
Member

@Patouche yes, a configurable option would be nice. By the way: the dependency / code could be removed in the generated code by writing a small post-processing script, so you have a workaround.

@wing328
Copy link
Member

wing328 commented Apr 21, 2020

@Patouche have you tried https://github.com/google/google-java-format suggested by @jmini ?

Of course, we can add another option but we also got feedback from users that we've too many options in some generators (e.g. Java)

@AceHack
Copy link

AceHack commented May 2, 2020

This is a problem for the vertx one too, please remove it.

@Patouche
Copy link
Contributor

Patouche commented May 4, 2020

@wing328 : Yes, I took a look at https://github.com/google/google-java-format but I didn't try it because it wasn't really a solution for this bug / feature.

This library is design to format java code. IMHO, the first step here is to remove the dependency to org.openapitools:jackson-databind-nullable of classpath (pom.xml or build.gradle). Furthermore, we will have some constraints when writing the openapi.yaml file to prevent models to use this library which may be annoying.

@sdoeringNew
Copy link
Contributor

@Patouche Is this done?
This whole JsonNullable concept bothers me as well - not just the import and creates, imo, more problem than it solves.

I would be happy if enableJacksonNullable is soon implemented so we can use it. 👍

@Patouche
Copy link
Contributor

@sdoeringNew : yes, it was. Now there is some conflict on the PR. I already propose to resolve them.

But I have no news for a code reviews. Maybe I did something wrong. I don't know...

@wing328
Copy link
Member

wing328 commented Aug 13, 2020

@its-atique1987
Copy link

its-atique1987 commented Aug 25, 2020

-Adolfo Rodriguez
Why to add maven dependency? we can select the package (Eclipse IDE) do press Ctrl+Shift+O which removes unused imports and done.

Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Add a option for all java client and server to prevent
usage of third party library (jackson-databind-nullable)
which may be forbidden in some company

Add samples for Vertx, Spring MVC, Spring Cloud, Feign and Play

Upgrade dependencies for org.openapitools:jackson-databind-nullable
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
Patouche added a commit to Patouche/openapi-generator that referenced this issue Aug 26, 2020
wing328 pushed a commit that referenced this issue Aug 31, 2020
…on Nullable library (#6154)

* Add option to prevent usage of jackson-nullable (#2901)

Add a option for all java client and server to prevent
usage of third party library (jackson-databind-nullable)
which may be forbidden in some company

Add samples for Vertx, Spring MVC, Spring Cloud, Feign and Play

Upgrade dependencies for org.openapitools:jackson-databind-nullable

* Samples - Remove dependency org.openapitools:jackson-databind-nullable (#2901)

* Fix generation of gradle file for vertx (#2901)

* Regenerate samples (#2901)

* Fix documentation and up to date (#2901)

* Fix forgotten regeneration of vertx after dependency integration (#2901)

* Regenerate template after rebase (#2901)

* Use yaml config files introduce in #6509 to manage samples (#2901)

* Regenerate template using the config (#2901)

* Fix bad version during testing generated samples (#2901)

* Regenerate template after fix bad version (#2901)

* Fix merge, allow for set importing on codegen model

Co-authored-by: Jim Schubert <[email protected]>
@Mrak2017
Copy link

Mrak2017 commented Dec 23, 2020

Name of the new property is "openApiNullable"

@eye-dee
Copy link

eye-dee commented Jan 18, 2021

With openApiNullable property set to false I still have unused import

"openApiNullable": false

version: 4.3.1

@macjohnny
Copy link
Member

@SausageRoll #6154 is included in 5.0.0

@eye-dee
Copy link

eye-dee commented Jan 18, 2021

I see
Thank you!

@sdoeringNew
Copy link
Contributor

Thanks for this fix. That was the reason we could not update to v4. And now we can jump straight to v5.

@ledinscak
Copy link

I get same error for versions 5.2.x and 5.3.x, in 5.1.x it works fine without added dependency.

@omarsmak
Copy link

I get same error for versions 5.2.x and 5.3.x, in 5.1.x it works fine without added dependency.

I think you need to set <openApiNullable>false</openApiNullable>

@tadeuskozlovski
Copy link

tadeuskozlovski commented Aug 3, 2022

I get same error for versions 5.2.x and 5.3.x, in 5.1.x it works fine without added dependency.

I think you need to set <openApiNullable>false</openApiNullable>
Thanks,
by default openApiNullable value is TRUE(https://openapi-generator.tech/docs/generators/spring).
false worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment