diff --git a/src/main/java/com/arangodb/internal/velocypack/VPackSerializers.java b/src/main/java/com/arangodb/internal/velocypack/VPackSerializers.java index 862507fe6..6bacf6b92 100644 --- a/src/main/java/com/arangodb/internal/velocypack/VPackSerializers.java +++ b/src/main/java/com/arangodb/internal/velocypack/VPackSerializers.java @@ -20,14 +20,33 @@ package com.arangodb.internal.velocypack; -import com.arangodb.entity.*; -import com.arangodb.entity.arangosearch.*; +import com.arangodb.entity.BaseDocument; +import com.arangodb.entity.BaseEdgeDocument; +import com.arangodb.entity.CollectionType; +import com.arangodb.entity.DocumentField; +import com.arangodb.entity.LogLevel; +import com.arangodb.entity.MinReplicationFactor; +import com.arangodb.entity.Permissions; +import com.arangodb.entity.ReplicationFactor; +import com.arangodb.entity.ViewType; +import com.arangodb.entity.arangosearch.ArangoSearchCompression; +import com.arangodb.entity.arangosearch.ArangoSearchProperties; +import com.arangodb.entity.arangosearch.CollectionLink; +import com.arangodb.entity.arangosearch.ConsolidationType; +import com.arangodb.entity.arangosearch.FieldLink; +import com.arangodb.entity.arangosearch.PrimarySort; +import com.arangodb.entity.arangosearch.StoreValuesType; +import com.arangodb.entity.arangosearch.StoredValue; import com.arangodb.internal.velocystream.internal.AuthenticationRequest; import com.arangodb.model.CollectionSchema; import com.arangodb.model.TraversalOptions; import com.arangodb.model.TraversalOptions.Order; import com.arangodb.model.arangosearch.ArangoSearchPropertiesOptions; -import com.arangodb.velocypack.*; +import com.arangodb.velocypack.VPackBuilder; +import com.arangodb.velocypack.VPackParser; +import com.arangodb.velocypack.VPackSerializer; +import com.arangodb.velocypack.VPackSlice; +import com.arangodb.velocypack.ValueType; import com.arangodb.velocystream.Request; import java.util.Collection; @@ -247,10 +266,10 @@ private static void serializeFieldLinks(final VPackBuilder builder, final Collec public static final VPackSerializer COLLECTION_VALIDATION = (builder, attribute, value, context) -> { VPackParser parser = new VPackParser.Builder().build(); - VPackSlice rule = parser.fromJson(value.getRule(), true); + VPackSlice rule = value.getRule() != null ? parser.fromJson(value.getRule(), true) : null; final Map doc = new HashMap<>(); doc.put("message", value.getMessage()); - doc.put("level", value.getLevel().getValue()); + doc.put("level", value.getLevel() != null ? value.getLevel().getValue() : null); doc.put("rule", rule); context.serialize(builder, attribute, doc); }; diff --git a/src/main/java/com/arangodb/model/CollectionCreateOptions.java b/src/main/java/com/arangodb/model/CollectionCreateOptions.java index f9ea9fecd..0d3200ec4 100644 --- a/src/main/java/com/arangodb/model/CollectionCreateOptions.java +++ b/src/main/java/com/arangodb/model/CollectionCreateOptions.java @@ -20,7 +20,11 @@ package com.arangodb.model; -import com.arangodb.entity.*; +import com.arangodb.entity.CollectionType; +import com.arangodb.entity.KeyOptions; +import com.arangodb.entity.KeyType; +import com.arangodb.entity.MinReplicationFactor; +import com.arangodb.entity.ReplicationFactor; /** * @author Mark Vollmary @@ -330,10 +334,21 @@ public CollectionSchema getSchema() { } /** - * @param schema object that specifies the collection level schema for documents - * @since ArangoDB 3.7 + * @param schema + * object that specifies the collection level schema for documents * @return options + * @since ArangoDB 3.7 + */ + public CollectionCreateOptions schema(final CollectionSchema schema) { + this.schema = schema; + return this; + } + + /** + * @since ArangoDB 3.7 + * @deprecated Use {@link #schema(CollectionSchema)} instead. */ + @Deprecated public CollectionCreateOptions setSchema(final CollectionSchema schema) { this.schema = schema; return this; diff --git a/src/main/java/com/arangodb/model/CollectionPropertiesOptions.java b/src/main/java/com/arangodb/model/CollectionPropertiesOptions.java index 31bd459e8..35e218787 100644 --- a/src/main/java/com/arangodb/model/CollectionPropertiesOptions.java +++ b/src/main/java/com/arangodb/model/CollectionPropertiesOptions.java @@ -29,6 +29,7 @@ public class CollectionPropertiesOptions { private Boolean waitForSync; private Long journalSize; + private CollectionSchema schema; public CollectionPropertiesOptions() { super(); @@ -62,4 +63,18 @@ public CollectionPropertiesOptions journalSize(final Long journalSize) { return this; } + public CollectionSchema getSchema() { + return schema; + } + + /** + * @param schema object that specifies the collection level schema for documents + * @since ArangoDB 3.7 + * @return options + */ + public CollectionPropertiesOptions schema(final CollectionSchema schema) { + this.schema = schema; + return this; + } + } diff --git a/src/main/java/com/arangodb/model/CollectionSchema.java b/src/main/java/com/arangodb/model/CollectionSchema.java index dcf7b3f0f..49b6fea85 100644 --- a/src/main/java/com/arangodb/model/CollectionSchema.java +++ b/src/main/java/com/arangodb/model/CollectionSchema.java @@ -28,6 +28,15 @@ * @since ArangoDB 3.7 */ public class CollectionSchema { + + /** + * Value to unset the collection schema on properties update {@link com.arangodb.ArangoCollection#changeProperties(CollectionPropertiesOptions)}. + */ + public static final CollectionSchema NULL_SCHEMA = new CollectionSchema() + .setLevel(null) + .setMessage(null) + .setRule(null); + private String rule; private Level level; private String message; diff --git a/src/test/java/com/arangodb/ArangoCollectionTest.java b/src/test/java/com/arangodb/ArangoCollectionTest.java index 275d8fe90..06e5fb61b 100644 --- a/src/test/java/com/arangodb/ArangoCollectionTest.java +++ b/src/test/java/com/arangodb/ArangoCollectionTest.java @@ -37,6 +37,7 @@ import com.arangodb.entity.ShardEntity; import com.arangodb.model.CollectionCreateOptions; import com.arangodb.model.CollectionPropertiesOptions; +import com.arangodb.model.CollectionSchema; import com.arangodb.model.DocumentCreateOptions; import com.arangodb.model.DocumentDeleteOptions; import com.arangodb.model.DocumentExistsOptions; @@ -2612,14 +2613,47 @@ public void getPropeties() { public void changeProperties() { final CollectionPropertiesEntity properties = collection.getProperties(); assertThat(properties.getWaitForSync(), is(notNullValue())); + if (isAtLeastVersion(3, 7)) { + assertThat(properties.getSchema(), is(nullValue())); + } - final CollectionPropertiesEntity changedProperties = collection.changeProperties(new CollectionPropertiesOptions() - .waitForSync(!properties.getWaitForSync())); + String schemaRule = ("{ " + + " \"properties\": {" + + " \"number\": {" + + " \"type\": \"number\"" + + " }" + + " }" + + " }") + .replaceAll("\\s", ""); + String schemaMessage = "The document has problems!"; + + CollectionPropertiesOptions updatedOptions = new CollectionPropertiesOptions() + .waitForSync(!properties.getWaitForSync()) + .schema(new CollectionSchema() + .setLevel(CollectionSchema.Level.NEW) + .setMessage(schemaMessage) + .setRule(schemaRule) + ); + + final CollectionPropertiesEntity changedProperties = collection.changeProperties(updatedOptions); assertThat(changedProperties.getWaitForSync(), is(notNullValue())); assertThat(changedProperties.getWaitForSync(), is(!properties.getWaitForSync())); + if (isAtLeastVersion(3, 7)) { + assertThat(changedProperties.getSchema(), is(notNullValue())); + assertThat(changedProperties.getSchema().getLevel(), is(CollectionSchema.Level.NEW)); + assertThat(changedProperties.getSchema().getMessage(), is(schemaMessage)); + assertThat(changedProperties.getSchema().getRule(), is(schemaRule)); + } // revert changes - collection.changeProperties(new CollectionPropertiesOptions().waitForSync(properties.getWaitForSync())); + CollectionPropertiesEntity revertedProperties = collection.changeProperties(new CollectionPropertiesOptions() + .waitForSync(properties.getWaitForSync()) + .schema(CollectionSchema.NULL_SCHEMA) + ); + if (isAtLeastVersion(3, 7)) { + assertThat(revertedProperties.getSchema(), is(nullValue())); + } + } @Test diff --git a/src/test/java/com/arangodb/ArangoDatabaseTest.java b/src/test/java/com/arangodb/ArangoDatabaseTest.java index 7c05c56e8..4b96b5a8e 100644 --- a/src/test/java/com/arangodb/ArangoDatabaseTest.java +++ b/src/test/java/com/arangodb/ArangoDatabaseTest.java @@ -311,7 +311,7 @@ public void createCollectionWithJsonSchema() { final CollectionEntity result = db .createCollection(name, new CollectionCreateOptions() - .setSchema( + .schema( new CollectionSchema() .setLevel(CollectionSchema.Level.NEW) .setMessage(message)