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

[REQ] Support SpringBoot 3.0 GA for Java-based API code generation #14143

Closed
kpupkov-ppa opened this issue Nov 29, 2022 · 41 comments
Closed

[REQ] Support SpringBoot 3.0 GA for Java-based API code generation #14143

kpupkov-ppa opened this issue Nov 29, 2022 · 41 comments

Comments

@kpupkov-ppa
Copy link

Is your feature request related to a problem? Please describe.

We need a new way to generate API with jakarta annotations in code

Describe the solution you'd like

all javax.XXXX is no longer supported and replaced by jakarta

Describe alternatives you've considered

no alternative

Additional context

https://spring.io/blog/2022/11/24/spring-boot-3-0-goes-ga

@maxl2287
Copy link

maxl2287 commented Dec 2, 2022

Same here...
Code-generation is not possible using Spring Boot 3.0.0 with the latest openapi-generator version...

@borsch
Copy link
Member

borsch commented Dec 7, 2022

@vigourouxc
Copy link

The same problem applies to the "java" code generator (client side). We're using java with webclient generator, and the templates use javax.annotation packages that are incompatible with a spring boot 3 application.

@pstorch
Copy link
Contributor

pstorch commented Dec 8, 2022

@borsch useSpringBoot3 only works for the model classes, but not for the generated restcontroller classes. They are still genereated with javax.annotation.* and javax.validation.* imports.

@mlemnian
Copy link

@ComputerDaddyGuy mentioned over in the swagger-codegen github repo the following workaround:

Assuming you are generating your objects inside /src/gen/java/your/package, you can add the following plugin to your pom.xml

<plugin>
	<groupId>com.google.code.maven-replacer-plugin</groupId>
	<artifactId>replacer</artifactId>
	<version>1.5.3</version>
	<executions>
		<execution>
			<phase>generate-sources</phase>
			<goals>
				<goal>replace</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<includes>
			<include>${project.basedir}/src/gen/java/**/*.java</include>
		</includes>
		<regex>false</regex>
		<token>javax.annotation</token>
		<value>jakarta.annotation</value>
	</configuration>
</plugin>

You will then see some new logs in your console, after the swagger-codegen-maven-plugin logs:

[INFO] --- replacer:1.5.3:replace (default) @ my-super-api ---
[INFO] Replacement run on 39 files.

source: swagger-api/swagger-codegen#11797 (comment)

The idea is to generate the code as usual but modify it automatically afterward. I'm not a huge fan of this approach but as a workaround, it's "ok".

@DennisHartrampf
Copy link
Contributor

We also need Spring Boot 3.0.0 support for Kotlin-Spring. Should this be a separate issue?
Also even useSpringBoot3 (which is not available in Kotlin-Spring) doesn't seem to be the end of the road. It is using Spring Boot 3.0.0-M3 instead of the release version 3.0.0.

@DennisHartrampf
Copy link
Contributor

Never mind. There is already an issue for Kotlin-Spring: #13578.

@aleksbal
Copy link

The solution for this problem would be the use of customised mustache templates in your project. Just copy original templates in a project folder and modify some or all of them by replacing "javax" with "jakarta"...

@mlemnian
Copy link

mlemnian commented Dec 16, 2022

@aleksbal IMHO this is not a solution but another workaround.
If there are any changes in the mustache templates in this project, you have to backport the changes into your own project as well.

This whole problem could have been handled years ago (since November 2020 the Jakarta EE 9 spec was released).
Since then openapi-generator but also the swagger-codegen could have implemented a java/jvm wide toggle/flag to use either a jakarta or javax packages.

Why java/jvm wide?
because of

  • other jvm languages like kotlin and
  • other APIs like json-b, json-p, jax-rs, bean-validation, ... that also have experienced the package change and
  • other generators for different frameworks like spring, helidon, camel, jaxrs-*.

E.g. the spring generator has a useSpringBoot3 that doesn't work in some cases.

  • in helidon-client and helidon-server we have a rootJavaEEPackage and helidonVersion property.
  • for java clients in general you can set the microprofileRestClientVersion property to 3.0 to implicitly set some root packages to jakarta.

To be honest, this is not a great experience.

@aleksbal
Copy link

@mlemnian

