openapi-generator: [BUG] `java` and `spring` client generators: old `javax` classes are used, not compatible with Spring Boot 3 that migrated to `jakarta.*` classes
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What’s the actual output vs expected output?
Description
Modern Spring Boot 3 switched from javax.* annotations to jakarta.*.
It leads to a problem — code generated with the OpenAPI Generator does not compile anymore.
Examples:
import javax.annotation.Generated; // incorrect annotation, it must be jakarta.annotation.Generated
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
@Validated
@Tag(name = "my-service", description = "Endpoints for My Service health-check")
public interface MyServiceApi {
import javax.servlet.http.HttpServletResponse; // it must be jakarta.servlet.http.HttpServletResponse
import java.io.IOException; // it must be jakarta.validation.constraints.*
public class ApiUtil {
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
import javax.validation.Valid; // it must be jakarta.validation.Valid
import javax.validation.constraints.*;
default ResponseEntity<Void> addEdamamInput(
@Parameter(name = "EdamamData", description = "", required = true) @Valid @RequestBody List<EdamamData> edamamData
) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
openapi-generator version
The last released version is used:
<openapi-generator-maven-plugin.version>6.2.1</openapi-generator-maven-plugin.version>
Generation Details
Maven config looks like this, tried both spring and java as generatorName.
<execution>
<id>ras-client</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/../etc/api/ras.yaml</inputSpec>
<generatorName>java</generatorName>
<library>resttemplate</library>
<configOptions>
<sourceFolder>src/main/java/main</sourceFolder>
<dateLibrary>java8</dateLibrary>
<openApiNullable>false</openApiNullable>
<packageName>my.generated.ras</packageName>
<apiPackage>my.generated.ras.api</apiPackage>
<modelPackage>my.generated.ras.model</modelPackage>
<hideGenerationTimestamp>true</hideGenerationTimestamp> <!-- does not change the package of javax.annotation.Generated -->
</configOptions>
</configuration>
</execution>
Steps to reproduce
Related issues/PRs
Yes, there are related issues, especially about javax.annotation.Generated. For example:
- https://github.com/OpenAPITools/openapi-generator/issues/3636
- https://github.com/OpenAPITools/openapi-generator/issues/13274
They are not fixed, specifying <hideGenerationTimestamp>true</hideGenerationTimestamp> just does not fill the timestamp, but the import is still import javax.annotation.Generated;.
Suggest a fix
We need a new generator that works for the new Spring version, which is planned to be released around 24.Nov.2022. It should use only the jakarta.* annotations instead of javax.* annotations.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 12
- Comments: 19 (6 by maintainers)
Commits related to this issue
- fix: switch to jakarta annotations https://github.com/OpenAPITools/openapi-generator/issues/14010 — committed to noid-dev/codewars-java-sdk by ParanoidUser a year ago
It is possible to have
jakarta.*imports with the followingconfigOpts@jwilmoth-nc In my case only jakarta annotations where produced which is desired:
The client generator works for me with 6.3.0 and above (with
useJakartaEe=true). Latest version is 6.6.0. Did you try that?UPDATE: previously referred to snapshots - now not necessary.
For my locally built
6.3.0-SNAPSHOTthe annotations are working (added<useJakartaEe>true</useJakartaEe>to each of the configs, both server, and client).Will try to use the external maven repo now, thanks for the hint.
My server config example:
My client config example:
UPDATE: Yes, it’s working with the SonaType plugin repo:
At first, the question was about
javagenerator (client), NOT ONLY thespringgenerator (server).Well, for the server, the
jakarta.*annotations are generated withspringgenerator withspring-bootlibrary, yes.However, there are no
authclasses generated for the server generator.So the question is — I need a client generator with
jakarta.*annotations.What for do I need this?
The problem arises for the clients of the external APIs.
To set the API key
For
javagenerator withrest-templatelibrary, I set it like this (ApiClientandApiKeyAuthare the generated classes):To set the bearer token into the
AuthorizationheaderrasApiandapiClientare the generated classes