Openapi-generator: remove unused import org.openapitools.jackson.nullable.JsonNullable from generated model

Created on 15 May 2019  路  21Comments  路  Source: OpenAPITools/openapi-generator

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

Bug

Most helpful comment

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

All 21 comments

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?

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

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)

This affect my project as well.

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

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

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

bug still there in generator 4.1.3

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 https://github.com/OpenAPITools/openapi-generator/issues/3461#issuecomment-518152486 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 https://github.com/OpenAPITools/openapi-generator/pull/3474#issuecomment-520393508, 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.

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.

still have this bug on version 4.2.2 and 4.2.3

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

@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.

@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.

@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)

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

@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.

@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. :+1:

@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...

-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.

Was this page helpful?
0 / 5 - 0 ratings