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

[Java/Spring] Support Spring Boot 3 and Jakarta EE 9 #12407

Merged
merged 19 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bin/configs/spring-boot-3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
generatorName: spring
outputDir: samples/openapi3/server/petstore/springboot-3
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
groupId: org.openapitools.openapi3
documentationProvider: springdoc
cachescrubber marked this conversation as resolved.
Show resolved Hide resolved
artifactId: springboot
snapshotVersion: "true"
useSpringBoot3: true
useBeanValidation: true
hideGenerationTimestamp: "true"
13 changes: 13 additions & 0 deletions bin/configs/spring-cloud-3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generatorName: spring
library: spring-cloud
outputDir: samples/openapi3/client/petstore/spring-cloud-3
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
groupId: org.openapitools.openapi3
documentationProvider: springdoc
artifactId: spring-cloud-oas3
useSpringBoot3: "true"
interfaceOnly: "true"
singleContentTypes: "true"
hideGenerationTimestamp: "true"
1 change: 1 addition & 0 deletions docs/generators/java-camel.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|useBeanValidation|Use BeanValidation API annotations| |true|
|useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|
|useOptional|Use Optional container for optional parameters| |false|
|useSpringBoot3|Generate code and provide dependencies for use with Spring Boot 3.x. (Use jakarta instead of javax in imports).| |true|
|useSpringController|Annotate the generated API as a Spring Controller| |false|
|useSwaggerUI|Open the OpenApi specification in swagger-ui. Will also import and configure needed dependencies| |true|
|useTags|use tags for creating interface and controller classnames| |false|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|useBeanValidation|Use BeanValidation API annotations| |true|
|useFeignClientUrl|Whether to generate Feign client with url parameter.| |true|
|useOptional|Use Optional container for optional parameters| |false|
|useSpringBoot3|Generate code and provide dependencies for use with Spring Boot 3.x. (Use jakarta instead of javax in imports).| |true|
|useSpringController|Annotate the generated API as a Spring Controller| |false|
|useSwaggerUI|Open the OpenApi specification in swagger-ui. Will also import and configure needed dependencies| |true|
|useTags|use tags for creating interface and controller classnames| |false|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String HATEOAS = "hateoas";
public static final String RETURN_SUCCESS_CODE = "returnSuccessCode";
public static final String UNHANDLED_EXCEPTION_HANDLING = "unhandledException";
public static final String USE_SPRING_BOOT3 = "useSpringBoot3";
public static final String USE_JAKARTA_EE = "useJakartaEe";

public static final String OPEN_BRACE = "{";
public static final String CLOSE_BRACE = "}";
Expand Down Expand Up @@ -126,6 +128,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean unhandledException = false;
protected boolean useSpringController = false;
protected boolean useSwaggerUI = true;
protected boolean useSpringBoot3 = false;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -204,6 +207,9 @@ public SpringCodegen() {
cliOptions.add(CliOption.newBoolean(USE_SWAGGER_UI,
"Open the OpenApi specification in swagger-ui. Will also import and configure needed dependencies",
useSwaggerUI));
cliOptions.add(CliOption.newBoolean(USE_SPRING_BOOT3,
"Generate code and provide dependencies for use with Spring Boot 3.x. (Use jakarta instead of javax in imports).",
useSwaggerUI));

supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
supportedLibraries.put(SPRING_CLOUD_LIBRARY,
Expand Down Expand Up @@ -415,6 +421,22 @@ public void processOpts() {
}
additionalProperties.put(UNHANDLED_EXCEPTION_HANDLING, this.isUnhandledException());

if (additionalProperties.containsKey(USE_SPRING_BOOT3)) {
this.setUseSpringBoot3(convertPropertyToBoolean(USE_SPRING_BOOT3));
}
if (isUseSpringBoot3()) {
if (DocumentationProvider.SPRINGFOX.equals(getDocumentationProvider())) {
throw new IllegalArgumentException(DocumentationProvider.SPRINGFOX.getPropertyName() + " is not supported with Spring Boot > 3.x");
}
if (AnnotationLibrary.SWAGGER1.equals(getAnnotationLibrary())) {
throw new IllegalArgumentException(AnnotationLibrary.SWAGGER1.getPropertyName() + " is not supported with Spring Boot > 3.x");
}
writePropertyBack(USE_JAKARTA_EE, true);
} else {
writePropertyBack(USE_JAKARTA_EE, false);
}
writePropertyBack(USE_SPRING_BOOT3, isUseSpringBoot3());

typeMapping.put("file", "org.springframework.core.io.Resource");
importMapping.put("org.springframework.core.io.Resource", "org.springframework.core.io.Resource");
importMapping.put("Pageable", "org.springframework.data.domain.Pageable");
Expand All @@ -431,7 +453,12 @@ public void processOpts() {
additionalProperties.put("delegate-method", true);
}

supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
if (isUseSpringBoot3()) {
supportingFiles.add(new SupportingFile("pom-sb3.mustache", "", "pom.xml"));
} else {
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
}

supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));

