added date library option

This commit is contained in:
Tony Tam 2016-01-15 14:20:20 -08:00 committed by Maelig Nantel
parent a39a89521f
commit 09e32bb959
4 changed files with 42 additions and 43 deletions

View File

@ -20,7 +20,9 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
public static final String FULL_JAVA_UTIL = "fullJavaUtil";
public static final String DEFAULT_LIBRARY = "<default>";
public static final String DATE_LIBRARY = "dateLibrary";
protected String dateLibrary = "default";
protected String invokerPackage = "io.swagger.client";
protected String groupId = "io.swagger";
protected String artifactId = "swagger-java-client";
@ -97,6 +99,14 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
library.setEnum(supportedLibraries);
library.setDefault(DEFAULT_LIBRARY);
cliOptions.add(library);
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
Map<String, String> dateOptions = new HashMap<String, String>();
dateOptions.put("java8", "Java 8 native");
dateOptions.put("joda", "Joda");
dateLibrary.setEnum(dateOptions);
cliOptions.add(dateLibrary);
}
@Override
@ -252,6 +262,32 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
} else if("jersey2".equals(getLibrary())) {
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
}
if("joda".equals(dateLibrary)) {
typeMapping.put("date", "LocalDate");
typeMapping.put("DateTime", "DateTime");
importMapping.put("LocalDate", "org.joda.time.LocalDate");
importMapping.put("DateTime", "org.joda.time.DateTime");
supportingFiles.add(new SupportingFile("JodaDateTimeProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaDateTimeProvider.java"));
supportingFiles.add(new SupportingFile("JodaLocalDateProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaLocalDateProvider.java"));
}
else if ("java8".equals(dateLibrary)) {
additionalProperties.put("java8", "true");
additionalProperties.put("javaVersion", "1.8");
typeMapping.put("date", "LocalDate");
typeMapping.put("DateTime", "LocalDateTime");
importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
supportingFiles.add(new SupportingFile("LocalDateTimeProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateTimeProvider.java"));
supportingFiles.add(new SupportingFile("LocalDateProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateProvider.java"));
}
}
private void sanitizeConfig() {
@ -760,4 +796,8 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
public void setFullJavaUtil(boolean fullJavaUtil) {
this.fullJavaUtil = fullJavaUtil;
}
public void setDateLibrary(String library) {
this.dateLibrary = library;
}
}

View File

@ -9,11 +9,9 @@ import java.io.File;
import java.util.*;
public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConfig {
protected String dateLibrary = "default";
protected String title = "Swagger Server";
protected String implFolder = "src/main/java";
public static final String DATE_LIBRARY = "dateLibrary";
public JaxRSServerCodegen() {
super();
@ -41,14 +39,6 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
}
}
CliOption dateLibrary = new CliOption(DATE_LIBRARY, "Option. Date library to use");
Map<String, String> dateOptions = new HashMap<String, String>();
dateOptions.put("java8", "Java 8 native");
dateOptions.put("joda", "Joda");
dateLibrary.setEnum(dateOptions);
cliOptions.add(dateLibrary);
CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
library.setDefault(DEFAULT_LIBRARY);
@ -109,32 +99,6 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
setDateLibrary(additionalProperties.get("dateLibrary").toString());
additionalProperties.put(dateLibrary, "true");
}
if("joda".equals(dateLibrary)) {
typeMapping.put("date", "LocalDate");
typeMapping.put("DateTime", "DateTime");
importMapping.put("LocalDate", "org.joda.time.LocalDate");
importMapping.put("DateTime", "org.joda.time.DateTime");
supportingFiles.add(new SupportingFile("JodaDateTimeProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaDateTimeProvider.java"));
supportingFiles.add(new SupportingFile("JodaLocalDateProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "JodaLocalDateProvider.java"));
}
else if ("java8".equals(dateLibrary)) {
additionalProperties.put("java8", "true");
additionalProperties.put("javaVersion", "1.8");
typeMapping.put("date", "LocalDate");
typeMapping.put("DateTime", "LocalDateTime");
importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("LocalDateTime", "java.time.LocalDateTime");
supportingFiles.add(new SupportingFile("LocalDateTimeProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateTimeProvider.java"));
supportingFiles.add(new SupportingFile("LocalDateProvider.mustache",
(sourceFolder + '/' + apiPackage).replace(".", "/"), "LocalDateProvider.java"));
}
}
@Override
@ -291,8 +255,4 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
public boolean shouldOverwrite(String filename) {
return super.shouldOverwrite(filename) && !filename.endsWith("ServiceImpl.java") && !filename.endsWith("ServiceFactory.java");
}
public void setDateLibrary(String library) {
this.dateLibrary = library;
}
}

View File

@ -39,6 +39,7 @@ public class JavaOptionsProvider implements OptionsProvider {
.put(JavaClientCodegen.FULL_JAVA_UTIL, FULL_JAVA_UTIL_VALUE)
.put(CodegenConstants.LIBRARY, LIBRARY_VALUE)
.put(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, "true")
.put(JavaClientCodegen.DATE_LIBRARY, "joda")
.build();
}

View File

@ -1,7 +1,6 @@
package io.swagger.codegen.options;
import com.google.common.collect.ImmutableMap;
import io.swagger.codegen.languages.JaxRSServerCodegen;
import io.swagger.codegen.CodegenConstants;
import java.util.Map;
@ -26,8 +25,7 @@ public class JaxRSServerOptionsProvider extends JavaOptionsProvider {
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.putAll(options)
.put(CodegenConstants.IMPL_FOLDER, IMPL_FOLDER_VALUE)
.put(JaxRSServerCodegen.DATE_LIBRARY, "joda");
.put(CodegenConstants.IMPL_FOLDER, IMPL_FOLDER_VALUE);
return builder.build();
}