Well, I think it is legit to use customised mustache templates for code generation in order to solve some specific project needs. However I agree that javax/jakarta problem is not really "the specific one" but rather the one which affects the whole ecosystem.

Since then openapi-generator but also the swagger-codegen could have implemented a java/jvm wide toggle/flag to use either a jakarta or javax packages.

Actually they've bee trying, for example, if you have a look in the templates from OpenAPI generator's Java Spring module (v6.2.1) you will find a flag called "useJakartaEe", which determines whether to generate the Java classes using "javax." or "jakarta."...

@borsch
Copy link
Member

borsch commented Dec 16, 2022

@borsch useSpringBoot3 only works for the model classes, but not for the generated restcontroller classes. They are still genereated with javax.annotation.* and javax.validation.* imports.

@pstorch could you please provide a sample for this? I don't see any javax imports in SB3 generated project

@aleksbal
Copy link

I however was not able to utilise the "useJakartaEe", just have seen it in the templates but to set the flag to true or false didn't work for me, so I adjusted the original templates to fit our needs. I would appreciate any example which demonstrates the usability. Thx.

@pstorch
Copy link
Contributor

pstorch commented Dec 17, 2022

@borsch mea culpa. I verified it again and the one project which had this issue is using custom templates, which use the javax namespace. 🤦 Sorry, I didn't see that.

@borsch
Copy link
Member

borsch commented Dec 21, 2022

  1. Recently these two MRs were merged with upgrades on SP3 to 3.0.0 & update for SB 2.7.x.
  2. jakarta as for now is available only with SP3 which can be enabled with useSpringBoot3=true option

Any other issues to be covered here? Otherwise make sense to close this one

#14283
#14285

@alwibrm
Copy link

alwibrm commented Dec 27, 2022

Hi @borsch, when using generator java with library webclient the generated code still contains javax.* instead of jakarta.*, even with the config option <useSpringBoot3>true</useSpringBoot3>. This is a blocker for us to upgrade on Spring Boot 3.

@borsch
Copy link
Member

borsch commented Dec 27, 2022

@alwibrm webclient doesn't yet support neither SB3 nor jakarta. This change is in progress

@alwibrm
Copy link

alwibrm commented Dec 27, 2022

@borsch Thanks for the quick reply. Is there an open issue for that or a planned release version/date?

@borsch
Copy link
Member

borsch commented Dec 27, 2022

@alwibrm I'm aware only about rest-template update to spring 6 #14276

@alwibrm
Copy link

alwibrm commented Dec 29, 2022

@borsch You mentioned the change for webclient was in progress. Is there any issue or merge request for this? Otherwise I would consider contributing a merge request.

@parenko
Copy link
Contributor

parenko commented Jan 2, 2023

FYI: with #14343 it is now possible to enable jakarta namespace for non SB3 Java templates by using useJakartaEe=true.

@LSzelecsenyi
Copy link

Hi. I am trying to upgrade our project to spring-boot 3. During this I encountered the javax/jakarta issue. Upgraded the openapi-generator to 6.2.0 and made the setting <useSpringBoot3>true</useSpringBoot3> in the pom.xml but still having problem with javax.ws.rs.core. I am having error "package javax.ws.rs.core does not exist". Any idea how to fix it? Thanks in advance!

@mlemnian
Copy link

mlemnian commented Feb 3, 2023

Hi. I am trying to upgrade our project to spring-boot 3. During this I encountered the javax/jakarta issue. Upgraded the openapi-generator to 6.2.0 and made the setting <useSpringBoot3>true</useSpringBoot3> in the pom.xml but still having problem with javax.ws.rs.core. I am having error "package javax.ws.rs.core does not exist". Any idea how to fix it? Thanks in advance!

@LSzelecsenyi
Suppose your application code references the old javax.* classes, you have to rename every occurrence of the name javax to jakarta inside of your code. (and update your maven/gradle dependencies)

If this reference is inside the generated code, this is odd.
Spring usually doesn't use a lot of JavaEE/JakartaEE classes/APIs, maybe you don't use the spring (<generatorName>spring</generatorName>) generator at all?

@parenko
Copy link
Contributor

parenko commented Feb 4, 2023

@kpupkov-ppa , could you please try the latest 6.3.0 release if your requiment is met?

