Skip to content

Commit 9b181e1

Browse files
authored
Merge pull request #11153 from swagger-api/issue-4113
added custom date converter in order to fix issue.
2 parents 90319be + facbe86 commit 9b181e1

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class SpringCodegen extends AbstractJavaCodegen
3939
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig";
4040
public static final String TARGET_OPENFEIGN = "generateForOpenFeign";
4141
public static final String DEFAULT_INTERFACES = "defaultInterfaces";
42+
public static final String DATE_PATTERN = "datePattern";
43+
public static final String DATE_TIME_PATTERN = "dateTimePattern";
4244

4345
protected String title = "swagger-petstore";
4446
protected String configPackage = "io.swagger.configuration";
@@ -94,6 +96,8 @@ public SpringCodegen() {
9496
"Use Optional container for optional parameters"));
9597
cliOptions.add(CliOption.newBoolean(TARGET_OPENFEIGN,"Generate for usage with OpenFeign (instead of feign)"));
9698
cliOptions.add(CliOption.newBoolean(DEFAULT_INTERFACES, "Generate default implementations for interfaces").defaultValue("true"));
99+
cliOptions.add(CliOption.newBoolean(DATE_PATTERN, "use pattern for date parameters").defaultValue("true"));
100+
cliOptions.add(CliOption.newBoolean(DATE_TIME_PATTERN, "use pattern for date time parameters").defaultValue("true"));
97101

98102
supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
99103
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@@ -293,6 +297,10 @@ public void processOpts() {
293297
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiOriginFilter.java"));
294298
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
295299
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java"));
300+
supportingFiles.add(new SupportingFile("LocalDateConverter.mustache",
301+
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "LocalDateConverter.java"));
302+
supportingFiles.add(new SupportingFile("LocalDateTimeConverter.mustache",
303+
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "LocalDateTimeConverter.java"));
296304
}
297305
} else if ( this.swaggerDocketConfig && !library.equals(SPRING_CLOUD_LIBRARY)) {
298306
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.swagger.configuration;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
import org.threeten.bp.LocalDate;
5+
import org.threeten.bp.format.DateTimeFormatter;
6+
7+
public class LocalDateConverter implements Converter<String, LocalDate> {
8+
private final DateTimeFormatter formatter;
9+
10+
public LocalDateConverter(String dateFormat) {
11+
this.formatter = DateTimeFormatter.ofPattern(dateFormat);
12+
}
13+
14+
@Override
15+
public LocalDate convert(String source) {
16+
if(source == null || source.isEmpty()) {
17+
return null;
18+
}
19+
return LocalDate.parse(source, this.formatter);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.swagger.configuration;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
import org.threeten.bp.LocalDateTime;
5+
import org.threeten.bp.format.DateTimeFormatter;
6+
7+
public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
8+
private final DateTimeFormatter formatter;
9+
10+
public LocalDateTimeConverter(String dateFormat) {
11+
this.formatter = DateTimeFormatter.ofPattern(dateFormat);
12+
}
13+
14+
@Override
15+
public LocalDateTime convert(String source) {
16+
if(source == null || source.isEmpty()) {
17+
return null;
18+
}
19+
return LocalDateTime.parse(source, this.formatter);
20+
}
21+
}

modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package {{basePackage}};
22

3+
import {{configPackage}}.LocalDateConverter;
4+
import {{configPackage}}.LocalDateTimeConverter;
35
import org.springframework.boot.CommandLineRunner;
46
import org.springframework.boot.ExitCodeGenerator;
57
import org.springframework.boot.SpringApplication;
68
import org.springframework.boot.autoconfigure.SpringBootApplication;
79
import org.springframework.context.annotation.ComponentScan;
810

11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.format.FormatterRegistry;
13+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
914
import springfox.documentation.swagger2.annotations.EnableSwagger2;
1015

1116
@SpringBootApplication
@@ -24,6 +29,15 @@ public class Swagger2SpringBoot implements CommandLineRunner {
2429
new SpringApplication(Swagger2SpringBoot.class).run(args);
2530
}
2631

32+
@Configuration
33+
static class MyConfig extends WebMvcConfigurerAdapter {
34+
@Override
35+
public void addFormatters(FormatterRegistry registry) {
36+
registry.addConverter(new LocalDateConverter("{{#datePattern}}{{datePattern}}{{/datePattern}}{{^datePattern}}yyyy-MM-dd{{/datePattern}}"));
37+
registry.addConverter(new LocalDateTimeConverter("{{#dateTimePattern}}{{dateTimePattern}}{{/dateTimePattern}}{{^dateTimePattern}}yyyy-MM-dd'T'HH:mm:ss.SSS{{/dateTimePattern}}"));
38+
}
39+
}
40+
2741
class ExitException extends RuntimeException implements ExitCodeGenerator {
2842
private static final long serialVersionUID = 1L;
2943

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class SpringOptionsProvider extends JavaOptionsProvider {
2626
public static final String DEFAULT_INTERFACES = "true";
2727
public static final String NOT_NULL_JACKSON_ANNOTATION = "false";
2828
public static final String IGNORE_UNKNOWN_JACKSON_ANNOTATION = "false";
29+
public static final String DATE_PATTERN_VALUE = "yyyy-MM-dd";
30+
public static final String DATE_TIME_PATTERN_VALUE = "yyyy-MM-dd'T'HH:mm:ss.SSS";
2931

3032
@Override
3133
public String getLanguage() {
@@ -54,6 +56,8 @@ public Map<String, String> createOptions() {
5456
options.put(SpringCodegen.DEFAULT_INTERFACES, DEFAULT_INTERFACES);
5557
options.put(SpringCodegen.NOT_NULL_JACKSON_ANNOTATION,NOT_NULL_JACKSON_ANNOTATION);
5658
options.put(SpringCodegen.IGNORE_UNKNOWN_JACKSON_ANNOTATION, IGNORE_UNKNOWN_JACKSON_ANNOTATION);
59+
options.put(SpringCodegen.DATE_PATTERN, DATE_PATTERN_VALUE);
60+
options.put(SpringCodegen.DATE_TIME_PATTERN, DATE_TIME_PATTERN_VALUE);
5761

5862
return options;
5963
}

0 commit comments

Comments
 (0)