diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/IdentifiableJsonSchemaProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/IdentifiableJsonSchemaProperty.java index 0fc95ee09..fe7ab6346 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/IdentifiableJsonSchemaProperty.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/IdentifiableJsonSchemaProperty.java @@ -24,6 +24,7 @@ import org.bson.Document; import org.springframework.data.domain.Range; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.ArrayJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.BooleanJsonSchemaObject; +import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.DateJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NullJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NumericJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.ObjectJsonSchemaObject; @@ -927,4 +928,34 @@ public class IdentifiableJsonSchemaProperty implemen return new NullJsonSchemaProperty(identifier, jsonSchemaObjectDelegate.generatedDescription()); } } + + /** + * Convenience {@link JsonSchemaProperty} implementation for a {@code type : 'date'} property. + * + * @author Christoph Strobl + * @since 2.1 + */ + public static class DateJsonSchemaProperty extends IdentifiableJsonSchemaProperty { + + DateJsonSchemaProperty(String identifier, DateJsonSchemaObject schemaObject) { + super(identifier, schemaObject); + } + + /** + * @param description must not be {@literal null}. + * @return new instance of {@link NullJsonSchemaProperty}. + * @see NullJsonSchemaObject#description(String) + */ + public DateJsonSchemaProperty description(String description) { + return new DateJsonSchemaProperty(identifier, jsonSchemaObjectDelegate.description(description)); + } + + /** + * @return new instance of {@link NullJsonSchemaProperty}. + * @see NullJsonSchemaObject#generateDescription() + */ + public DateJsonSchemaProperty generatedDescription() { + return new DateJsonSchemaProperty(identifier, jsonSchemaObjectDelegate.generatedDescription()); + } + } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaObject.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaObject.java index 650e1711a..5c1486cfa 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaObject.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaObject.java @@ -29,6 +29,7 @@ import org.bson.Document; import org.bson.types.ObjectId; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.ArrayJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.BooleanJsonSchemaObject; +import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.DateJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NullJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NumericJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.ObjectJsonSchemaObject; @@ -125,6 +126,15 @@ public interface JsonSchemaObject { return new NullJsonSchemaObject(); } + /** + * Create a new {@link JsonSchemaObject} of {@code type : 'date'}. + * + * @return never {@literal null}. + */ + static DateJsonSchemaObject date() { + return new DateJsonSchemaObject(); + } + /** * Create a new {@link JsonSchemaObject} of given {@link Type}. * 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 6a9b8ac4a..37893693e 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 @@ -20,6 +20,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.ArrayJsonSchemaProperty; import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.BooleanJsonSchemaProperty; +import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.DateJsonSchemaProperty; import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.NullJsonSchemaProperty; import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.NumericJsonSchemaProperty; import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.ObjectJsonSchemaProperty; @@ -150,7 +151,7 @@ public interface JsonSchemaProperty extends JsonSchemaObject { * * @param identifier the {@literal property} name or {@literal patternProperty} regex. Must not be {@literal null} nor * {@literal empty}. - * @return new instance of {@link ArrayJsonSchemaProperty}. + * @return new instance of {@link BooleanJsonSchemaProperty}. */ static BooleanJsonSchemaProperty bool(String identifier) { return new BooleanJsonSchemaProperty(identifier, JsonSchemaObject.bool()); @@ -161,12 +162,23 @@ public interface JsonSchemaProperty extends JsonSchemaObject { * * @param identifier the {@literal property} name or {@literal patternProperty} regex. Must not be {@literal null} nor * {@literal empty}. - * @return new instance of {@link ArrayJsonSchemaProperty}. + * @return new instance of {@link NullJsonSchemaProperty}. */ static NullJsonSchemaProperty nil(String identifier) { return new NullJsonSchemaProperty(identifier, JsonSchemaObject.nil()); } + /** + * Creates a new {@link DateJsonSchemaProperty} with given {@literal identifier} of {@code type : 'date'}. + * + * @param identifier the {@literal property} name or {@literal patternProperty} regex. Must not be {@literal null} nor + * {@literal empty}. + * @return new instance of {@link DateJsonSchemaProperty}. + */ + static DateJsonSchemaProperty date(String identifier) { + return new DateJsonSchemaProperty(identifier, JsonSchemaObject.date()); + } + /** * Obtain a builder to create a {@link JsonSchemaProperty}. * diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/TypedJsonSchemaObject.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/TypedJsonSchemaObject.java index 6ca4831f1..2cab6a162 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/TypedJsonSchemaObject.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/TypedJsonSchemaObject.java @@ -1447,4 +1447,96 @@ public class TypedJsonSchemaObject extends UntypedJsonSchemaObject { return "Must be null."; } } + + /** + * {@link JsonSchemaObject} implementation of {@code type : 'null'} schema elements.
+ * Provides programmatic access to schema specifics via a fluent API producing immutable {@link JsonSchemaObject + * schema objects}. + * + * @author Christoph Strobl + * @since 2.1 + */ + static class DateJsonSchemaObject extends TypedJsonSchemaObject { + + DateJsonSchemaObject() { + this(null, false, null); + } + + private DateJsonSchemaObject(@Nullable String description, boolean generateDescription, + @Nullable Restrictions restrictions) { + super(Type.dateType(), description, generateDescription, restrictions); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#possibleValues(java.util.Collection) + */ + @Override + public DateJsonSchemaObject possibleValues(Collection possibleValues) { + return new DateJsonSchemaObject(description, generateDescription, restrictions.possibleValues(possibleValues)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#allOf(java.util.Collection) + */ + @Override + public DateJsonSchemaObject allOf(Collection allOf) { + return new DateJsonSchemaObject(description, generateDescription, restrictions.allOf(allOf)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#anyOf(java.util.Collection) + */ + @Override + public DateJsonSchemaObject anyOf(Collection anyOf) { + return new DateJsonSchemaObject(description, generateDescription, restrictions.anyOf(anyOf)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#oneOf(java.util.Collection) + */ + @Override + public DateJsonSchemaObject oneOf(Collection oneOf) { + return new DateJsonSchemaObject(description, generateDescription, restrictions.oneOf(oneOf)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#notMatch(org.springframework.data.mongodb.core.schema.JsonSchemaObject) + */ + @Override + public DateJsonSchemaObject notMatch(JsonSchemaObject notMatch) { + return new DateJsonSchemaObject(description, generateDescription, restrictions.notMatch(notMatch)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#description(java.lang.String) + */ + @Override + public DateJsonSchemaObject description(String description) { + return new DateJsonSchemaObject(description, generateDescription, restrictions); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#generatedDescription() + */ + @Override + public DateJsonSchemaObject generatedDescription() { + return new DateJsonSchemaObject(description, true, restrictions); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#generateDescription() + */ + @Override + protected String generateDescription() { + return "Must be a date."; + } + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaObjectUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaObjectUnitTests.java index 409a5d5f6..53a68907c 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaObjectUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaObjectUnitTests.java @@ -263,6 +263,17 @@ public class JsonSchemaObjectUnitTests { .isEqualTo(new Document("type", "null").append("description", "Must be null.")); } + // ----------------- + // type : 'date' + // ----------------- + + @Test // DATAMONGO-1877 + public void dateShouldRenderCorrectly() { + + assertThat(date().generatedDescription().toDocument()) + .isEqualTo(new Document("bsonType", "date").append("description", "Must be a date.")); + } + // ----------------- // type : 'any' // ----------------- 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 a4a5d5df7..d00d95ac8 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 @@ -19,11 +19,13 @@ import static org.springframework.data.mongodb.test.util.Assertions.*; import org.junit.Test; import org.springframework.data.mongodb.core.schema.JsonSchemaObject.Type; +import org.springframework.data.mongodb.core.schema.JsonSchemaProperty.JsonSchemaPropertyBuilder; /** * Unit tests for {@link JsonSchemaProperty}. * * @author Mark Paluch + * @author Christoph Strobl */ public class JsonSchemaPropertyUnitTests { @@ -52,4 +54,9 @@ public class JsonSchemaPropertyUnitTests { assertThat(JsonSchemaProperty.named("foo").ofType(Type.binaryType()).toDocument()).containsEntry("foo.bsonType", "binData"); } + + @Test // DATAMONGO-1877 + public void shouldRenderDateCorrectly() { + assertThat(JsonSchemaProperty.date("foo").toDocument()).containsEntry("foo.bsonType", "date"); + } }