@LSzelecsenyi
Copy link

LSzelecsenyi commented Feb 4, 2023

@LSzelecsenyi Suppose your application code references the old javax.* classes, you have to rename every occurrence of the name javax to jakarta inside of your code. (and update your maven/gradle dependencies)

If this reference is inside the generated code, this is odd. Spring usually doesn't use a lot of JavaEE/JakartaEE classes/APIs, maybe you don't use the spring (<generatorName>spring</generatorName>) generator at all?

@mlemnian Thanks for the reply.
In my code I changed every import to jakarta, the problem is with the generated stuff only.
I am using spring in the generator's config.
For a temporary fix I am using maven-replacer-plugin to change javax from the generated code

@parenko
Copy link
Contributor

parenko commented Feb 4, 2023

@LSzelecsenyi Suppose your application code references the old javax.* classes, you have to rename every occurrence of the name javax to jakarta inside of your code. (and update your maven/gradle dependencies)
If this reference is inside the generated code, this is odd. Spring usually doesn't use a lot of JavaEE/JakartaEE classes/APIs, maybe you don't use the spring (<generatorName>spring</generatorName>) generator at all?

@mlemnian Thanks for the reply. In my code I changed every import to jakarta, the problem is with the generated stuff only. I am using spring in the generator's config. For a temporary fix I am using maven-replacer-plugin to change javax from the generated code

@LSzelecsenyi please try 6.3.0 with configOption <useJakartaEe>true</useJakartaEe>.

@LSzelecsenyi
Copy link

LSzelecsenyi commented Feb 7, 2023

@LSzelecsenyi please try 6.3.0 with configOption <useJakartaEe>true</useJakartaEe>.

@parenko
I am trying with <useSpringBoot3>true</useSpringBoot3> doc says:

useSpringBoot3: Generate code and provide dependencies for use with Spring Boot 3.x. (Use jakarta instead of javax in imports). Enabling this option will also enable useJakartaEe.

Tried with 6.3.0 but without the replacer I am getting the same error: package javax.ws.rs.core does not exist

@pstorch
Copy link
Contributor

pstorch commented Feb 7, 2023

For me it worked with 6.2.1 and 6.3.0 with configOption <useSpringBoot3>true</useSpringBoot3>
@LSzelecsenyi can you show your maven/gradle section? Do you use custom templates?

@LSzelecsenyi
Copy link

LSzelecsenyi commented Feb 7, 2023

@pstorch

<plugin>
	<groupId>org.openapitools</groupId>
	<artifactId>openapi-generator-maven-plugin</artifactId>
	<version>6.3.0</version>
	<executions>
		<execution>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<inputSpec>
					${basedir}/src/main/resources/openapi/api.yaml
				</inputSpec>
				<generatorName>spring</generatorName>
				<apiPackage>${project.groupId}.foo.client</apiPackage>
				<modelPackage>${project.groupId}.foo.model</modelPackage>
				<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
				<configOptions>
					<openApiNullable>false</openApiNullable>
					<useTags>true</useTags>
					<useSpringBoot3>true</useSpringBoot3>
					<sourceFolder>src/main/java/</sourceFolder>
					<delegatePattern>true</delegatePattern>
					<interfaceOnly>true</interfaceOnly>
				</configOptions>
			</configuration>
		</execution>
	</executions>
</plugin>

@alwibrm
Copy link

alwibrm commented Feb 7, 2023

The config option <useJakartaEe>true</useJakartaEe> in version 6.3.0 for generator java and library webclient now leads to the correct jakarta.* namespaces.

Unfortunately the build is still broken because of a cannot find symbol exception in Schema.RequiredMode.

This is because there are two different versions of swagger-annotations on the classpath, 1.6.8 and 2.2.4:

[INFO] +- org.openapitools:openapi-generator:jar:6.3.0:compile
[INFO] |  +- io.swagger.parser.v3:swagger-parser:jar:2.1.6:compile
[INFO] |  |  +- io.swagger.parser.v3:swagger-parser-v2-converter:jar:2.1.6:compile
[INFO] |  |  |  +- io.swagger:swagger-core:jar:1.6.8:compile
[INFO] |  |  |  |  +- io.swagger:swagger-models:jar:1.6.8:compile
[INFO] |  |  |  |  |  \- io.swagger:swagger-annotations:jar:1.6.8:compile
[INFO] |  |  \- io.swagger.parser.v3:swagger-parser-v3:jar:2.1.6:compile
[INFO] |  |     +- io.swagger.core.v3:swagger-core:jar:2.2.4:compile
[INFO] |  |     |  +- io.swagger.core.v3:swagger-annotations:jar:2.2.4:compile

