From 96d21a5b0b5d155c3571223e97c50e8582fd9962 Mon Sep 17 00:00:00 2001 From: DaScheid <75588422+dascheid@users.noreply.github.com> Date: Thu, 3 Oct 2024 15:04:31 +0200 Subject: [PATCH] fix jakarta imports When configuring generator to use imports from the jakarta-namespace, then no javax-imports should be present in the generated files --- .../handlebars/JavaSpring/NotUndefined.mustache | 6 ++++++ .../JavaSpring/NotUndefinedValidator.mustache | 6 ++++++ .../v3/generators/java/SpringGeneratorTest.java | 17 +++++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/resources/handlebars/JavaSpring/NotUndefined.mustache b/src/main/resources/handlebars/JavaSpring/NotUndefined.mustache index be3adc1b42..d63b6e2c46 100644 --- a/src/main/resources/handlebars/JavaSpring/NotUndefined.mustache +++ b/src/main/resources/handlebars/JavaSpring/NotUndefined.mustache @@ -1,7 +1,13 @@ package {{configPackage}}; +{{#jakarta}} +import jakarta.validation.Constraint; +import jakarta.validation.Payload; +{{/jakarta}} +{{^jakarta}} import javax.validation.Constraint; import javax.validation.Payload; +{{/jakarta}} import java.lang.annotation.*; @Target({ElementType.TYPE}) diff --git a/src/main/resources/handlebars/JavaSpring/NotUndefinedValidator.mustache b/src/main/resources/handlebars/JavaSpring/NotUndefinedValidator.mustache index 92dc13018e..d143f20907 100644 --- a/src/main/resources/handlebars/JavaSpring/NotUndefinedValidator.mustache +++ b/src/main/resources/handlebars/JavaSpring/NotUndefinedValidator.mustache @@ -1,8 +1,14 @@ package {{configPackage}}; import org.openapitools.jackson.nullable.JsonNullable; +{{#jakarta}} +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +{{/jakarta}} +{{^jakarta}} import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; +{{/jakarta}} import java.lang.reflect.Field; public class NotUndefinedValidator implements ConstraintValidator{ diff --git a/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorTest.java b/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorTest.java index 0d66ade7c0..71a3f79856 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorTest.java @@ -16,6 +16,8 @@ import java.nio.file.Files; import java.util.List; +import static org.testng.Assert.*; + public class SpringGeneratorTest extends AbstractCodegenTest { @Test public void testGenerator() throws Exception { @@ -37,12 +39,13 @@ public void testGenerator() throws Exception { // .addAdditionalProperty("validationMode", "legacyNullable") // .addAdditionalProperty("useBeanValidation", false) // .addAdditionalProperty("useNullableForNotNull", false) + .addAdditionalProperty("jakarta", true) .outputDir(path) ); List files = new GeneratorService().generationRequest(request).generate(); Assert.assertFalse(files.isEmpty()); -/* for (File f: files) { + for (File f: files) { // test stuff if (f.getName().endsWith("Pet.java")) { String content = new String(Files.readAllBytes(f.toPath())); @@ -60,7 +63,17 @@ public void testGenerator() throws Exception { String content = new String(Files.readAllBytes(f.toPath())); // System.out.println(content); } - }*/ + if (f.getName().endsWith("NotUndefined.java")) { + String content = new String(Files.readAllBytes(f.toPath())); + assertNotNull(content); + assertFalse(content.contains("import javax.validation")); + } + if (f.getName().endsWith("NotUndefinedValidator.java")) { + String content = new String(Files.readAllBytes(f.toPath())); + assertNotNull(content); + assertFalse(content.contains("import javax.validation")); + } + } } protected static File getTmpFolder() {