Skip to content

Commit

Permalink
Add JsonNode parent to rules.
Browse files Browse the repository at this point in the history
This is required in order to implement context-aware rules, e.g. such as joelittlejohngh-910.
  • Loading branch information
jrehwaldt committed Sep 17, 2018
1 parent 2424bcd commit 9e1de34
Show file tree
Hide file tree
Showing 41 changed files with 177 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public JType generate(JCodeModel codeModel, String className, String packageName

ObjectNode schemaNode = readSchema(schemaUrl);

return ruleFactory.getSchemaRule().apply(className, schemaNode, jpackage, new Schema(null, schemaNode, schemaNode));
return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, schemaNode));

}

Expand Down Expand Up @@ -117,7 +117,7 @@ public JType generate(JCodeModel codeModel, String className, String packageName

JsonNode schemaNode = objectMapper().readTree(json);

return ruleFactory.getSchemaRule().apply(className, schemaNode, jpackage,
return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage,
new Schema(schemaLocation, schemaNode, schemaNode));
}

Expand All @@ -133,7 +133,7 @@ public JType generate(JCodeModel codeModel, String className, String packageName
schemaNode = objectMapper().readTree(json);
}

return ruleFactory.getSchemaRule().apply(className, schemaNode, jpackage, new Schema(null, schemaNode, schemaNode));
return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, schemaNode));
}

