Merge pull request #1784 from swagger-api/issue-1035

adds configurable date library, prepares for multiple jaxrs versions
This commit is contained in:
Tony Tam 2015-12-29 23:57:08 -08:00
commit b5e9ca0295
38 changed files with 337 additions and 3 deletions

View File

@ -1 +0,0 @@
{"library":"feign"}

View File

@ -9,8 +9,10 @@ import java.io.File;
import java.util.*;
public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConfig {
protected String dateLibrary = "default";
protected String title = "Swagger Server";
public static final String DATE_LIBRARY = "dateLibrary";
public JaxRSServerCodegen() {
super.processOpts();
@ -24,7 +26,6 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
apiTemplateFiles.put("apiService.mustache", ".java");
apiTemplateFiles.put("apiServiceImpl.mustache", ".java");
apiTemplateFiles.put("apiServiceFactory.mustache", ".java");
embeddedTemplateDir = templateDir = "JavaJaxRS";
apiPackage = System.getProperty("swagger.codegen.jaxrs.apipackage", "io.swagger.api");
modelPackage = System.getProperty("swagger.codegen.jaxrs.modelpackage", "io.swagger.model");
@ -33,6 +34,34 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
additionalProperties.put(CodegenConstants.ARTIFACT_ID, artifactId);
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
additionalProperties.put("title", title);
embeddedTemplateDir = templateDir = "JavaJaxRS" + File.separator + "jersey1_18";
for(int i = 0; i < cliOptions.size(); i++) {
if(CodegenConstants.LIBRARY.equals(cliOptions.get(i).getOpt())) {
cliOptions.remove(i);
break;
}
}
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);
Map<String, String> supportedLibraries = new LinkedHashMap<String, String>();
supportedLibraries.put(DEFAULT_LIBRARY, "Jersey core 1.18.1");
// supportedLibraries.put("jersey2", "Jersey2 core library 2.x");
library.setEnum(supportedLibraries);
cliOptions.add(library);
}
@Override
@ -54,6 +83,10 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
public void processOpts() {
super.processOpts();
// if("jersey2".equals(getLibrary())) {
// embeddedTemplateDir = templateDir = "JavaJaxRS" + File.separator + "jersey2";
// }
supportingFiles.clear();
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
@ -68,6 +101,36 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf
supportingFiles.add(new SupportingFile("web.mustache",
("src/main/webapp/WEB-INF"), "web.xml"));
if (additionalProperties.containsKey("dateLibrary")) {
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
@ -237,4 +300,8 @@ 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

@ -0,0 +1,44 @@
package {{apiPackage}};
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
import org.joda.time.DateTime;
import java.util.List;
@Provider
public class JodaDateTimeProvider extends PerRequestTypeInjectableProvider<QueryParam, DateTime> {
private final UriInfo uriInfo;
public JodaDateTimeProvider(@Context UriInfo uriInfo) {
super(DateTime.class);
this.uriInfo = uriInfo;
}
@Override
public Injectable<DateTime> getInjectable(final ComponentContext cc, final QueryParam a) {
return new Injectable<DateTime>() {
@Override
public DateTime getValue() {
final List<String> values = uriInfo.getQueryParameters().get(a.value());
if (values == null || values.isEmpty())
return null;
if (values.size() > 1) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
entity(a.value() + " cannot contain multiple values").build());
}
return DateTime.parse(values.get(0));
}
};
}
}

View File

@ -0,0 +1,44 @@
package {{apiPackage}};
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
import org.joda.time.LocalDate;
import java.util.List;
@Provider
public class JodaLocalDateProvider extends PerRequestTypeInjectableProvider<QueryParam, LocalDate> {
private final UriInfo uriInfo;
public JodaLocalDateProvider(@Context UriInfo uriInfo) {
super(LocalDate.class);
this.uriInfo = uriInfo;
}
@Override
public Injectable<LocalDate> getInjectable(final ComponentContext cc, final QueryParam a) {
return new Injectable<LocalDate>() {
@Override
public LocalDate getValue() {
final List<String> values = uriInfo.getQueryParameters().get(a.value());
if (values == null || values.isEmpty())
return null;
if (values.size() > 1) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
entity(a.value() + " cannot contain multiple values").build());
}
return LocalDate.parse(values.get(0));
}
};
}
}

View File

@ -0,0 +1,44 @@
package {{apiPackage}};
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
import java.time.LocalDate;
import java.util.List;
@Provider
public class LocalDateProvider extends PerRequestTypeInjectableProvider<QueryParam, LocalDate> {
private final UriInfo uriInfo;
public LocalDateProvider(@Context UriInfo uriInfo) {
super(LocalDate.class);
this.uriInfo = uriInfo;
}
@Override
public Injectable<LocalDate> getInjectable(final ComponentContext cc, final QueryParam a) {
return new Injectable<LocalDate>() {
@Override
public LocalDate getValue() {
final List<String> values = uriInfo.getQueryParameters().get(a.value());
if (values == null || values.isEmpty())
return null;
if (values.size() > 1) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
entity(a.value() + " cannot contain multiple values").build());
}
return LocalDate.parse(values.get(0));
}
};
}
}

