From d63145be72b2076f61b14c66761636d591a27c33 Mon Sep 17 00:00:00 2001 From: frantuma Date: Tue, 15 Oct 2024 12:02:59 +0200 Subject: [PATCH] refs #1308 - fix jakarta spring generator --- .../v3/generators/java/SpringCodegen.java | 2 ++ .../JavaSpring/NotUndefined.mustache | 6 ++++++ .../JavaSpring/NotUndefinedValidator.mustache | 6 ++++++ .../customInstantDeserializer.mustache | 6 ++++++ .../libraries/spring-boot/pom.mustache | 19 ++++++++++++++++++- .../spring-boot/swagger2SpringBoot.mustache | 11 ++++++++++- .../libraries/spring-boot3/pom.mustache | 11 ++++++++--- .../generators/java/SpringGeneratorTest.java | 2 ++ 8 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java index 968e672084..ce31cc6c0d 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java @@ -233,6 +233,8 @@ public void processOpts() { if (isSpringBoot3Library()) { setDateLibrary("java8"); + setJakarta(true); + additionalProperties.put(JAKARTA, jakarta); } super.processOpts(); 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/main/resources/handlebars/JavaSpring/customInstantDeserializer.mustache b/src/main/resources/handlebars/JavaSpring/customInstantDeserializer.mustache index da7b57a613..0ac2179487 100644 --- a/src/main/resources/handlebars/JavaSpring/customInstantDeserializer.mustache +++ b/src/main/resources/handlebars/JavaSpring/customInstantDeserializer.mustache @@ -200,7 +200,13 @@ public class CustomInstantDeserializer return value; } } + {{#jakarta}} + context.reportInputMismatch(handledType(), "Expected type float, integer, or string."); + return null; + {{/jakarta}} + {{^jakarta}} throw context.mappingException("Expected type float, integer, or string."); + {{/jakarta}} } private ZoneId getZone(DeserializationContext context) { diff --git a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache index 1a18ebd250..f524cc226c 100644 --- a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache +++ b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache @@ -6,9 +6,14 @@ {{artifactId}} {{artifactVersion}} + {{#jakarta}} + 17 + {{/jakarta}} + {{^jakarta}} {{#java11}}11{{/java11}}{{^java11}}{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}{{/java11}} ${java.version} ${java.version} + {{/jakarta}} {{#useOas2}} 2.9.2 {{/useOas2}} @@ -19,7 +24,12 @@ org.springframework.boot spring-boot-starter-parent + {{#jakarta}} + 3.3.4 + {{/jakarta}} + {{^jakarta}} {{springBootVersion}} + {{/jakarta}} src/main/java @@ -66,7 +76,13 @@ org.springframework.boot spring-boot-starter-tomcat - + {{#jakarta}} + + org.hibernate.validator + hibernate-validator + 8.0.1.Final + + {{/jakarta}} {{#useOas2}} @@ -139,6 +155,7 @@ jakarta.validation jakarta.validation-api + 3.0.2 {{/jakarta}} {{^jakarta}} diff --git a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache index 46fd67794d..a8bdebda3b 100644 --- a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache +++ b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache @@ -27,8 +27,12 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; +{{#jakarta}} +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +{{/jakarta}} +{{^jakarta}} import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - +{{/jakarta}} @SpringBootApplication {{#useOas2}} @EnableSwagger2 @@ -62,7 +66,12 @@ public class Swagger2SpringBoot implements CommandLineRunner { {{/useBeanValidation}} @Configuration + {{#jakarta}} + static class CustomDateConfig implements WebMvcConfigurer { + {{/jakarta}} + {{^jakarta}} static class CustomDateConfig extends WebMvcConfigurerAdapter { + {{/jakarta}} @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new LocalDateConverter("{{#datePattern}}{{datePattern}}{{/datePattern}}{{^datePattern}}yyyy-MM-dd{{/datePattern}}")); diff --git a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot3/pom.mustache b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot3/pom.mustache index d1713dd3e7..18dc2b9479 100644 --- a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot3/pom.mustache +++ b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot3/pom.mustache @@ -8,14 +8,14 @@ UTF-8 17 - 3.1.1 - 2.2.14 + 3.3.4 + 2.2.25 1.7.0 org.springframework.boot spring-boot-starter-parent - 3.1.1 + 3.3.4 src/main/java @@ -44,6 +44,11 @@ spring-boot-starter-tomcat ${springboot-version} + + org.hibernate.validator + hibernate-validator + 8.0.1.Final + {{#withXml}} com.fasterxml.jackson.dataformat 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..84f91b8845 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 @@ -32,6 +32,8 @@ public void testGenerator() throws Exception { false)) // OpenAPI 3.x - use true for Swagger/OpenAPI 2.x definitions .options( new Options() + // .library("spring-boot3") + // .addAdditionalProperty("jakarta", true) // .addAdditionalProperty("validationMode", "loose") // .addAdditionalProperty("validationMode", "legacy") // .addAdditionalProperty("validationMode", "legacyNullable")