private ObjectMapper objectMapper() {
Expand All @@ -142,4 +142,4 @@ private ObjectMapper objectMapper() {
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected AdditionalPropertiesRule(RuleFactory ruleFactory) {
* @return the given Java type jclass
*/
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
public JDefinedClass apply(String nodeName, JsonNode node, JsonNode parent, JDefinedClass jclass, Schema schema) {

if (node != null && node.isBoolean() && node.asBoolean() == false) {
// no additional properties allowed
Expand All @@ -100,7 +100,7 @@ public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass,

JType propertyType;
if (node != null && node.size() != 0) {
propertyType = ruleFactory.getSchemaRule().apply(nodeName + "Property", node, jclass, schema);
propertyType = ruleFactory.getSchemaRule().apply(nodeName + "Property", node, parent, jclass, schema);
} else {
propertyType = jclass.owner().ref(Object.class);
}
Expand All @@ -112,7 +112,7 @@ public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass,
addSetter(jclass, propertyType, field);

if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()) {
ruleFactory.getValidRule().apply(nodeName, node, field, schema);
ruleFactory.getValidRule().apply(nodeName, node, parent, field, schema);
}

if (ruleFactory.getGenerationConfig().isGenerateBuilders()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,22 @@ protected ArrayRule(RuleFactory ruleFactory) {
* the name of the property which has type "array"
* @param node
* the schema "type" node
* @param parent
* the parent node
* @param jpackage
* the package into which newly generated types should be added
* @return the Java type associated with this array rule, either {@link Set}
* or {@link List}, narrowed by the "items" type
*/
@Override
public JClass apply(String nodeName, JsonNode node, JPackage jpackage, Schema schema) {
public JClass apply(String nodeName, JsonNode node, JsonNode parent, JPackage jpackage, Schema schema) {

boolean uniqueItems = node.has("uniqueItems") && node.get("uniqueItems").asBoolean();
boolean rootSchemaIsArray = !schema.isGenerated();

JType itemType;
if (node.has("items")) {
itemType = ruleFactory.getSchemaRule().apply(makeSingular(nodeName), node.get("items"), jpackage, schema);
itemType = ruleFactory.getSchemaRule().apply(makeSingular(nodeName), node.get("items"), node, jpackage, schema);
} else {
itemType = jpackage.owner().ref(Object.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JType;
import scala.annotation.meta.field;

/**
* Applies the "default" schema rule.
Expand Down Expand Up @@ -81,7 +82,7 @@ public DefaultRule(RuleFactory ruleFactory) {
* @return field, which will have an init expression is appropriate
*/
@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {

boolean defaultPresent = node != null && isNotEmpty(node.asText());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ protected DescriptionRule() {
* the name of the object to which this description applies
* @param node
* the "description" schema node
* @param parent
* the parent node
* @param generatableType
* comment-able code generation construct, usually a java class,
* which should have this description applied
* @return the JavaDoc comment created to contain the description
*/
@Override
public JDocComment apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) {
public JDocComment apply(String nodeName, JsonNode node, JsonNode parent, JDocCommentable generatableType, Schema schema) {
JDocComment javadoc = generatableType.javadoc();

javadoc.append(node.asText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ public DynamicPropertiesRule(RuleFactory ruleFactory) {
* @param node
* the properties node, containing property names and their
* definition
* @param parent
* the parent node
* @param jclass
* the Java type which will have the given properties added
* @param currentSchema
* the schema being implemented
* @return the given jclass
*/
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema currentSchema) {
public JDefinedClass apply(String nodeName, JsonNode node, JsonNode parent, JDefinedClass jclass, Schema currentSchema) {
if (!ruleFactory.getGenerationConfig().isIncludeDynamicAccessors() ||
(!ruleFactory.getGenerationConfig().isIncludeDynamicSetters() &&
!ruleFactory.getGenerationConfig().isIncludeDynamicGetters() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ protected EnumRule(RuleFactory ruleFactory) {
* the name of the property which is an "enum"
* @param node
* the enum node
* @param parent
* the parent node
* @param container
* the class container (class or package) to which this enum
* should be added
* @return the newly generated Java type that was created to represent the
* given enum
*/
@Override
public JType apply(String nodeName, JsonNode node, JClassContainer container, Schema schema) {
public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer container, Schema schema) {

JDefinedClass _enum;
try {
Expand All @@ -117,22 +119,22 @@ public JType apply(String nodeName, JsonNode node, JClassContainer container, Sc
// If type is specified on the enum, get a type rule for it. Otherwise, we're a string.
// (This is different from the default of Object, which is why we don't do this for every case.)
JType backingType = node.has("type") ?
ruleFactory.getTypeRule().apply(nodeName, typeNode, container, schema) :
ruleFactory.getTypeRule().apply(nodeName, typeNode, parent, container, schema) :
container.owner().ref(String.class);

JFieldVar valueField = addValueField(_enum, backingType);
JFieldVar valueField = addValueField(_enum, backingType);

// override toString only if we have a sensible string to return
if(isString(backingType)){
addToString(_enum, valueField);
}
// override toString only if we have a sensible string to return
if(isString(backingType)){
addToString(_enum, valueField);
}

addValueMethod(_enum, valueField);
addValueMethod(_enum, valueField);

addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType);
addFactoryMethod(_enum, backingType);
addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType);
addFactoryMethod(_enum, backingType);

return _enum;
return _enum;
}

private JDefinedClass createEnum(JsonNode node, String nodeName, JClassContainer container) throws ClassAlreadyExistsException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.apache.commons.lang.StringUtils.*;

import java.net.URI;
import java.net.URL;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -78,14 +79,16 @@ protected FormatRule(RuleFactory ruleFactory) {
* the name of the node to which this format is applied
* @param node
* the format node
* @param parent
* the parent node
* @param baseType
* the type which which is being formatted e.g. for
* <code>{ "type" : "string", "format" : "uri" }</code> the
* baseType would be java.lang.String
* @return the Java type that is appropriate for the format value
*/
@Override
public JType apply(String nodeName, JsonNode node, JType baseType, Schema schema) {
public JType apply(String nodeName, JsonNode node, JsonNode parent, JType baseType, Schema schema) {

if (node.asText().equals("date-time")) {
return baseType.owner().ref(getDateTimeType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class JavaNameRule implements Rule<JDocCommentable, JDocComment> {

@Override
public JDocComment apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema currentSchema) {
public JDocComment apply(String nodeName, JsonNode node, JsonNode parent, JDocCommentable generatableType, Schema currentSchema) {
JDocComment javaDoc = generatableType.javadoc();

javaDoc.append(String.format("%nCorresponds to the \"%s\" property.", nodeName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ protected MediaRule() {
* the name of the property.
* @param mediaNode
* the media node
* @param parent
* the parent node
* @param baseType
* the type with the media node. This must be java.lang.String.
* @param schema
Expand All @@ -63,7 +65,7 @@ protected MediaRule() {
* @since 0.4.2
*/
@Override
public JType apply(String nodeName, JsonNode mediaNode, JType baseType, Schema schema) {
public JType apply(String nodeName, JsonNode mediaNode, JsonNode parent, JType baseType, Schema schema) {
if (!mediaNode.has(BINARY_ENCODING)) {
return baseType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jsonschema2pojo.Schema;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JFieldVar;
import scala.annotation.meta.field;

public class MinItemsMaxItemsRule implements Rule<JFieldVar, JFieldVar> {

Expand All @@ -32,7 +33,7 @@ protected MinItemsMaxItemsRule(RuleFactory ruleFactory) {
}

@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {

if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()
&& (node.has("minItems") || node.has("maxItems"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jsonschema2pojo.Schema;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JFieldVar;
import scala.annotation.meta.field;

public class MinLengthMaxLengthRule implements Rule<JFieldVar, JFieldVar> {

Expand All @@ -32,7 +33,7 @@ protected MinLengthMaxLengthRule(RuleFactory ruleFactory) {
}

@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {

if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()
&& (node.has("minLength") || node.has("maxLength"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jsonschema2pojo.Schema;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JFieldVar;
import scala.annotation.meta.field;

public class MinimumMaximumRule implements Rule<JFieldVar, JFieldVar> {

Expand All @@ -33,7 +34,7 @@ protected MinimumMaximumRule(RuleFactory ruleFactory) {
}

@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {

if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ protected NotRequiredRule(RuleFactory ruleFactory) {
* @param node
* the "not required" node, having a value <code>false</code> or
* <code>no value</code>
* @param parent
* the parent node
* @param generatableType
* the class or method which may be marked as "not required"
* @return the JavaDoc comment attached to the generatableType, which
* <em>may</em> have an added not to mark this construct as
* not required.
*/
@Override
public JDocCommentable apply(String nodeName, JsonNode node, JDocCommentable generatableType, Schema schema) {
public JDocCommentable apply(String nodeName, JsonNode node, JsonNode parent, JDocCommentable generatableType, Schema schema) {

// Since NotRequiredRule is executed for all fields that do not have "required" present,
// we need to recognize whether the field is part of the RequiredArrayRule.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected ObjectRule(RuleFactory ruleFactory, ParcelableHelper parcelableHelper)
* characteristics. See other implementers of {@link Rule} for details.
*/
@Override
public JType apply(String nodeName, JsonNode node, JPackage _package, Schema schema) {
public JType apply(String nodeName, JsonNode node, JsonNode parent, JPackage _package, Schema schema) {

JType superType = getSuperType(nodeName, node, _package, schema);

Expand All @@ -113,25 +113,25 @@ public JType apply(String nodeName, JsonNode node, JPackage _package, Schema sch
}

if (node.has("title")) {
ruleFactory.getTitleRule().apply(nodeName, node.get("title"), jclass, schema);
ruleFactory.getTitleRule().apply(nodeName, node.get("title"), node, jclass, schema);
}

if (node.has("description")) {
ruleFactory.getDescriptionRule().apply(nodeName, node.get("description"), jclass, schema);
ruleFactory.getDescriptionRule().apply(nodeName, node.get("description"), node, jclass, schema);
}

ruleFactory.getPropertiesRule().apply(nodeName, node.get("properties"), jclass, schema);
ruleFactory.getPropertiesRule().apply(nodeName, node.get("properties"), node, jclass, schema);

if (node.has("javaInterfaces")) {
addInterfaces(jclass, node.get("javaInterfaces"));
}

ruleFactory.getAdditionalPropertiesRule().apply(nodeName, node.get("additionalProperties"), jclass, schema);
ruleFactory.getAdditionalPropertiesRule().apply(nodeName, node.get("additionalProperties"), node, jclass, schema);

ruleFactory.getDynamicPropertiesRule().apply(nodeName, node.get("properties"), jclass, schema);
ruleFactory.getDynamicPropertiesRule().apply(nodeName, node.get("properties"), node, jclass, schema);

if (node.has("required")) {
ruleFactory.getRequiredArrayRule().apply(nodeName, node.get("required"), jclass, schema);
ruleFactory.getRequiredArrayRule().apply(nodeName, node.get("required"), node, jclass, schema);
}

if (ruleFactory.getGenerationConfig().isIncludeToString()) {
Expand Down Expand Up @@ -333,7 +333,7 @@ private JType getSuperType(String nodeName, JsonNode node, JPackage jPackage, Sc
JType superType = jPackage.owner().ref(Object.class);
Schema superTypeSchema = getSuperSchema(node, schema, false);
if (superTypeSchema != null) {
superType = ruleFactory.getSchemaRule().apply(nodeName + "Parent", node.get("extends"), jPackage, superTypeSchema);
superType = ruleFactory.getSchemaRule().apply(nodeName + "Parent", node.get("extends"), node, jPackage, superTypeSchema);
} else if (node.has("extendsJavaClass")) {
superType = resolveType(jPackage, node.get("extendsJavaClass").asText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jsonschema2pojo.Schema;
import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JFieldVar;
import scala.annotation.meta.field;

public class PatternRule implements Rule<JFieldVar, JFieldVar> {

Expand All @@ -32,7 +33,7 @@ public PatternRule(RuleFactory ruleFactory) {
}

@Override
public JFieldVar apply(String nodeName, JsonNode node, JFieldVar field, Schema currentSchema) {
public JFieldVar apply(String nodeName, JsonNode node, JsonNode parent, JFieldVar field, Schema currentSchema) {

if (ruleFactory.getGenerationConfig().isIncludeJsr303Annotations()) {
JAnnotationUse annotation = field.annotate(Pattern.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ protected PropertiesRule(RuleFactory ruleFactory) {
* @return the given jclass
*/
@Override
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
public JDefinedClass apply(String nodeName, JsonNode node, JsonNode parent, JDefinedClass jclass, Schema schema) {
if (node == null) {
node = JsonNodeFactory.instance.objectNode();
}

for (Iterator<String> properties = node.fieldNames(); properties.hasNext(); ) {
String property = properties.next();

ruleFactory.getPropertyRule().apply(property, node.get(property), jclass, schema);
ruleFactory.getPropertyRule().apply(property, node.get(property), node, jclass, schema);
}

if (ruleFactory.getGenerationConfig().isGenerateBuilders() && !jclass._extends().name().equals("Object")) {
Expand Down
Loading

0 comments on commit 9e1de34

Please sign in to comment.