OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.58k stars 6.52k forks source link

[BUG][JAVA] maven-compile-plugin fails with missing symbol errors after openapi-generator-maven-plugin generated the sources #8298

Open TheKvist opened 3 years ago

TheKvist commented 3 years ago
Description

When copying the plugin-Block from the Usage-Example of the openapi-generator-maven-plugin's README into a completely empty POM and executing mvn clean compile as instructed, the execution fails when the maven-compiler-plugin tries to compile the newly generated sources and, of course, can't find the packages referenced in those sources, such as gson, okio, etc.:

...
[ERROR] [...]\src\gen\java\main\org\openapitools\client\ApiClient.java:[23,22] error: package org.threeten.bp does not exist
[ERROR] [...]\src\gen\java\main\org\openapitools\client\ApiClient.java:[24,22] error: package org.threeten.bp does not exist
[ERROR] [...]\src\gen\java\main\org\openapitools\client\ApiClient.java:[25,29] error: package org.threeten.bp.format does not exist
[ERROR] [...]\src\gen\java\main\org\openapitools\client\ApiClient.java:[74,12] error: cannot find symbol
[ERROR] [...]\src\gen\java\main\org\openapitools\client\ApiClient.java:[77,12] error: cannot find symbol
...

So the plugin basically does what it's supposed to do, but the Maven compiler afterwards seems to be thrown off by the newly introduced code. The Plugin's README unfortunately does not seem to be addressing this issue.

Am I supposed to manually add those dependencies to my root POM and retry compiling until it works or am I missing some crucial configuration or knowledge about Maven's lifecycle?

Since the plugin generates a full project including POM which has those dependencies defined, I expected them to be transitively installed during compilation, but this does not seem to be the case. Even if they're installed in my local repository, I get the same error.

openapi-generator version

Tried with version 5.0.0 and any version above and including 4.0.0

Maven Version: 3.0.5 Java Version: 1.8.0_202

OpenAPI declaration file content or url

To test, I've used the official v3.0 Petstore example right from the master-Branch here on GitHub. I expect this spec to be valid and it seems to be, as the actual generation is not the problem.

Generation Details

A miminum sample repository can be found here.

The only step executed here is mvn clean compile.

Steps to reproduce
  1. Clone the sample repository / set up a minimum configuration for the Maven plugin against any valid spec to generate Java
  2. Execute mvn clean compile
Related issues/PRs

I've looked for both, open and closed issues containing the terms "maven", "plugin", and "symbol", but could not find any issues that seem to describe my issue. So I think what I'm experiencing is simply a configuration error, but I can't explain what I've missed since I didn't actually configure anything.

Suggest a fix

Unfortunately, I have only very little experience with Maven plugins and code generation with them in particular, so I don't know what to look for, here.

borsch commented 3 years ago

If you use plugin then you have to care about all dependencies on your own. On the other hand plugin has generated all needed dependecies for you. This what I get when I run mvn clean install for yours sample. That generated pom.xml has all necesary dependencies

зображення

TheKvist commented 3 years ago

I see, thanks for that information. I falsely made the assumption that Maven would be installing the dependencies from the generated POM so the first mvn clean install does not fail with the missing symbols errors.

So if I got it right the procedure is to actually have the first compile fail, then copy the generated dependencies in my root POM, then execute the command again? Or do I need so separately install the generated code?

borsch commented 3 years ago

yeah, you can do that. mvn clean compile, then check newly generated pom.xml add copy all required dependencies to yours pom.xml

MarcellHarmaci commented 4 weeks ago

Is there a way to configure maven to automatically unify the project pom.xmlwith the generated one? Something like @TheKvist expected