Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support annotating additionalProperties field. #170

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 @@ -68,5 +68,9 @@ public void enumValueMethod(JMethod valueMethod) {
public boolean isAdditionalPropertiesSupported() {
return true;
}

@Override
public void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,6 @@ public interface Annotator {
* properties'.
*/
boolean isAdditionalPropertiesSupported();

void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,12 @@ public boolean isAdditionalPropertiesSupported() {
}
return true;
}

@Override
public void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName) {
for (Annotator annotator : annotators) {
annotator.additionalPropertiesField(field, clazz, propertyName);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.codehaus.jackson.annotate.JsonAnyGetter;
import org.codehaus.jackson.annotate.JsonAnySetter;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.codehaus.jackson.annotate.JsonValue;
Expand Down Expand Up @@ -93,4 +94,9 @@ public boolean isAdditionalPropertiesSupported() {
return true;
}

@Override
public void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName) {
field.annotate(JsonIgnore.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
Expand Down Expand Up @@ -93,4 +94,8 @@ public boolean isAdditionalPropertiesSupported() {
return true;
}

@Override
public void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName) {
field.annotate(JsonIgnore.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ private JFieldVar addAdditionalPropertiesField(JDefinedClass jclass, JType prope
propertiesMapImplType = propertiesMapImplType.narrow(jclass.owner().ref(String.class), propertyType.boxify());

JFieldVar field = jclass.field(JMod.PRIVATE, propertiesMapType, "additionalProperties");

ruleFactory.getAnnotator().additionalPropertiesField(field, jclass, "additionalProperties");

field.init(JExpr._new(propertiesMapImplType));

return field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;

Expand Down Expand Up @@ -146,5 +149,25 @@ public void additionalPropertiesOfBooleanTypeOnly() throws SecurityException, No
classWithNoAdditionalProperties.getMethod("setAdditionalProperty", String.class, boolean.class);

}


@Test
public void additionalPropertiesWorkWithAllVisibility() throws ClassNotFoundException, SecurityException, NoSuchMethodException, JsonProcessingException, IOException {
mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
mapper.setVisibilityChecker(mapper.getVisibilityChecker().with(Visibility.ANY));

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

Class<?> classWithAdditionalProperties = resultsClassLoader.loadClass("com.example.DefaultAdditionalProperties");
String jsonWithAdditionalProperties = "{\"a\":1, \"b\":2};";
Object instanceWithAdditionalProperties = mapper.readValue(jsonWithAdditionalProperties, classWithAdditionalProperties);

JsonNode jsonNode = mapper.readTree(mapper.writeValueAsString(instanceWithAdditionalProperties));

assertThat(jsonNode.path("a").asText(), is("1"));
assertThat(jsonNode.path("b").asInt(), is(2));
assertThat(jsonNode.has("additionalProperties"), is(false));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.JsonNode;

import org.jsonschema2pojo.Annotator;

import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
Expand Down Expand Up @@ -155,6 +157,13 @@ public boolean isAdditionalPropertiesSupported() {
return true;
}

@Override
public void additionalPropertiesField(JFieldVar field,
JDefinedClass clazz, String propertyName) {
field.annotate(Deprecated.class);

}

}

}