Skip to content

Commit

Permalink
Merge pull request #469 from joelittlejohn/custom-date-time
Browse files Browse the repository at this point in the history
Added support for custom date-time, date, time classes.
  • Loading branch information
joelittlejohn committed Dec 18, 2015
2 parents 1562680 + 7d8de57 commit 508f07e
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,15 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {
private boolean includeAccessors = true;

private String targetVersion = "1.6";

private boolean includeDynamicAccessors = true;

private String dateTimeType = null;

private String timeType = null;

private String dateType = null;


/**
* Execute this task (it's expected that all relevant setters will have been
Expand Down Expand Up @@ -762,5 +769,20 @@ public String getTargetVersion() {
@Override
public boolean isIncludeDynamicAccessors() {
return includeDynamicAccessors;
}

@Override
public String getDateTimeType() {
return dateTimeType;
}

@Override
public String getDateType() {
return dateType;
}

@Override
public String getTimeType() {
return timeType;
}
}
16 changes: 16 additions & 0 deletions jsonschema2pojo-ant/src/site/Jsonschema2PojoTask.html
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,23 @@ <h3>Parameters</h3>
<td valign="top">targetVersion</td>
<td valign="top">The target version for generated source files.</td>
<td align="center" valign="top">No (default <code>1.6</code>)</td>
</tr>
<tr>
<td valign="top">dateTimeType</td>
<td valign="top">Specify class to use i.e.<code>java.time.LocalDateTime</code> instead of <code>java.util.Date</code> when adding date-time type fields to generated Java types.</td>
<td align="center" valign="top">default <code>null</code></td>
</tr>
<tr>
<td valign="top">dateType</td>
<td valign="top">Specify class to use i.e.<code>java.time.LocalDate</code> instead of <code>java.util.Date</code> when adding date-time type fields to generated Java types.</td>
<td align="center" valign="top">default <code>null</code></td>
</tr>
<tr>
<td valign="top">timeType</td>
<td valign="top">Specify class to use i.e.<code>java.time.LocalTime</code> instead of <code>java.util.Date</code> when adding date-time type fields to generated Java types.</td>
<td align="center" valign="top">default <code>null</code></td>
</tr>

</table>

<h3>Examples</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public class Arguments implements GenerationConfig {
@Parameter(names = { "-jt", "--joda-local-times" }, description = "Whether to use org.joda.time.LocalTime instead" + "of String when adding time type fields to generated Java types.")
private boolean useJodaLocalTimes = false;

@Parameter(names = { "-dtt", "--datetime-class" }, description = "Specify datetime class")
private String dateTimeType = null;

@Parameter(names = { "-tt", "--time-class" }, description = "Specify time class")
private String timeType = null;

@Parameter(names = { "-dt", "--date-class" }, description = "Specify date class")
private String dateType = null;

@Parameter(names = { "-c3", "--commons-lang3" }, description = "Whether to use commons-lang 3.x imports instead of commons-lang 2.x imports when adding equals, hashCode and toString methods.")
private boolean useCommonsLang3 = false;

Expand Down Expand Up @@ -337,4 +346,19 @@ public boolean isIncludeDynamicAccessors() {
return !disableDynamicAccessors;
}

@Override
public String getDateTimeType() {
return dateTimeType;
}

@Override
public String getDateType() {
return dateType;
}

@Override
public String getTimeType() {
return timeType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,19 @@ public String getTargetVersion() {
public boolean isIncludeDynamicAccessors() {
return true;
}

@Override
public String getDateTimeType() {
return null;
}

@Override
public String getDateType() {
return null;
}

@Override
public String getTimeType() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public interface GenerationConfig {
* methods and create public fields instead.
*/
boolean isIncludeAccessors();

/**
* Gets the 'targetVersion' configuration option
*
Expand All @@ -317,5 +317,31 @@ public interface GenerationConfig {
* or to omit these methods.
*/
boolean isIncludeDynamicAccessors();

/**
* Allow to specifiy a class for date-time jsonschema type. Could be JSR310, Joda, ...
* [org.joda.time.LocalDateTime,java.time.LocalDateTime, ...]
* java.time.* require JVM8 or greater
* @return
*/
String getDateTimeType();


/**
* Allow to specifiy a class for date-time jsonschema type. Could be JSR310, Joda, ...
* [org.joda.time.LocalDate,java.time.LocalDate, ...]
* java.time.* require JVM8 or greater
* @return
*/
String getDateType();

/**
* Allow to specifiy a class for date-time jsonschema type. Could be JSR310, Joda, ...
* [org.joda.time.LocalTime,java.time.LocalTime, ...]
* java.time.* require JVM8 or greater
* @return
*/
String getTimeType();


}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public JsonNode getContent() {
}

public JsonNode getParentContent() {
return parentContent;
}
return parentContent;
}

