From 22cb17486c9e273c15d7b4b403bb22779bbe5339 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 2 Mar 2018 15:19:12 +0100 Subject: [PATCH] DATAMONGO-1877 - Polishing. Add JsonSchemaProperty#timestamp(). Fix JavaDoc. Original pull request: #537. --- .../IdentifiableJsonSchemaProperty.java | 39 +++++++- .../mongodb/core/schema/JsonSchemaObject.java | 10 ++ .../core/schema/JsonSchemaProperty.java | 12 +++ .../core/schema/TypedJsonSchemaObject.java | 95 ++++++++++++++++++- .../schema/JsonSchemaObjectUnitTests.java | 11 +++ .../schema/JsonSchemaPropertyUnitTests.java | 6 +- 6 files changed, 167 insertions(+), 6 deletions(-) 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 fe7ab6346..2d38105a9 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 @@ -29,6 +29,7 @@ import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NullJs import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NumericJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.ObjectJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.StringJsonSchemaObject; +import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.TimestampJsonSchemaObject; import org.springframework.util.Assert; /** @@ -943,19 +944,49 @@ public class IdentifiableJsonSchemaProperty implemen /** * @param description must not be {@literal null}. - * @return new instance of {@link NullJsonSchemaProperty}. - * @see NullJsonSchemaObject#description(String) + * @return new instance of {@link DateJsonSchemaProperty}. + * @see DateJsonSchemaProperty#description(String) */ public DateJsonSchemaProperty description(String description) { return new DateJsonSchemaProperty(identifier, jsonSchemaObjectDelegate.description(description)); } /** - * @return new instance of {@link NullJsonSchemaProperty}. - * @see NullJsonSchemaObject#generateDescription() + * @return new instance of {@link DateJsonSchemaProperty}. + * @see DateJsonSchemaProperty#generateDescription() */ public DateJsonSchemaProperty generatedDescription() { return new DateJsonSchemaProperty(identifier, jsonSchemaObjectDelegate.generatedDescription()); } } + + /** + * Convenience {@link JsonSchemaProperty} implementation for a {@code type : 'timestamp'} property. + * + * @author Mark Paluch + * @since 2.1 + */ + public static class TimestampJsonSchemaProperty extends IdentifiableJsonSchemaProperty { + + TimestampJsonSchemaProperty(String identifier, TimestampJsonSchemaObject schemaObject) { + super(identifier, schemaObject); + } + + /** + * @param description must not be {@literal null}. + * @return new instance of {@link TimestampJsonSchemaProperty}. + * @see TimestampJsonSchemaProperty#description(String) + */ + public TimestampJsonSchemaProperty description(String description) { + return new TimestampJsonSchemaProperty(identifier, jsonSchemaObjectDelegate.description(description)); + } + + /** + * @return new instance of {@link TimestampJsonSchemaProperty}. + * @see TimestampJsonSchemaProperty#generateDescription() + */ + public TimestampJsonSchemaProperty generatedDescription() { + return new TimestampJsonSchemaProperty(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 5c1486cfa..15164f1b5 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 @@ -34,6 +34,7 @@ import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NullJs import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NumericJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.ObjectJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.StringJsonSchemaObject; +import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.TimestampJsonSchemaObject; import org.springframework.lang.Nullable; import org.springframework.util.ClassUtils; @@ -135,6 +136,15 @@ public interface JsonSchemaObject { return new DateJsonSchemaObject(); } + /** + * Create a new {@link JsonSchemaObject} of {@code type : 'timestamp'}. + * + * @return never {@literal null}. + */ + static TimestampJsonSchemaObject timestamp() { + return new TimestampJsonSchemaObject(); + } + /** * 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 37893693e..44fb3c7dc 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 @@ -25,6 +25,7 @@ import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProper import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.NumericJsonSchemaProperty; import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.ObjectJsonSchemaProperty; import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.StringJsonSchemaProperty; +import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.TimestampJsonSchemaProperty; import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.UntypedJsonSchemaProperty; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.NumericJsonSchemaObject; import org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject.ObjectJsonSchemaObject; @@ -179,6 +180,17 @@ public interface JsonSchemaProperty extends JsonSchemaObject { return new DateJsonSchemaProperty(identifier, JsonSchemaObject.date()); } + /** + * Creates a new {@link TimestampJsonSchemaProperty} with given {@literal identifier} of {@code type : 'timestamp'}. + * + * @param identifier the {@literal property} name or {@literal patternProperty} regex. Must not be {@literal null} nor + * {@literal empty}. + * @return new instance of {@link TimestampJsonSchemaProperty}. + */ + static TimestampJsonSchemaProperty timestamp(String identifier) { + return new TimestampJsonSchemaProperty(identifier, JsonSchemaObject.timestamp()); + } + /** * 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 2cab6a162..ebf57187a 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 @@ -1449,7 +1449,7 @@ public class TypedJsonSchemaObject extends UntypedJsonSchemaObject { } /** - * {@link JsonSchemaObject} implementation of {@code type : 'null'} schema elements.
+ * {@link JsonSchemaObject} implementation of {@code type : 'date'} schema elements.
* Provides programmatic access to schema specifics via a fluent API producing immutable {@link JsonSchemaObject * schema objects}. * @@ -1539,4 +1539,97 @@ public class TypedJsonSchemaObject extends UntypedJsonSchemaObject { return "Must be a date."; } } + + /** + * {@link JsonSchemaObject} implementation of {@code type : 'timestamp'} schema elements.
+ * Provides programmatic access to schema specifics via a fluent API producing immutable {@link JsonSchemaObject + * schema objects}. + * + * @author Mark Paluch + * @since 2.1 + */ + static class TimestampJsonSchemaObject extends TypedJsonSchemaObject { + + TimestampJsonSchemaObject() { + this(null, false, null); + } + + private TimestampJsonSchemaObject(@Nullable String description, boolean generateDescription, + @Nullable Restrictions restrictions) { + super(Type.timestampType(), description, generateDescription, restrictions); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#possibleValues(java.util.Collection) + */ + @Override + public TimestampJsonSchemaObject possibleValues(Collection possibleValues) { + return new TimestampJsonSchemaObject(description, generateDescription, + restrictions.possibleValues(possibleValues)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#allOf(java.util.Collection) + */ + @Override + public TimestampJsonSchemaObject allOf(Collection allOf) { + return new TimestampJsonSchemaObject(description, generateDescription, restrictions.allOf(allOf)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#anyOf(java.util.Collection) + */ + @Override + public TimestampJsonSchemaObject anyOf(Collection anyOf) { + return new TimestampJsonSchemaObject(description, generateDescription, restrictions.anyOf(anyOf)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#oneOf(java.util.Collection) + */ + @Override + public TimestampJsonSchemaObject oneOf(Collection oneOf) { + return new TimestampJsonSchemaObject(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 TimestampJsonSchemaObject notMatch(JsonSchemaObject notMatch) { + return new TimestampJsonSchemaObject(description, generateDescription, restrictions.notMatch(notMatch)); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#description(java.lang.String) + */ + @Override + public TimestampJsonSchemaObject description(String description) { + return new TimestampJsonSchemaObject(description, generateDescription, restrictions); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#generatedDescription() + */ + @Override + public TimestampJsonSchemaObject generatedDescription() { + return new TimestampJsonSchemaObject(description, true, restrictions); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.schema.TypedJsonSchemaObject#generateDescription() + */ + @Override + protected String generateDescription() { + return "Must be a timestamp."; + } + } } 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 53a68907c..2912302a5 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 @@ -274,6 +274,17 @@ public class JsonSchemaObjectUnitTests { .isEqualTo(new Document("bsonType", "date").append("description", "Must be a date.")); } + // ----------------- + // type : 'timestamp' + // ----------------- + + @Test // DATAMONGO-1877 + public void timestampShouldRenderCorrectly() { + + assertThat(timestamp().generatedDescription().toDocument()) + .isEqualTo(new Document("bsonType", "timestamp").append("description", "Must be a timestamp.")); + } + // ----------------- // 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 d00d95ac8..361ed155d 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,7 +19,6 @@ 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}. @@ -59,4 +58,9 @@ public class JsonSchemaPropertyUnitTests { public void shouldRenderDateCorrectly() { assertThat(JsonSchemaProperty.date("foo").toDocument()).containsEntry("foo.bsonType", "date"); } + + @Test // DATAMONGO-1877 + public void shouldRenderTimestampCorrectly() { + assertThat(JsonSchemaProperty.timestamp("foo").toDocument()).containsEntry("foo.bsonType", "timestamp"); + } }