Skip to content

Commit 36ad1d3

Browse files
author
Marek Paterczyk
committed
Allow referenced fields to be set to null and only null
1 parent 800a639 commit 36ad1d3

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

crud/src/main/java/com/redhat/lightblue/eval/SetExpressionEvaluator.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
import org.slf4j.LoggerFactory;
2727

2828
import com.fasterxml.jackson.databind.JsonNode;
29+
import com.fasterxml.jackson.databind.node.ArrayNode;
2930
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
3031
import com.fasterxml.jackson.databind.node.ObjectNode;
31-
import com.fasterxml.jackson.databind.node.ArrayNode;
3232
import com.redhat.lightblue.crud.CrudConstants;
3333
import com.redhat.lightblue.metadata.ArrayField;
3434
import com.redhat.lightblue.metadata.FieldTreeNode;
3535
import com.redhat.lightblue.metadata.ObjectArrayElement;
3636
import com.redhat.lightblue.metadata.ObjectField;
37+
import com.redhat.lightblue.metadata.ReferenceField;
3738
import com.redhat.lightblue.metadata.SimpleArrayElement;
3839
import com.redhat.lightblue.metadata.SimpleField;
3940
import com.redhat.lightblue.metadata.Type;
@@ -134,14 +135,23 @@ public SetExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, Se
134135

135136
if (mdNode instanceof SimpleField || mdNode instanceof SimpleArrayElement) {
136137
data = initializeSimple(rvalue, refMdNode, mdNode, field, refPath);
138+
setValues.add(data);
139+
} else if (mdNode instanceof ReferenceField) {
140+
if (rvalue.getValue() != null) {
141+
throw new EvaluationError(CrudConstants.ERR_ASSIGNMENT + field +" can't assign values to referenced fields");
142+
} else {
143+
// ignore if null
144+
}
137145
} else if (mdNode instanceof ObjectField || mdNode instanceof ObjectArrayElement) {
138146
data = initializeObject(rvalue, refMdNode, mdNode, field, refPath);
147+
setValues.add(data);
139148
} else if (mdNode instanceof ArrayField) {
140149
data = initializeArray(rvalue, refMdNode, mdNode, field, refPath);
141-
}
142-
if (data != null) {
143150
setValues.add(data);
151+
} else {
152+
throw new EvaluationError(CrudConstants.ERR_ASSIGNMENT + field + " field type "+mdNode.getClass()+" is not recognized!");
144153
}
154+
145155
}
146156
}
147157

crud/src/test/java/com/redhat/lightblue/eval/SetExpressionEvaluatorTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,20 @@ public void assign_arr_to_field() throws Exception {
100100
Assert.assertEquals("[1,2]", jsonDoc.get(new Path("field6.nf10")).toString());
101101

102102
}
103+
104+
@Test
105+
public void setting_reference_to_null_is_ignored() throws Exception {
106+
UpdateExpression expr = EvalTestContext.updateExpressionFromJson("{'$set': {'ref': null } }");
107+
Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr);
108+
Assert.assertFalse(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path()));
109+
110+
}
111+
112+
@Test(expected=EvaluationError.class)
113+
public void setting_reference_to_non_null_throws_exception() throws Exception {
114+
UpdateExpression expr = EvalTestContext.updateExpressionFromJson("{'$set': {'ref': 'foo' } }");
115+
Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr);
116+
Assert.assertFalse(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path()));
117+
118+
}
103119
}

crud/src/test/resources/testMetadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@
270270
}
271271
}
272272
}
273+
},
274+
"ref" : {
275+
"type":"reference",
276+
"entity":"referenced_entity",
277+
"query": { "field":"field1","op":"=","rfield":"$parent.field1"}
273278
}
274279
}
275280
}

0 commit comments

Comments
 (0)