Openapi-generator: [BUG] bad import for SpringBoot model

Created on 25 Jul 2019  路  10Comments  路  Source: OpenAPITools/openapi-generator

The commit https://github.com/OpenAPITools/openapi-generator/commit/0fca90133ce097f987098e6121936c0ffcb210a8 adds
`import org.openapitools.jackson.nullable.JsonNullable; ```

Why I need to import the class if I don't need it?

Bug Spring

Most helpful comment

I have started to add google-java-format as post-generation step when I use OpenAPI-Generator.

It removes the unused imports and format the code.
There are gradle and maven plugins available.

All 10 comments

cc @cbornet

I also saw this. It is unused import and it forces to add dependency.

I think this issue is far bigger. In a lot of generators there are a heck of a ton unused imports in every file. My IDE really hates me for that since there are generated files which are not annotated as generated in the comments. I think the best option would be to make each single import conditional which would bloat the whole generator in its current state.

Each import would be associated with one or multiple features. If one or more of those features are used the import should be set as well as other conditional events like adding the dependency to the dependency management system.
That feature could be implemented as a base feature for all languages and after that could be adopted to each individual generator.

The problem is even bigger, if you want to use the generator in build tool, after generating it does not compil witout removing that unused import, so you cannot use it in an automated build!

I have started to add google-java-format as post-generation step when I use OpenAPI-Generator.

It removes the unused imports and format the code.
There are gradle and maven plugins available.

@jmini That is a good solution to start with. But that only fixes the issue for Java. Since the generator is not java specific I tend to not like that as a final solution.

@jmini sound like a big hack. This kind of solution tends to hide problems. Why to not fix the bug as it perfectly visible and reproducible.

@bademux: feel free to propose a PR.

The problem is that the way the generator is built, the imports are written first and at this point in time the information to know if one of the field will use JsonNullable in the class content or not is not present.

@jmini Well, it a bit of a work to propose new generator :)
Anyway, thanks for discussion!

Closed as a "Won't fix" (until global generator context with all needed information will be passed)

According to https://github.com/OpenAPITools/openapi-generator/pull/3474#issuecomment-520393508, I think that @bkabrda added the code you wanted:

It also makes sure that the imports for JsonNullable are only added when at least one of the model's fields actually uses that.

Can you test with the latest SNAPSHOT?

Was this page helpful?
0 / 5 - 0 ratings