public boolean isGenerated() {
return javaType != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public synchronized Schema create(URI id) {
if (id.toString().contains("#")) {
JsonNode childContent = fragmentResolver.resolve(content, '#' + substringAfter(id.toString(), "#"));
schemas.put(id, new Schema(id, childContent, content));
} else {
schemas.put(id, new Schema(id, content, content));
} else {
schemas.put(id, new Schema(id, content, content));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,45 @@ else if (node.asText().equals("uuid")) {
}

private Class<?> getDateTimeType() {
String type=ruleFactory.getGenerationConfig().getDateTimeType();
if (type!=null && type.length()>0){
try {
Class<?> clazz=Class.forName(type);
return clazz;
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return ruleFactory.getGenerationConfig().isUseJodaDates() ? DateTime.class : Date.class;
}

private Class<?> getDateOnlyType() {
String type=ruleFactory.getGenerationConfig().getDateType();
if (type!=null && type.length()>0){
try {
Class<?> clazz=Class.forName(type);
return clazz;
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
return ruleFactory.getGenerationConfig().isUseJodaLocalDates() ? LocalDate.class : String.class;
}

private Class<?> getTimeOnlyType() {
String type=ruleFactory.getGenerationConfig().getTimeType();
if (type!=null && type.length()>0){
try {
Class<?> clazz=Class.forName(type);
return clazz;
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return ruleFactory.getGenerationConfig().isUseJodaLocalTimes() ? LocalTime.class : String.class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class JsonSchemaExtension implements GenerationConfig {
boolean useJodaDates
boolean useJodaLocalDates
boolean useJodaLocalTimes
String dateTimeType
String dateType
String timeType
boolean useLongIntegers
boolean usePrimitives
FileFilter fileFilter
Expand All @@ -85,6 +88,9 @@ public class JsonSchemaExtension implements GenerationConfig {
useJodaDates = false
useJodaLocalDates = false
useJodaLocalTimes = false
dateTimeType = null
dateType = null
timeType = null
useCommonsLang3 = false
parcelable = false
fileFilter = new AllFileFilter()
Expand Down Expand Up @@ -151,6 +157,9 @@ public class JsonSchemaExtension implements GenerationConfig {
|useJodaDates = ${useJodaDates}
|useJodaLocalDates = ${useJodaLocalDates}
|useJodaLocalTimes = ${useJodaLocalTimes}
|dateTimeType = ${dateTimeType}
|dateType = ${dateType}
|timeType = ${timeType}
|useCommonsLang3 = ${useCommonsLang3}
|parcelable = ${parcelable}
|initializeCollections = ${initializeCollections}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright © 2010-2014 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.integration.config;

import static org.hamcrest.Matchers.*;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.*;
import static org.junit.Assert.*;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.junit.Test;

public class CustomDatesIT {

@Test
public void defaultTypesAreNotCustom() throws ClassNotFoundException, IntrospectionException {
ClassLoader classLoader = generateAndCompile("/schema/format/formattedProperties.json", "com.example");

Class<?> classWithDate = classLoader.loadClass("com.example.FormattedProperties");

List<String[]> nonJodaTypes = Arrays.asList(
new String[] {"stringAsDateTime", "java.util.Date"},
new String[] {"stringAsDate", "java.lang.String"},
new String[] {"stringAsTime", "java.lang.String"}
);

for (String[] nonJodaType : nonJodaTypes) {
assertTypeIsExpected(classWithDate, nonJodaType[0], nonJodaType[1]);
}
}

@Test
public void dateTimeTypeCausesCustomDateTimeType() throws IntrospectionException, ClassNotFoundException {
String clazz="org.joda.time.LocalDateTime";
ClassLoader classLoader = generateAndCompile("/schema/format/formattedProperties.json", "com.example",
config("dateTimeType", clazz));
Class<?> classWithDate = classLoader.loadClass("com.example.FormattedProperties");
assertTypeIsExpected(classWithDate, "stringAsDateTime", clazz);
}

@Test
public void disablingDateTimeTypeCausesDefault() throws ClassNotFoundException, IntrospectionException {
String clazz="org.joda.time.LocalDateTime";
ClassLoader classLoader = generateAndCompile("/schema/format/formattedProperties.json", "com.example",
config("dateTimeType", null));
Class<?> classWithDate = classLoader.loadClass("com.example.FormattedProperties");
assertTypeIsExpected(classWithDate, "stringAsDateTime", "java.util.Date");
}

@Test
public void dateTypeCausesCustomDateTimeType() throws IntrospectionException, ClassNotFoundException {
String clazz="org.joda.time.LocalDate";
ClassLoader classLoader = generateAndCompile("/schema/format/formattedProperties.json", "com.example",
config("dateType", clazz));
Class<?> classWithDate = classLoader.loadClass("com.example.FormattedProperties");
assertTypeIsExpected(classWithDate, "stringAsDate", clazz);
}

@Test
public void disablingDateTypeCausesDefault() throws ClassNotFoundException, IntrospectionException {
String clazz="org.joda.time.LocalDate";
ClassLoader classLoader = generateAndCompile("/schema/format/formattedProperties.json", "com.example",
config("dateType", null));
Class<?> classWithDate = classLoader.loadClass("com.example.FormattedProperties");
assertTypeIsExpected(classWithDate, "stringAsDate", "java.lang.String");
}

@Test
public void timeTypeCausesCustomTimeType() throws IntrospectionException, ClassNotFoundException {
String clazz="org.joda.time.LocalTime";
ClassLoader classLoader = generateAndCompile("/schema/format/formattedProperties.json", "com.example",
config("timeType", clazz));
Class<?> classWithTime = classLoader.loadClass("com.example.FormattedProperties");
assertTypeIsExpected(classWithTime, "stringAsTime", clazz);
}

@Test
public void disablingTimeTypeCausesDefault() throws ClassNotFoundException, IntrospectionException {
String clazz="org.joda.time.LocalTime";
ClassLoader classLoader = generateAndCompile("/schema/format/formattedProperties.json", "com.example",
config("timeType", null));
Class<?> classWithTime = classLoader.loadClass("com.example.FormattedProperties");
assertTypeIsExpected(classWithTime, "stringAsTime", "java.lang.String");
}


private void assertTypeIsExpected(Class<?> classInstance, String propertyName, String expectedType)
throws IntrospectionException {
Method getter = new PropertyDescriptor(propertyName, classInstance).getReadMethod();
assertThat(getter.getReturnType().getName(), is(expectedType));
}

}
Loading

0 comments on commit 508f07e

Please sign in to comment.