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][jaxrs-spec] Add option to use jakarta imports / namespace #13593

Closed
wants to merge 13 commits into from
3 changes: 2 additions & 1 deletion .github/workflows/samples-jaxrs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
- samples/server/petstore/jaxrs/jersey2-useTags
- samples/server/petstore/jaxrs-jersey
- samples/server/petstore/jaxrs-spec
- samples/server/petstore/jaxrs-spec-ee
- samples/server/petstore/jaxrs-spec-interface
- samples/server/petstore/jaxrs-spec-interface-response
- samples/server/petstore/jaxrs-jersey
Expand All @@ -43,7 +44,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 8
java-version: 11
- name: Cache maven dependencies
uses: actions/cache@v3
env:
Expand Down
11 changes: 11 additions & 0 deletions bin/configs/jaxrs-spec-ee.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
generatorName: jaxrs-spec
outputDir: samples/server/petstore/jaxrs-spec-ee
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaJaxRS/spec
additionalProperties:
artifactId: jaxrs-spec-petstore-server
serializableModel: "true"
hideGenerationTimestamp: "true"
implicitHeadersRegex: (api_key|enum_header_string)
generateBuilders: "true"
useJakartaEE: true
2 changes: 2 additions & 0 deletions docs/generators/jaxrs-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|testOutput|Set output folder for models and APIs tests| |${project.build.directory}/generated-test-sources/openapi|
|title|a title describing the application| |OpenAPI Server|
|useBeanValidation|Use BeanValidation API annotations| |true|
|useJakartaEE|Generate code and provide dependencies for use with Jakarta EE. (Use jakarta instead of javax in imports).| |false|
|useSwaggerAnnotations|Whether to generate Swagger annotations.| |true|
|useTags|use tags for creating interface and controller classnames| |false|
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
| useJakartaEE | Use Jakarta imports | |false|

## SUPPORTED VENDOR EXTENSIONS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
public static final String USE_SWAGGER_ANNOTATIONS = "useSwaggerAnnotations";
public static final String OPEN_API_SPEC_FILE_LOCATION = "openApiSpecFileLocation";
public static final String GENERATE_BUILDERS = "generateBuilders";
public static final String USE_JAKARTA_EE = "useJakartaEE";


public static final String QUARKUS_LIBRARY = "quarkus";
public static final String THORNTAIL_LIBRARY = "thorntail";
public static final String OPEN_LIBERTY_LIBRARY = "openliberty";
public static final String HELIDON_LIBRARY = "helidon";
public static final String KUMULUZEE_LIBRARY = "kumuluzee";

private boolean useJakartaEE = false;
private boolean interfaceOnly = false;
private boolean returnResponse = false;
private boolean generatePom = true;
Expand Down Expand Up @@ -100,6 +103,7 @@ public JavaJAXRSSpecServerCodegen() {
library.setEnum(supportedLibraries);

cliOptions.add(library);
cliOptions.add(CliOption.newBoolean(USE_JAKARTA_EE,"Generate code and provide dependencies for use with Jakarta EE. (Use jakarta instead of javax in imports).").defaultValue(String.valueOf(useJakartaEE)));
cliOptions.add(CliOption.newBoolean(GENERATE_POM, "Whether to generate pom.xml if the file does not already exist.").defaultValue(String.valueOf(generatePom)));
cliOptions.add(CliOption.newBoolean(GENERATE_BUILDERS, "Whether to generate builders for models.").defaultValue(String.valueOf(generateBuilders)));
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.").defaultValue(String.valueOf(interfaceOnly)));
Expand Down Expand Up @@ -179,8 +183,10 @@ public void processOpts() {
.doNotOverwrite());

if (generatePom) {

supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")
.doNotOverwrite());
.doNotOverwrite());

}
if (!interfaceOnly) {
supportingFiles.add(new SupportingFile("RestApplication.mustache",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package {{invokerPackage}};


{{#useJakartaEE}}
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
{{/useJakartaEE}}
{{^useJakartaEE}}
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
{{/useJakartaEE}}

@ApplicationPath(RestResourceRoot.APPLICATION_PATH)
public class RestApplication extends Application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ package {{package}};
{{#imports}}import {{import}};
{{/imports}}


{{#useJakartaEE}}
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;
{{/useJakartaEE}}
{{^useJakartaEE}}
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
{{/useJakartaEE}}



{{#useSwaggerAnnotations}}
import io.swagger.annotations.*;
{{/useSwaggerAnnotations}}

{{#supportAsync}}
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CompletableFuture;
Expand All @@ -17,8 +27,19 @@ import java.util.concurrent.CompletableFuture;
import java.io.InputStream;
import java.util.Map;
import java.util.List;
{{#useBeanValidation}}import javax.validation.constraints.*;
import javax.validation.Valid;{{/useBeanValidation}}

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



@Path("{{commonPath}}"){{#useSwaggerAnnotations}}
@Api(description = "the {{{baseName}}} API"){{/useSwaggerAnnotations}}{{#hasConsumes}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
@javax.annotation.Generated(value = "{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})

{{#useJakartaEE}}
@jakarta.annotation.Generated(value = "{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
{{/useJakartaEE}}
{{^useJakartaEE}}
@javax.annotation.Generated(value = "{{generatorClass}}"{{^hideGenerationTimestamp}}, date = "{{generatedDate}}"{{/hideGenerationTimestamp}})
{{/useJakartaEE}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ package {{package}};
{{#serializableModel}}
import java.io.Serializable;
{{/serializableModel}}


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

{{#models}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,20 @@
<artifactId>joda-time</artifactId>
<version>${joda-version}</version>
</dependency>
{{#useJakartaEE}}
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta.annotation-api-version}</version>
</dependency>
{{/useJakartaEE}}
{{^useJakartaEE}}
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax.annotation-api-version}</version>
</dependency>
{{/useJakartaEE}}
{{#useSwaggerAnnotations}}
<dependency>
<groupId>io.swagger</groupId>
Expand Down Expand Up @@ -155,9 +164,10 @@
<junit-version>4.13.2</junit-version>
<joda-version>2.10.13</joda-version>
<javax.annotation-api-version>1.3.2</javax.annotation-api-version>
<jakarta.annotation-api-version>2.1.1</jakarta.annotation-api-version>
{{#useBeanValidation}}
<beanvalidation-version>2.0.2</beanvalidation-version>
<beanvalidation-version>3.0.2</beanvalidation-version>
{{/useBeanValidation}}
<jakarta.ws.rs-version>2.1.6</jakarta.ws.rs-version>
<jakarta.ws.rs-version>3.1.0</jakarta.ws.rs-version>
</properties>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Do not edit the class manually.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${org.springframework.boot.version}</version>
<executions>
<execution>
<goals>
Expand Down
Loading