DATAMONGO-1877 - Add JsonSchemaProperty for date type.
We now provide factory methods and schema objects for date and timestamp types. Original pull request: #537.
This commit is contained in:
committed by
Mark Paluch
parent
a71e4bb313
commit
448df55ff7
@@ -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<T extends JsonSchemaObject> 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<DateJsonSchemaObject> {
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}.
|
||||
*
|
||||
|
||||
@@ -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}.
|
||||
*
|
||||
|
||||
@@ -1447,4 +1447,96 @@ public class TypedJsonSchemaObject extends UntypedJsonSchemaObject {
|
||||
return "Must be null.";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link JsonSchemaObject} implementation of {@code type : 'null'} schema elements.<br />
|
||||
* 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<? extends Object> 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<JsonSchemaObject> 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<JsonSchemaObject> 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<JsonSchemaObject> 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.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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'
|
||||
// -----------------
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user