DATAMONGO-1877 - Polishing.

Add JsonSchemaProperty#timestamp(). Fix JavaDoc.

Original pull request: #537.
This commit is contained in:
Mark Paluch
2018-03-02 15:19:12 +01:00
parent 448df55ff7
commit 22cb17486c
6 changed files with 167 additions and 6 deletions

View File

@@ -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<T extends JsonSchemaObject> 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<TimestampJsonSchemaObject> {
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());
}
}
}

View File

@@ -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}.
*

View File

@@ -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}.
*

View File

@@ -1449,7 +1449,7 @@ public class TypedJsonSchemaObject extends UntypedJsonSchemaObject {
}
/**
* {@link JsonSchemaObject} implementation of {@code type : 'null'} schema elements.<br />
* {@link JsonSchemaObject} implementation of {@code type : 'date'} schema elements.<br />
* 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.<br />
* 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<? extends Object> 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<JsonSchemaObject> 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<JsonSchemaObject> 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<JsonSchemaObject> 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.";
}
}
}

View File

@@ -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'
// -----------------

View File

@@ -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");
}
}