if (!interfaceOnly) {
Expand Down Expand Up @@ -1045,4 +1072,12 @@ public List<VendorExtension> getSupportedVendorExtensions() {
extensions.add(VendorExtension.X_SPRING_PAGINATED);
return extensions;
}

public boolean isUseSpringBoot3() {
return useSpringBoot3;
}

public void setUseSpringBoot3(boolean useSpringBoot3) {
this.useSpringBoot3 = useSpringBoot3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ import org.springframework.http.codec.multipart.Part;
{{/reactive}}

{{#useBeanValidation}}
{{#useJakartaEe}}
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.validation.Valid;
import javax.validation.constraints.*;
{{/useJakartaEe}}
{{/useBeanValidation}}
import java.util.List;
import java.util.Map;
Expand All @@ -66,7 +72,12 @@ import java.util.Optional;
{{#async}}
import java.util.concurrent.CompletableFuture;
{{/async}}
{{#useJakartaEe}}
import jakarta.annotation.Generated;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.annotation.Generated;
{{/useJakartaEe}}

{{>generatedAnnotation}}
{{#useBeanValidation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,25 @@ import org.springframework.web.context.request.NativeWebRequest;
{{/isDelegate}}

{{#useBeanValidation}}
{{#useJakartaEe}}
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.validation.constraints.*;
import javax.validation.Valid;
{{/useJakartaEe}}
{{/useBeanValidation}}

import java.util.List;
import java.util.Map;
import java.util.Optional;
{{#useJakartaEe}}
import jakarta.annotation.Generated;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.annotation.Generated;
{{/useJakartaEe}}

{{>generatedAnnotation}}
@Controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import java.util.Optional;
{{#async}}
import java.util.concurrent.CompletableFuture;
{{/async}}
{{#useJakartaEe}}
import jakarta.annotation.Generated;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.annotation.Generated;
{{/useJakartaEe}}

{{#operations}}
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package {{apiPackage}};

{{#useJakartaEe}}
import jakarta.annotation.Generated;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.annotation.Generated;
{{/useJakartaEe}}

/**
* The exception that can be used to store the HTTP status code returned by an API response.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package {{apiPackage}};

import java.io.IOException;

{{#useJakartaEe}}
import jakarta.annotation.Generated;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletResponse;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.annotation.Generated;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
{{/useJakartaEe}}

{{>generatedAnnotation}}
public class ApiOriginFilter implements javax.servlet.Filter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package {{apiPackage}};

{{#useJakartaEe}}
import jakarta.annotation.Generated;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.annotation.Generated;
{{/useJakartaEe}}
import javax.xml.bind.annotation.XmlTransient;

{{>generatedAnnotation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import reactor.core.publisher.Mono;
{{^reactive}}
import org.springframework.web.context.request.NativeWebRequest;

{{#useJakartaEe}}
import jakarta.servlet.http.HttpServletResponse;
{{/useJakartaEe}}
{{^useJakartaEe}}
import javax.servlet.http.HttpServletResponse;
{{/useJakartaEe}}
import java.io.IOException;
{{/reactive}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ minLength not set, maxLength set
@Size: minItems not set && maxItems set
}}{{^minItems}}{{#maxItems}}@Size(max = {{.}}) {{/maxItems}}{{/minItems}}{{!
@Email: useBeanValidation set && isEmail set
}}{{#useBeanValidation}}{{#isEmail}}@javax.validation.constraints.Email{{/isEmail}}{{/useBeanValidation}}{{!
}}{{#useBeanValidation}}{{#isEmail}}@Email{{/isEmail}}{{/useBeanValidation}}{{!
check for integer or long / all others=decimal type with @Decimal*
isInteger set
}}{{#isInteger}}{{#minimum}}@Min({{.}}) {{/minimum}}{{#maximum}}@Max({{.}}) {{/maximum}}{{/isInteger}}{{!
Expand Down
Loading