Skip to content

Commit 11bbcb2

Browse files
author
Burak Serdar
committed
fix #775: two problems: composite metadata doesn't copy simple array element constraints, metadata parser doesn't save those constraints
1 parent 43cef32 commit 11bbcb2

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

crud/src/main/java/com/redhat/lightblue/crud/ConstraintValidator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ private void checkConstraints(JsonDoc doc, Path currentValuePath, JsonNode curre
231231
skip=currentFieldNode.getFullPath();
232232
}
233233
if(skip==null) {
234-
LOGGER.debug("checking field {}", currentFieldPath);
234+
LOGGER.debug("checking field {} for {} - {}", currentFieldPath,currentFieldNode);
235235
Error.push(currentFieldPath.toString());
236236
try {
237237
List<FieldConstraint> constraints = null;
@@ -240,6 +240,7 @@ private void checkConstraints(JsonDoc doc, Path currentValuePath, JsonNode curre
240240
} else if (currentFieldNode instanceof SimpleArrayElement) {
241241
constraints = ((SimpleArrayElement) currentFieldNode).getConstraints();
242242
}
243+
LOGGER.debug("Constraints:{}",constraints);
243244
if (constraints != null && !constraints.isEmpty()) {
244245
checkFieldConstraints(doc, constraints, currentValuePath, currentValue);
245246
}

crud/src/test/resources/crud/validator/testSimpleArrayEnum.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"entityInfo": {
3-
"name": "test",
3+
"name": "testEnum",
44
"datastore": {
55
"backend":"mongo",
6-
"collection": "test"
6+
"datasource":"mongodata",
7+
"collection":"testEnum"
78
},
89
"enums":[
910
{
@@ -13,9 +14,14 @@
1314
]
1415
},
1516
"schema": {
16-
"name": "validationTest",
17+
"access":{
18+
"insert":["anyone"],
19+
"find":["anyone"],
20+
"update":["anyone"]
21+
},
22+
"name": "testEnum",
1723
"version": {
18-
"value": "1.0",
24+
"value": "1.0.0-SNAPSHOT",
1925
"changelog": "blahblah"
2026
},
2127
"status": {

metadata/src/main/java/com/redhat/lightblue/metadata/CompositeMetadata.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ private static void copyFields(Fields dest,
445445
path.pop();
446446
} else {
447447
newElement = new SimpleArrayElement(((SimpleArrayElement) sourceEl).getType());
448+
((SimpleArrayElement)newElement).setConstraints( ((SimpleArrayElement)sourceEl).getConstraints());
449+
LOGGER.debug("Constraints:{}",((SimpleArrayElement)newElement).getConstraints());
448450
}
449451
newElement.getProperties().putAll(sourceEl.getProperties());
450452
ArrayField newField = new ArrayField(field.getName(), newElement);

metadata/src/main/java/com/redhat/lightblue/metadata/parser/MetadataParser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,8 +1489,11 @@ private void convertArrayField(ArrayField field, T fieldObject) {
14891489
putString(items, STR_TYPE, el.getType().getName());
14901490
if (el instanceof ObjectArrayElement) {
14911491
convertObjectArrayElement((ObjectArrayElement) el, items);
1492+
} else {
1493+
convertFieldConstraints(items, ((SimpleArrayElement)el).getConstraints());
14921494
}
1493-
}
1495+
convertProperties(field, fieldObject);
1496+
}
14941497

14951498
private void convertReferenceField(ReferenceField field, T fieldObject) {
14961499
putString(fieldObject, STR_ENTITY, field.getEntityName());

metadata/src/test/java/com/redhat/lightblue/metadata/parser/JSONMetadataParserTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import com.redhat.lightblue.metadata.Enums;
5454
import com.redhat.lightblue.metadata.MetadataConstants;
5555
import com.redhat.lightblue.metadata.SimpleField;
56+
import com.redhat.lightblue.metadata.SimpleArrayElement;
57+
import com.redhat.lightblue.metadata.ArrayField;
5658
import com.redhat.lightblue.metadata.ValueGenerator;
5759
import com.redhat.lightblue.metadata.EntitySchema;
5860
import com.redhat.lightblue.metadata.Fields;
@@ -630,6 +632,22 @@ public void testParseConvertValueGenerator() throws IOException {
630632
Assert.assertEquals("1000", props.get("initialValue").asText());
631633
}
632634

635+
@Test
636+
public void testParseConvertArrayConstraints() throws IOException {
637+
JsonNode object = loadJsonNode("JSONMetadataParserTest-array.json");
638+
EntitySchema em = parser.parseEntitySchema(object);
639+
ArrayField field = (ArrayField) em.resolve(new Path("name"));
640+
Assert.assertNotNull(field);
641+
Assert.assertEquals(1,((SimpleArrayElement)field.getElement()).getConstraints().size());
642+
643+
Fields fields=new Fields(null);
644+
fields.put(field);
645+
646+
ObjectNode obj=(ObjectNode)parser.convert(fields);
647+
System.out.println("array:"+obj);
648+
Assert.assertTrue(obj.get("name").get("items").get("constraints").get("required").booleanValue());
649+
}
650+
633651
@Test
634652
public void testProperties() throws IOException {
635653
JsonNode object = loadJsonNode("JSONMetadataParserTest-properties.json");
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "test",
3+
"version": {
4+
"value": "1.0.0",
5+
"changelog": "Initial version"
6+
},
7+
"status": {
8+
"value": "active"
9+
},
10+
"access": {
11+
"insert": ["admin"],
12+
"find": ["admin", "all"],
13+
"update": ["admin"],
14+
"delete": ["admin"]
15+
},
16+
"datastore": {
17+
"backend": "empty"
18+
},
19+
"fields": {
20+
"name": {
21+
"type": "array",
22+
"items": {
23+
"type":"string",
24+
"constraints": {
25+
"required":true
26+
}
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)