From 2a6107fcb6660520beecb8c012acf318f83d6dce Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 22 May 2019 12:07:27 +0200 Subject: [PATCH] DATAMONGO-2282 - Add shortcut to create JsonSchemaProperty for ObjectId. Original pull request: #757. --- .../data/mongodb/core/schema/JsonSchemaProperty.java | 12 ++++++++++++ .../core/schema/JsonSchemaPropertyUnitTests.java | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaProperty.java index c1fd88a60..36a44eb3f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaProperty.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaProperty.java @@ -90,6 +90,18 @@ public interface JsonSchemaProperty extends JsonSchemaObject { return new ObjectJsonSchemaProperty(identifier, JsonSchemaObject.object()); } + /** + * Creates a new {@link JsonSchemaProperty} with given {@literal identifier} of {@code bsonType : 'objectId'}. + * + * @param identifier the {@literal property} name or {@literal patternProperty} regex. Must not be {@literal null} nor + * {@literal empty}. + * @return new instance of {@link JsonSchemaProperty}. + * @since 2.2 + */ + static JsonSchemaProperty objectId(String identifier) { + return JsonSchemaProperty.named(identifier).ofType(Type.objectIdType()); + } + /** * Creates a new {@link NumericJsonSchemaProperty} with given {@literal identifier} of {@code type : 'number'}. * diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaPropertyUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaPropertyUnitTests.java index 1c173a41e..6c7121576 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaPropertyUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaPropertyUnitTests.java @@ -63,4 +63,9 @@ public class JsonSchemaPropertyUnitTests { public void shouldRenderTimestampCorrectly() { assertThat(JsonSchemaProperty.timestamp("foo").toDocument()).containsEntry("foo.bsonType", "timestamp"); } + + @Test // DATAMONGO-2282 + public void objectIdShouldBeRenderedCorrectly() { + assertThat(JsonSchemaProperty.objectId("_id").toDocument()).containsEntry("_id.bsonType", "objectId"); + } }