View File

@ -0,0 +1,44 @@
package {{apiPackage}};
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
import java.time.LocalDateTime;
import java.util.List;
@Provider
public class LocalDateTimeProvider extends PerRequestTypeInjectableProvider<QueryParam, LocalDateTime> {
private final UriInfo uriInfo;
public LocalDateTimeProvider(@Context UriInfo uriInfo) {
super(LocalDateTime.class);
this.uriInfo = uriInfo;
}
@Override
public Injectable<LocalDateTime> getInjectable(final ComponentContext cc, final QueryParam a) {
return new Injectable<LocalDateTime>() {
@Override
public LocalDateTime getValue() {
final List<String> values = uriInfo.getQueryParameters().get(a.value());
if (values == null || values.isEmpty())
return null;
if (values.size() > 1) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
entity(a.value() + " cannot contain multiple values").build());
}
return LocalDateTime.parse(values.get(0));
}
};
}
}

View File

@ -17,7 +17,7 @@ import java.nio.charset.StandardCharsets;
public class AllowableValuesTest {
private static final String TEMPLATE_FILE = "JavaJaxRS/allowableValues.mustache";
private static final String TEMPLATE_FILE = "JavaJaxRS/jersey1_18/allowableValues.mustache";
private static final String PROVIDER_NAME = "operations";
private static String loadClassResource(Class<?> cls, String name) throws IOException {

View File

@ -49,6 +49,8 @@ public class JaxRSServerOptionsTest extends JavaClientOptionsTest {
times = 1;
clientCodegen.setFullJavaUtil(Boolean.valueOf(JaxRSServerOptionsProvider.FULL_JAVA_UTIL_VALUE));
times = 1;
clientCodegen.setDateLibrary("joda");
times = 1;
}};
}
}

View File

@ -0,0 +1,36 @@
package io.swagger.codegen.jaxrs;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.languages.JaxRSServerCodegen;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.DateProperty;
import io.swagger.models.properties.DateTimeProperty;
import io.swagger.models.properties.LongProperty;
import io.swagger.util.Json;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class JaxrsJava8ModelTest {
@Test(description = "convert a simple java model with java8 types")
public void simpleModelTest() {
final Model model = new ModelImpl()
.description("a sample model")
.property("id", new LongProperty())
.property("theDate", new DateProperty())
.property("createdAt", new DateTimeProperty())
.required("id")
.required("name");
final JaxRSServerCodegen codegen = new JaxRSServerCodegen();
codegen.setDateLibrary("java8");
codegen.processOpts();
final CodegenModel cm = codegen.fromModel("sample", model);
Json.prettyPrint(cm);
assertEquals(cm.vars.get(0).datatype, "Long");
assertEquals(cm.vars.get(1).datatype, "LocalDate");
assertEquals(cm.vars.get(2).datatype, "LocalDateTime");
}
}

View File

@ -0,0 +1,36 @@
package io.swagger.codegen.jaxrs;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.languages.JaxRSServerCodegen;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.DateProperty;
import io.swagger.models.properties.DateTimeProperty;
import io.swagger.models.properties.LongProperty;
import io.swagger.util.Json;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
public class JaxrsJodaModelTest {
@Test(description = "convert a simple java model with Joda types")
public void simpleModelTest() {
final Model model = new ModelImpl()
.description("a sample model")
.property("id", new LongProperty())
.property("theDate", new DateProperty())
.property("createdAt", new DateTimeProperty())
.required("id")
.required("name");
final JaxRSServerCodegen codegen = new JaxRSServerCodegen();
codegen.setDateLibrary("joda");
codegen.processOpts();
final CodegenModel cm = codegen.fromModel("sample", model);
Json.prettyPrint(cm);
assertEquals(cm.vars.get(0).datatype, "Long");
assertEquals(cm.vars.get(1).datatype, "LocalDate");
assertEquals(cm.vars.get(2).datatype, "DateTime");
}
}

View File

@ -1,6 +1,13 @@
package io.swagger.codegen.options;
import com.google.common.collect.ImmutableMap;
import io.swagger.codegen.languages.JaxRSServerCodegen;
import java.util.Map;
public class JaxRSServerOptionsProvider extends JavaOptionsProvider {
public static final String JODA_DATE_LIBRARY = "joda";
@Override
public boolean isServer() {
return true;
@ -10,4 +17,15 @@ public class JaxRSServerOptionsProvider extends JavaOptionsProvider {
public String getLanguage() {
return "jaxrs";
}
@Override
public Map<String, String> createOptions() {
Map<String, String> options = super.createOptions();
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.putAll(options)
.put(JaxRSServerCodegen.DATE_LIBRARY, "joda");
return builder.build();
}
}