Skip to content

Commit

Permalink
Allow referenced fields to be set to null and only null
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Paterczyk committed Jun 7, 2017
1 parent 800a639 commit 36ad1d3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.redhat.lightblue.crud.CrudConstants;
import com.redhat.lightblue.metadata.ArrayField;
import com.redhat.lightblue.metadata.FieldTreeNode;
import com.redhat.lightblue.metadata.ObjectArrayElement;
import com.redhat.lightblue.metadata.ObjectField;
import com.redhat.lightblue.metadata.ReferenceField;
import com.redhat.lightblue.metadata.SimpleArrayElement;
import com.redhat.lightblue.metadata.SimpleField;
import com.redhat.lightblue.metadata.Type;
Expand Down Expand Up @@ -134,14 +135,23 @@ public SetExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, Se

if (mdNode instanceof SimpleField || mdNode instanceof SimpleArrayElement) {
data = initializeSimple(rvalue, refMdNode, mdNode, field, refPath);
setValues.add(data);
} else if (mdNode instanceof ReferenceField) {
if (rvalue.getValue() != null) {
throw new EvaluationError(CrudConstants.ERR_ASSIGNMENT + field +" can't assign values to referenced fields");
} else {
// ignore if null
}
} else if (mdNode instanceof ObjectField || mdNode instanceof ObjectArrayElement) {
data = initializeObject(rvalue, refMdNode, mdNode, field, refPath);
setValues.add(data);
} else if (mdNode instanceof ArrayField) {
data = initializeArray(rvalue, refMdNode, mdNode, field, refPath);
}
if (data != null) {
setValues.add(data);
} else {
throw new EvaluationError(CrudConstants.ERR_ASSIGNMENT + field + " field type "+mdNode.getClass()+" is not recognized!");
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,20 @@ public void assign_arr_to_field() throws Exception {
Assert.assertEquals("[1,2]", jsonDoc.get(new Path("field6.nf10")).toString());

}

@Test
public void setting_reference_to_null_is_ignored() throws Exception {
UpdateExpression expr = EvalTestContext.updateExpressionFromJson("{'$set': {'ref': null } }");
Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr);
Assert.assertFalse(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path()));

}

@Test(expected=EvaluationError.class)
public void setting_reference_to_non_null_throws_exception() throws Exception {
UpdateExpression expr = EvalTestContext.updateExpressionFromJson("{'$set': {'ref': 'foo' } }");
Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr);
Assert.assertFalse(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path()));

}
}
5 changes: 5 additions & 0 deletions crud/src/test/resources/testMetadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@
}
}
}
},
"ref" : {
"type":"reference",
"entity":"referenced_entity",
"query": { "field":"field1","op":"=","rfield":"$parent.field1"}
}
}
}
Expand Down

0 comments on commit 36ad1d3

Please sign in to comment.