Skip to content
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 @@ -21,6 +21,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import org.jsonschema2pojo.Schema;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
Expand Down Expand Up @@ -103,6 +104,10 @@ public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass,

addSetter(jclass, propertyType, field);

if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
addBuilder(jclass, propertyType, field);
}

return jclass;
}

Expand Down Expand Up @@ -144,4 +149,17 @@ private JMethod addGetter(JDefinedClass jclass, JFieldVar field) {
return getter;
}

private void addBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) {
JMethod builder = jclass.method(JMod.PUBLIC, jclass, "withAdditionalProperty");

JVar nameParam = builder.param(String.class, "name");
JVar valueParam = builder.param(propertyType, "value");

JBlock body = builder.body();
JInvocation mapInvocation = body.invoke(JExpr._this().ref(field), "put");
mapInvocation.arg(nameParam);
mapInvocation.arg(valueParam);
body._return(JExpr._this());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;

import org.hamcrest.Matcher;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
Expand Down Expand Up @@ -90,7 +91,7 @@ public void additionalPropertiesAreNotDeserializableWhenDisallowed() throws Clas
@Test
public void additionalPropertiesOfStringTypeOnly() throws SecurityException, NoSuchMethodException, ClassNotFoundException {

ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesString.json", "com.example");
ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesString.json", "com.example", config("generateBuilders", true));

Class<?> classWithNoAdditionalProperties = resultsClassLoader.loadClass("com.example.AdditionalPropertiesString");
Method getter = classWithNoAdditionalProperties.getMethod("getAdditionalProperties");
Expand All @@ -99,12 +100,17 @@ public void additionalPropertiesOfStringTypeOnly() throws SecurityException, NoS

// setter with these types should exist:
classWithNoAdditionalProperties.getMethod("setAdditionalProperty", String.class, String.class);

// builder with these types should exist:
Method builderMethod = classWithNoAdditionalProperties.getMethod("withAdditionalProperty", String.class, String.class);
assertThat("the builder method returns this type", builderMethod.getReturnType(), typeEqualTo(classWithNoAdditionalProperties));

}

@Test
public void additionalPropertiesOfObjectTypeCreatesNewClassForPropertyValues() throws SecurityException, NoSuchMethodException, ClassNotFoundException {

ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesObject.json", "com.example");
ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesObject.json", "com.example", config("generateBuilders", true));

Class<?> classWithNoAdditionalProperties = resultsClassLoader.loadClass("com.example.AdditionalPropertiesObject");
Class<?> propertyValueType = resultsClassLoader.loadClass("com.example.AdditionalPropertiesObjectProperty");
Expand All @@ -116,12 +122,31 @@ public void additionalPropertiesOfObjectTypeCreatesNewClassForPropertyValues() t
// setter with these types should exist:
classWithNoAdditionalProperties.getMethod("setAdditionalProperty", String.class, propertyValueType);

// builder with these types should exist:
Method builderMethod = classWithNoAdditionalProperties.getMethod("withAdditionalProperty", String.class, propertyValueType);
assertThat("the builder method returns this type", builderMethod.getReturnType(), typeEqualTo(classWithNoAdditionalProperties));

}

@Test(expected=NoSuchMethodException.class)
public void additionalPropertiesBuilderAbsentIfNotConfigured() throws SecurityException, NoSuchMethodException, ClassNotFoundException {

ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesObject.json", "com.example");

Class<?> classWithNoAdditionalProperties = resultsClassLoader.loadClass("com.example.AdditionalPropertiesObject");
Class<?> propertyValueType = resultsClassLoader.loadClass("com.example.AdditionalPropertiesObjectProperty");

// builder with these types should not exist:
Method builderMethod = classWithNoAdditionalProperties.getMethod("withAdditionalProperty", String.class, propertyValueType);
assertThat("the builder method returns this type", builderMethod.getReturnType(), typeEqualTo(classWithNoAdditionalProperties));

fail("additional properties builder found when not requested");
}

@Test
public void additionalPropertiesOfStringArrayTypeOnly() throws SecurityException, NoSuchMethodException, ClassNotFoundException {

ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesArraysOfStrings.json", "com.example");
ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesArraysOfStrings.json", "com.example", config("generateBuilders", true));

Class<?> classWithNoAdditionalProperties = resultsClassLoader.loadClass("com.example.AdditionalPropertiesArraysOfStrings");
Method getter = classWithNoAdditionalProperties.getMethod("getAdditionalProperties");
Expand All @@ -132,13 +157,17 @@ public void additionalPropertiesOfStringArrayTypeOnly() throws SecurityException
// setter with these types should exist:
classWithNoAdditionalProperties.getMethod("setAdditionalProperty", String.class, List.class);

// builder with these types should exist:
Method builderMethod = classWithNoAdditionalProperties.getMethod("withAdditionalProperty", String.class, List.class);
assertThat("the builder method returns this type", builderMethod.getReturnType(), typeEqualTo(classWithNoAdditionalProperties));

}

@Test
public void additionalPropertiesOfBooleanTypeOnly() throws SecurityException, NoSuchMethodException, ClassNotFoundException {

ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesPrimitiveBoolean.json", "com.example",
config("usePrimitives", true));
config("usePrimitives", true, "generateBuilders", true));

Class<?> classWithNoAdditionalProperties = resultsClassLoader.loadClass("com.example.AdditionalPropertiesPrimitiveBoolean");
Method getter = classWithNoAdditionalProperties.getMethod("getAdditionalProperties");
Expand All @@ -148,9 +177,30 @@ public void additionalPropertiesOfBooleanTypeOnly() throws SecurityException, No
// setter with these types should exist:
classWithNoAdditionalProperties.getMethod("setAdditionalProperty", String.class, boolean.class);

// builder with these types should exist:
Method builderMethod = classWithNoAdditionalProperties.getMethod("withAdditionalProperty", String.class, boolean.class);
assertThat("the builder method returns this type", builderMethod.getReturnType(), typeEqualTo(classWithNoAdditionalProperties));

}



@Test
public void withAdditionalPropertyStoresValue() throws Exception {
ClassLoader resultsClassLoader = generateAndCompile("/schema/additionalProperties/additionalPropertiesString.json", "com.example", config("generateBuilders", true));

Class<?> classWithNoAdditionalProperties = resultsClassLoader.loadClass("com.example.AdditionalPropertiesString");
Method getter = classWithNoAdditionalProperties.getMethod("getAdditionalProperties");
Method builderMethod = classWithNoAdditionalProperties.getMethod("withAdditionalProperty", String.class, String.class);

Object value = "value";
Object instance = classWithNoAdditionalProperties.newInstance();
Object result = builderMethod.invoke(instance, "prop", value);
Object stored = ((Map<?, ?>)getter.invoke(instance)).get("prop");

assertThat("the builder returned the instance", result, sameInstance(instance));
assertThat("the getter returned the value", stored, sameInstance(value));

}

@Test
public void additionalPropertiesWorkWithAllVisibility() throws ClassNotFoundException, SecurityException, NoSuchMethodException, JsonProcessingException, IOException {
mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
Expand All @@ -170,4 +220,9 @@ public void additionalPropertiesWorkWithAllVisibility() throws ClassNotFoundExce
assertThat(jsonNode.has("additionalProperties"), is(false));
}

@SuppressWarnings("rawtypes")
public static Matcher<Class> typeEqualTo( Class<?> type ) {
return equalTo((Class)type);
}

}