@pstorch
Copy link
Contributor

pstorch commented Feb 7, 2023

@LSzelecsenyi ok, I don't see any obvious mistakes.

@parenko
Copy link
Contributor

parenko commented Feb 13, 2023

@LSzelecsenyi could you also share your api.yaml which causes "package javax.ws.rs.core does not exist"?
Currently I cant reproduce the error only with provided plugin configuration.

@laidani
Copy link

laidani commented Mar 18, 2023

For feature readers, I solved the issue by adding these dependencies to my project plus <useSpringBoot3>true</useSpringBoot3>:

<dependency>
    <groupId>org.openapitools</groupId>
    <artifactId>jackson-databind-nullable</artifactId>
    <version>0.2.6</version>
</dependency>
<dependency>
    <groupId>jakarta.validation</groupId>
    <artifactId>jakarta.validation-api</artifactId>
    <version>3.0.2</version>
</dependency>
<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>2.2.8</version>
</dependency>

@aquacode
Copy link

I'm using the https://github.com/int128/gradle-swagger-generator-plugin plugin along with openapi-generator-cli version 6.6.0 and I am also noticing this for:
generator: spring
library: spring-boot

How are people working around this issue? I seem to have the correct annotations bytecode since my version of Schema on classpath doesn't have the requiredCode property or RequiredMode.

It seems to be a problem with the model code generation using the outdated requiredMode property. Is that a problem in the mustache template? Is there any workaround?

@max-kry
Copy link

max-kry commented May 24, 2023

The workaround ended up being:

  • check out openapi-generator
  • copy gradle.build.mustache of your choice (for example from openapi-generator/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/) into a folder, say templates
  • in the template replace
    • jakarta_annotation_version = "1.3.5"
      with
      jakarta_annotation_version = "2.1.1"
    • implementation 'javax.ws.rs:javax.ws.rs-api:2.1.1'
      with
      implementation 'jakarta.ws.rs:jakarta.ws.rs-api:3.1.0'
  • point the generator at the patched template using templateDir property
task generate(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    generatorName = "java"
    ..
    templateDir = "$projectDir/templates".toString()
    ..
    configOptions = [
            ..
            useJakartaEe            : "true",
            ..

@ario-afrashteh
Copy link

Any update guys?

@frankjkelly
Copy link

Thanks to @laidani I was able to get passed the
cannot find symbol
on requiredMode = Schema.RequiredMode.REQUIRED by forcibly updating the following

  implementation('io.swagger.core.v3:swagger-annotations:+') {
    because("we get error that we cannot find a symobl on Schema.RequiredMode. See https://github.com/OpenAPITools/openapi-generator/issues/14143 ")
  }

@Philzen
Copy link
Contributor

Philzen commented Jun 2, 2024

@kpupkov-ppa correct me if i'm wrong, but as far as i understand code generation for java with spring boot 3 is working fine nowadays … if you agree, kindly close this issue.

@tukez
Copy link

tukez commented Jul 17, 2024

There is still problem with the generated controllers which use javax annotation imports.

@pstorch
Copy link
Contributor

pstorch commented Jul 17, 2024

@tukez as far es I can see the imports are all fine if you use the useSpringBoot3 option.

@tukez
Copy link

tukez commented Jul 17, 2024

@pstorch Thank you, that is correct. It was some Eclipse problem which was corrected once I manually deleted the old generated file (Eclipse clean was not enough for some reason).

@Philzen
Copy link
Contributor

Philzen commented Jul 17, 2024

@tukez @kpupkov-ppa as confirmed now, useSpringBoot3 (or only useJakartaEe for that matter) flags do the job.

Closing this issue for now. Should anyone come across a specific java generator that doesn't respect these flags correctly, kindly open a new issue so they can be targeted and fixed in more atomic PRs.

@Philzen Philzen closed this as completed Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.