Skip to content

Commit 3c398d1

Browse files
authored
fixed properties with reserved keys (#22)
1 parent fcbb68a commit 3c398d1

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src/main/java/com/arangodb/tinkerpop/gremlin/persistence/PropertiesContainer.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public abstract class PropertiesContainer<V> {
2424
private final Map<String, V> properties = new HashMap<>();
2525

2626
public Map<String, V> getProperties() {
27-
return properties;
27+
return Collections.unmodifiableMap(properties);
2828
}
2929

3030
public Set<String> keySet() {
31-
return properties.keySet();
31+
return Collections.unmodifiableSet(properties.keySet());
3232
}
3333

3434
public Collection<V> values() {
35-
return properties.values();
35+
return Collections.unmodifiableCollection(properties.values());
3636
}
3737

3838
public V get(String key) {
@@ -47,12 +47,6 @@ public void put(String key, V value) {
4747
properties.put(key, value);
4848
}
4949

50-
public void putAll(Map<String, V> map) {
51-
if (map != null) {
52-
properties.putAll(map);
53-
}
54-
}
55-
5650
public void remove(String key) {
5751
properties.remove(key);
5852
}

src/main/java/com/arangodb/tinkerpop/gremlin/persistence/serde/VertexDataDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public VertexData deserialize(JsonParser p, DeserializationContext ctx) throws I
6262
if (!config.isReservedField(prop.getKey())) {
6363
VertexPropertyData pd = new VertexPropertyData(c.treeToValue(prop.getValue(), Object.class));
6464
String key = prop.getKey();
65-
pd.putAll(meta.get(key));
65+
meta.getOrDefault(key, Collections.emptyMap()).forEach(pd::put);
6666
data.put(key, pd);
6767
}
6868
}

src/main/java/com/arangodb/tinkerpop/gremlin/structure/ArangoDBVertex.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.arangodb.tinkerpop.gremlin.persistence.VertexData;
2020
import com.arangodb.tinkerpop.gremlin.persistence.VertexPropertyData;
21+
import com.arangodb.tinkerpop.gremlin.utils.ArangoDBUtil;
2122
import org.apache.tinkerpop.gremlin.structure.*;
2223
import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
2324
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
@@ -56,6 +57,7 @@ public <V> VertexProperty<V> property(
5657
}
5758

5859
VertexPropertyData prop = new VertexPropertyData(value);
60+
ArangoDBUtil.validateProperty(key, value, graph.config);
5961
data.put(key, prop);
6062
doUpdate();
6163

src/test/java/com/arangodb/tinkerpop/gremlin/arangodb/simple/SimplePersistenceTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import static com.arangodb.tinkerpop.gremlin.structure.ArangoDBGraph.GRAPH_VARIABLES_COLLECTION;
3333
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.assertj.core.api.Assertions.catchThrowable;
3435

3536
@SuppressWarnings("resource")
3637
public class SimplePersistenceTest extends AbstractGremlinTest {
@@ -92,6 +93,45 @@ public void vertices() {
9293
.containsEntry("meta", "metaValue");
9394
}
9495

96+
@Test
97+
@SuppressWarnings("unchecked")
98+
public void settingKeyAsPropertyShouldFail() {
99+
Vertex v = graph.addVertex(
100+
T.id, "foo",
101+
T.label, "bar"
102+
);
103+
Throwable thrown = catchThrowable(() -> v.property("_key", "test"));
104+
assertThat(thrown)
105+
.isInstanceOf(IllegalArgumentException.class)
106+
.hasMessageContaining("Property key can not be a reserved key");
107+
}
108+
109+
@Test
110+
@SuppressWarnings("unchecked")
111+
public void settingIdAsPropertyShouldFail() {
112+
Vertex v = graph.addVertex(
113+
T.id, "foo",
114+
T.label, "bar"
115+
);
116+
Throwable thrown = catchThrowable(() -> v.property("_id", "test"));
117+
assertThat(thrown)
118+
.isInstanceOf(IllegalArgumentException.class)
119+
.hasMessageContaining("Property key can not be a reserved key");
120+
}
121+
122+
@Test
123+
@SuppressWarnings("unchecked")
124+
public void settingLabelAsPropertyShouldFail() {
125+
Vertex v = graph.addVertex(
126+
T.id, "foo",
127+
T.label, "bar"
128+
);
129+
Throwable thrown = catchThrowable(() -> v.property("type", "test"));
130+
assertThat(thrown)
131+
.isInstanceOf(IllegalArgumentException.class)
132+
.hasMessageContaining("Property key can not be a reserved key");
133+
}
134+
95135
@Test
96136
@SuppressWarnings("unchecked")
97137
public void edges() {

0 commit comments

Comments
 (0)