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

added custom date converter in order to fix issue. #11153

Merged
merged 2 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig";
public static final String TARGET_OPENFEIGN = "generateForOpenFeign";
public static final String DEFAULT_INTERFACES = "defaultInterfaces";
public static final String DATE_PATTERN = "datePattern";
public static final String DATE_TIME_PATTERN = "dateTimePattern";

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

supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
Expand Down Expand Up @@ -293,6 +297,10 @@ public void processOpts() {
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiOriginFilter.java"));
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java"));
supportingFiles.add(new SupportingFile("LocalDateConverter.mustache",
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "LocalDateConverter.java"));
supportingFiles.add(new SupportingFile("LocalDateTimeConverter.mustache",
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "LocalDateTimeConverter.java"));
}
} else if ( this.swaggerDocketConfig && !library.equals(SPRING_CLOUD_LIBRARY)) {
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.swagger.configuration;

import org.springframework.core.convert.converter.Converter;
import org.threeten.bp.LocalDate;
import org.threeten.bp.format.DateTimeFormatter;

public class LocalDateConverter implements Converter<String, LocalDate> {
private final DateTimeFormatter formatter;

public LocalDateConverter(String dateFormat) {
this.formatter = DateTimeFormatter.ofPattern(dateFormat);
}

@Override
public LocalDate convert(String source) {
if(source == null || source.isEmpty()) {
return null;
}
return LocalDate.parse(source, this.formatter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.swagger.configuration;

import org.springframework.core.convert.converter.Converter;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.format.DateTimeFormatter;

public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
private final DateTimeFormatter formatter;
public LocalDateTimeConverter(String dateFormat) {
this.formatter = DateTimeFormatter.ofPattern(dateFormat);
}

@Override
public LocalDateTime convert(String source) {
if(source == null || source.isEmpty()) {
return null;
}
return LocalDateTime.parse(source, this.formatter);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package {{basePackage}};

import {{configPackage}}.LocalDateConverter;
import {{configPackage}}.LocalDateTimeConverter;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

@Configuration
static class MyConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new LocalDateConverter("{{#datePattern}}{{datePattern}}{{/datePattern}}{{^datePattern}}yyyy-MM-dd{{/datePattern}}"));
registry.addConverter(new LocalDateTimeConverter("{{#dateTimePattern}}{{dateTimePattern}}{{/dateTimePattern}}{{^dateTimePattern}}yyyy-MM-dd'T'HH:mm:ss.SSS{{/dateTimePattern}}"));
}
}

class ExitException extends RuntimeException implements ExitCodeGenerator {
private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class SpringOptionsProvider extends JavaOptionsProvider {
public static final String DEFAULT_INTERFACES = "true";
public static final String NOT_NULL_JACKSON_ANNOTATION = "false";
public static final String IGNORE_UNKNOWN_JACKSON_ANNOTATION = "false";
public static final String DATE_PATTERN_VALUE = "yyyy-MM-dd";
public static final String DATE_TIME_PATTERN_VALUE = "yyyy-MM-dd'T'HH:mm:ss.SSS";

@Override
public String getLanguage() {
Expand Down Expand Up @@ -54,6 +56,8 @@ public Map<String, String> createOptions() {
options.put(SpringCodegen.DEFAULT_INTERFACES, DEFAULT_INTERFACES);
options.put(SpringCodegen.NOT_NULL_JACKSON_ANNOTATION,NOT_NULL_JACKSON_ANNOTATION);
options.put(SpringCodegen.IGNORE_UNKNOWN_JACKSON_ANNOTATION, IGNORE_UNKNOWN_JACKSON_ANNOTATION);
options.put(SpringCodegen.DATE_PATTERN, DATE_PATTERN_VALUE);
options.put(SpringCodegen.DATE_TIME_PATTERN, DATE_TIME_PATTERN_VALUE);

return options;
}
Expand Down