Skip to content

Commit

Permalink
(#706) : Default value for property of "integer enum" type caused POJ…
Browse files Browse the repository at this point in the history
…O compilation error
  • Loading branch information
EE\romanso authored and s13o committed Mar 20, 2017
1 parent fc5fe25 commit f673fc5
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;

import java.math.BigDecimal;
Expand All @@ -38,11 +40,12 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import static org.apache.commons.lang3.StringUtils.*;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.jsonschema2pojo.Schema;
import org.jsonschema2pojo.exception.GenerationException;

/**
* Applies the "enum" schema rule.
Expand Down Expand Up @@ -152,7 +155,7 @@ private JExpression getDefaultValue(JType fieldType, JsonNode node) {

} else if (fieldType instanceof JDefinedClass && ((JDefinedClass) fieldType).getClassType().equals(ClassType.ENUM)) {

return getDefaultEnum(fieldType, node);
return getDefaultEnum((JDefinedClass)fieldType, node);

} else {
return JExpr._null();
Expand Down Expand Up @@ -243,11 +246,25 @@ private JExpression getDefaultSet(JType fieldType, JsonNode node) {

}

private JExpression getDefaultEnum(JType fieldType, JsonNode node) {
private boolean isIntegerEnumValue(JDefinedClass fieldType){

JInvocation invokeFromValue = ((JClass) fieldType).staticInvoke("fromValue");
invokeFromValue.arg(node.asText());
for(JMethod method : fieldType.methods()){
if (EnumRule.FROM_VALUE_METHOD_NAME.equals(method.name())
&& (method.mods().getValue() & JMod.PUBLIC) == JMod.PUBLIC
&& (method.mods().getValue() & JMod.STATIC) == JMod.STATIC ){
final JType type = method.listParamTypes()[0];
return type.unboxify() == type.owner().INT;
}
}
throw new GenerationException("Factory method '" + EnumRule.FROM_VALUE_METHOD_NAME + "' is not found in "+ fieldType);

}

private JExpression getDefaultEnum(JDefinedClass fieldType, JsonNode node) {

JInvocation invokeFromValue = fieldType.staticInvoke(EnumRule.FROM_VALUE_METHOD_NAME);
invokeFromValue.arg(isIntegerEnumValue(fieldType) ?
JExpr.lit(node.asInt()) : JExpr.lit(node.asText()));
return invokeFromValue;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
public class EnumRule implements Rule<JClassContainer, JType> {

private static final String VALUE_FIELD_NAME = "value";
public static final String FROM_VALUE_METHOD_NAME = "fromValue";

private final RuleFactory ruleFactory;

Expand Down Expand Up @@ -166,8 +167,8 @@ private JDefinedClass createEnum(JsonNode node, String nodeName, JClassContainer
private void addFactoryMethod(JDefinedClass _enum, JType backingType) {
JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType);

JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
JVar valueParam = fromValue.param(backingType, "value");
JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, FROM_VALUE_METHOD_NAME);
JVar valueParam = fromValue.param(backingType, VALUE_FIELD_NAME);

JBlock body = fromValue.body();
JVar constant = body.decl(_enum, "constant");
Expand Down Expand Up @@ -200,7 +201,7 @@ private JFieldVar addQuickLookupMap(JDefinedClass _enum, JType backingType) {

JForEach forEach = _enum.init().forEach(_enum, "c", JExpr.invoke("values"));
JInvocation put = forEach.body().invoke(lookupMap, "put");
put.arg(forEach.var().ref("value"));
put.arg(forEach.var().ref(VALUE_FIELD_NAME));
put.arg(forEach.var());

return lookupMap;
Expand Down Expand Up @@ -232,7 +233,7 @@ private void addToString(JDefinedClass _enum, JFieldVar valueField) {
}

private void addValueMethod(JDefinedClass _enum, JFieldVar valueField) {
JMethod fromValue = _enum.method(JMod.PUBLIC, valueField.type(), "value");
JMethod fromValue = _enum.method(JMod.PUBLIC, valueField.type(), VALUE_FIELD_NAME);

JBlock body = fromValue.body();
body._return(JExpr._this().ref(valueField));
Expand Down
4 changes: 2 additions & 2 deletions jsonschema2pojo-gradle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<plugin>
<groupId>com.bluetrainsoftware.maven</groupId>
<artifactId>groovydoc-maven-plugin</artifactId>
<version>1.2</version>
<version>1.3</version>
<executions>
<execution>
<id>attach-docs</id>
Expand All @@ -90,7 +90,7 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<version>2.19.1</version>
<configuration>
<useFile>false</useFile>
<includes>
Expand Down
38 changes: 36 additions & 2 deletions jsonschema2pojo-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
<!-- switch off surefire, there's no unit tests in this module -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.7.2</version>
<version>2.19.1</version>
<executions>
<execution>
<id>integration-test</id>
Expand Down Expand Up @@ -147,6 +147,40 @@
<groupId>org.robolectric</groupId>
<artifactId>robolectric</artifactId>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-base</artifactId>
<classifier>real</classifier>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-kxml2</artifactId>
<classifier>real</classifier>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-policy</artifactId>
<classifier>real</classifier>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-luni</artifactId>
<classifier>real</classifier>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-res</artifactId>
<classifier>real</classifier>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.ccil.cowan.tagsoup</groupId>
<artifactId>tagsoup</artifactId>
</dependency>
<dependency>
<groupId>qdox</groupId>
<artifactId>qdox</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.hamcrest.core.IsInstanceOf;
import org.jsonschema2pojo.integration.util.Jsonschema2PojoRule;
import org.jsonschema2pojo.rules.EnumRule;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void enumContainsWorkingAnnotatedSerializationMethod() throws NoSuchMetho
@Test
public void enumContainsWorkingAnnotatedDeserializationMethod() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

Method fromValue = enumClass.getMethod("fromValue", String.class);
Method fromValue = enumClass.getMethod(EnumRule.FROM_VALUE_METHOD_NAME, String.class);

assertThat((Enum) fromValue.invoke(enumClass, "one"), is(sameInstance(enumClass.getEnumConstants()[0])));
assertThat((Enum) fromValue.invoke(enumClass, "secondOne"), is(sameInstance(enumClass.getEnumConstants()[1])));
Expand All @@ -108,7 +109,7 @@ public void enumContainsWorkingAnnotatedDeserializationMethod() throws NoSuchMet
@Test
public void enumDeserializationMethodRejectsInvalidValues() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

Method fromValue = enumClass.getMethod("fromValue", String.class);
Method fromValue = enumClass.getMethod(EnumRule.FROM_VALUE_METHOD_NAME, String.class);

try {
fromValue.invoke(enumClass, "something invalid");
Expand Down Expand Up @@ -141,7 +142,7 @@ public void intEnumAtRootCreatesIntBackedEnum() throws ClassNotFoundException, N
Class<Enum> rootEnumClass = (Class<Enum>) resultsClassLoader.loadClass("com.example.enums.IntegerEnumAsRoot");

assertThat(rootEnumClass.isEnum(), is(true));
assertThat(rootEnumClass.getDeclaredMethod("fromValue", Integer.class), is(notNullValue()));
assertThat(rootEnumClass.getDeclaredMethod(EnumRule.FROM_VALUE_METHOD_NAME, Integer.class), is(notNullValue()));
assertThat(isPublic(rootEnumClass.getModifiers()), is(true));
}

Expand All @@ -154,7 +155,7 @@ public void doubleEnumAtRootCreatesIntBackedEnum() throws ClassNotFoundException
Class<Enum> rootEnumClass = (Class<Enum>) resultsClassLoader.loadClass("com.example.enums.DoubleEnumAsRoot");

assertThat(rootEnumClass.isEnum(), is(true));
assertThat(rootEnumClass.getDeclaredMethod("fromValue", Double.class), is(notNullValue()));
assertThat(rootEnumClass.getDeclaredMethod(EnumRule.FROM_VALUE_METHOD_NAME, Double.class), is(notNullValue()));
assertThat(isPublic(rootEnumClass.getModifiers()), is(true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"javaType" : "com.example.enums.IntegerEnumAsRoot",
"type" : "integer",
"enum" : [1, 2, 3],
"javaEnumNames" : ["One", "Two", "Three"]
"javaEnumNames" : ["One", "Two", "Three"],
"default": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"testEnum" : {
"type" : "integer",
"enum" : [1, 2, 3],
"javaEnumNames" : ["One", "Two", "Three"]
"javaEnumNames" : ["One", "Two", "Three"],
"default": 3
}
}
}
46 changes: 46 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,52 @@
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-base</artifactId>
<version>4.1.2_r1_rc</version>
<classifier>real</classifier>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-kxml2</artifactId>
<version>4.1.2_r1_rc</version>
<classifier>real</classifier>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-policy</artifactId>
<version>4.1.2_r1_rc</version>
<classifier>real</classifier>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-luni</artifactId>
<version>4.1.2_r1_rc</version>
<classifier>real</classifier>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>android-res</artifactId>
<version>4.1.2_r1_rc</version>
<classifier>real</classifier>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ccil.cowan.tagsoup</groupId>
<artifactId>tagsoup</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
Expand Down

0 comments on commit f673fc5

Please sign in to comment.