From e4b1ab98925ae619e45db9394f071feba4c2290f Mon Sep 17 00:00:00 2001 From: Thierry <57956193+tgillain@users.noreply.github.com> Date: Thu, 8 Jun 2023 16:30:39 +0200 Subject: [PATCH 1/2] fix: Use jakarta.servlet import in generated *Api.java when the jakarta property is true --- .../handlebars/JavaSpring/api.mustache | 5 ++ .../java/SpringGeneratorCodegenTest.java | 73 ++++++++++++++++--- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/main/resources/handlebars/JavaSpring/api.mustache b/src/main/resources/handlebars/JavaSpring/api.mustache index 08a3bfd92c..6546e1008d 100644 --- a/src/main/resources/handlebars/JavaSpring/api.mustache +++ b/src/main/resources/handlebars/JavaSpring/api.mustache @@ -44,7 +44,12 @@ import org.springframework.web.multipart.MultipartFile; import org.springframework.web.bind.annotation.CookieValue; {{#jdk8-no-delegate}} +{{#jakarta}} +import jakarta.servlet.http.HttpServletRequest; +{{/jakarta}} +{{^jakarta}} import javax.servlet.http.HttpServletRequest; +{{/jakarta}} {{/jdk8-no-delegate}} {{#useBeanValidation}} {{#jakarta}} diff --git a/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java b/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java index 8ebeb1fc54..57836b88e5 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java @@ -1,6 +1,7 @@ package io.swagger.codegen.v3.generators.java; import static io.swagger.codegen.v3.generators.java.AbstractJavaCodegen.JAKARTA; +import static io.swagger.codegen.v3.generators.java.AbstractJavaCodegen.JAVA8_MODE; import io.swagger.codegen.v3.ClientOptInput; import io.swagger.codegen.v3.CodegenArgument; @@ -88,16 +89,16 @@ public void testOas2AndJakarta() throws Exception { final File output = folder.getRoot(); final CodegenConfigurator configurator = new CodegenConfigurator() - .setLang("spring") - .setInputSpecURL("src/test/resources/3_0_0/parameterOrder.yaml") - .setOutputDir(output.getAbsolutePath()) - .addAdditionalProperty(JAKARTA, true); + .setLang("spring") + .setInputSpecURL("src/test/resources/3_0_0/parameterOrder.yaml") + .setOutputDir(output.getAbsolutePath()) + .addAdditionalProperty(JAKARTA, true); configurator.setCodegenArguments(Collections.singletonList( - new CodegenArgument() - .option(CodegenConstants.USE_OAS2_OPTION) - .type("boolean") - .value(Boolean.TRUE.toString()))); + new CodegenArgument() + .option(CodegenConstants.USE_OAS2_OPTION) + .type("boolean") + .value(Boolean.TRUE.toString()))); final ClientOptInput clientOptInput = configurator.toClientOptInput(); new DefaultGenerator().opts(clientOptInput).generate(); @@ -124,10 +125,10 @@ public void testUseOas3AndJakarta() throws Exception { final File output = folder.getRoot(); final CodegenConfigurator configurator = new CodegenConfigurator() - .setLang("spring") - .setInputSpecURL("src/test/resources/3_0_0/parameterOrder.yaml") - .setOutputDir(output.getAbsolutePath()) - .addAdditionalProperty(JAKARTA, true); + .setLang("spring") + .setInputSpecURL("src/test/resources/3_0_0/parameterOrder.yaml") + .setOutputDir(output.getAbsolutePath()) + .addAdditionalProperty(JAKARTA, true); final ClientOptInput clientOptInput = configurator.toClientOptInput(); new DefaultGenerator().opts(clientOptInput).generate(); @@ -148,4 +149,52 @@ public void testUseOas3AndJakarta() throws Exception { folder.delete(); } + @Test(description = "verify java8 & jakarta") + public void testJava8Jakarta() throws Exception { + final TemporaryFolder folder = new TemporaryFolder(); + folder.create(); + final File output = folder.getRoot(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setLang("spring") + .setInputSpecURL("src/test/resources/3_0_0/parameterOrder.yaml") + .setOutputDir(output.getAbsolutePath()) + .addAdditionalProperty(JAKARTA, true) + .addAdditionalProperty(JAVA8_MODE, true); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + new DefaultGenerator().opts(clientOptInput).generate(); + + final File adminApiFile = new File(output, "/src/main/java/io/swagger/api/AdminApi.java"); + final String contentAdminApi = FileUtils.readFileToString(adminApiFile); + + Assert.assertTrue(contentAdminApi.contains("import jakarta.servlet.http.HttpServletRequest;")); + + folder.delete(); + } + + @Test(description = "verify java8 & javax") + public void testJava8Javax() throws Exception { + final TemporaryFolder folder = new TemporaryFolder(); + folder.create(); + final File output = folder.getRoot(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setLang("spring") + .setInputSpecURL("src/test/resources/3_0_0/parameterOrder.yaml") + .setOutputDir(output.getAbsolutePath()) + .addAdditionalProperty(JAKARTA, false) + .addAdditionalProperty(JAVA8_MODE, true); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + new DefaultGenerator().opts(clientOptInput).generate(); + + final File adminApiFile = new File(output, "/src/main/java/io/swagger/api/AdminApi.java"); + final String contentAdminApi = FileUtils.readFileToString(adminApiFile); + + Assert.assertTrue(contentAdminApi.contains("import javax.servlet.http.HttpServletRequest;")); + + folder.delete(); + } + } From e7ac39a2224268228f99f7e4ea55cb4dc3783e83 Mon Sep 17 00:00:00 2001 From: tgillain <57956193+tgillain@users.noreply.github.com> Date: Fri, 16 Jun 2023 11:41:14 +0200 Subject: [PATCH 2/2] Update SpringGeneratorCodegenTest.java Revert wrong formatting change --- .../java/SpringGeneratorCodegenTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java b/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java index 57836b88e5..2717890eca 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java @@ -30,10 +30,10 @@ public void testParameterOrdersUseOas2() throws Exception { .setOutputDir(output.getAbsolutePath()); configurator.setCodegenArguments(Collections.singletonList( - new CodegenArgument() - .option(CodegenConstants.USE_OAS2_OPTION) - .type("boolean") - .value(Boolean.TRUE.toString()))); + new CodegenArgument() + .option(CodegenConstants.USE_OAS2_OPTION) + .type("boolean") + .value(Boolean.TRUE.toString()))); final ClientOptInput clientOptInput = configurator.toClientOptInput(); new DefaultGenerator().opts(clientOptInput).generate(); @@ -95,10 +95,10 @@ public void testOas2AndJakarta() throws Exception { .addAdditionalProperty(JAKARTA, true); configurator.setCodegenArguments(Collections.singletonList( - new CodegenArgument() - .option(CodegenConstants.USE_OAS2_OPTION) - .type("boolean") - .value(Boolean.TRUE.toString()))); + new CodegenArgument() + .option(CodegenConstants.USE_OAS2_OPTION) + .type("boolean") + .value(Boolean.TRUE.toString()))); final ClientOptInput clientOptInput = configurator.toClientOptInput(); new DefaultGenerator().opts(clientOptInput).generate();