Browse Source

Use correct `boolean` type for JSON Schema creation.

We now use the correct JSON type boolean again when creating schemas. Furthermore, we use the bool type for MongoDB $type queries.

Closes #4220
3.4.x
Mark Paluch 3 years ago
parent
commit
be714cf702
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java
  2. 2
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaObject.java
  3. 26
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/JsonSchemaQueryTests.java
  4. 2
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreatorUnitTests.java
  5. 43
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java
  6. 6
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaObjectUnitTests.java

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

@ -450,7 +450,7 @@ public class Criteria implements CriteriaDefinition { @@ -450,7 +450,7 @@ public class Criteria implements CriteriaDefinition {
Assert.notNull(types, "Types must not be null!");
criteria.put("$type", types.stream().map(Type::value).collect(Collectors.toList()));
criteria.put("$type", types.stream().map(Type::toBsonType).map(Type::value).collect(Collectors.toList()));
return this;
}

2
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/JsonSchemaObject.java

@ -295,7 +295,7 @@ public interface JsonSchemaObject { @@ -295,7 +295,7 @@ public interface JsonSchemaObject {
Type OBJECT = jsonTypeOf("object");
Type ARRAY = jsonTypeOf("array");
Type NUMBER = jsonTypeOf("number");
Type BOOLEAN = jsonTypeOf("bool");
Type BOOLEAN = jsonTypeOf("boolean");
Type STRING = jsonTypeOf("string");
Type NULL = jsonTypeOf("null");

26
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/JsonSchemaQueryTests.java

@ -29,6 +29,8 @@ import org.junit.jupiter.api.Test; @@ -29,6 +29,8 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.schema.JsonSchemaProperty;
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
import org.springframework.data.mongodb.test.util.Client;
import org.springframework.data.mongodb.test.util.MongoTemplateExtension;
@ -83,6 +85,28 @@ public class JsonSchemaQueryTests { @@ -83,6 +85,28 @@ public class JsonSchemaQueryTests {
template.save(jellyBelly);
template.save(roseSpringHeart);
template.save(kazmardBoombub);
}
@Test // DATAMONGO-1835
public void createsWorkingSchema() {
try {
template.dropCollection("person_schema");
} catch (Exception e) {}
MongoJsonSchema schema = MongoJsonSchemaCreator.create(template.getConverter()).createSchemaFor(Person.class);
template.createCollection("person_schema", CollectionOptions.empty().schema(schema));
}
@Test // DATAMONGO-1835
public void queriesBooleanType() {
MongoJsonSchema schema = MongoJsonSchema.builder().properties(JsonSchemaProperty.bool("alive")).build();
assertThat(template.find(query(matchingDocumentStructure(schema)), Person.class)).hasSize(3);
assertThat(template.find(query(Criteria.where("alive").type(Type.BOOLEAN)), Person.class)).hasSize(3);
}
@Test // DATAMONGO-1835
@ -201,6 +225,8 @@ public class JsonSchemaQueryTests { @@ -201,6 +225,8 @@ public class JsonSchemaQueryTests {
Gender gender;
Address address;
Object value;
boolean alive;
}
@Data

2
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreatorUnitTests.java

@ -292,7 +292,7 @@ class MappingMongoJsonSchemaCreatorUnitTests { @@ -292,7 +292,7 @@ class MappingMongoJsonSchemaCreatorUnitTests {
" 're-named-property' : { 'type' : 'string' }," + //
" 'retypedProperty' : { 'bsonType' : 'javascript' }," + //
" 'primitiveInt' : { 'bsonType' : 'int' }," + //
" 'booleanProperty' : { 'type' : 'bool' }," + //
" 'booleanProperty' : { 'type' : 'boolean' }," + //
" 'longProperty' : { 'bsonType' : 'long' }," + //
" 'intProperty' : { 'bsonType' : 'int' }," + //
" 'dateProperty' : { 'bsonType' : 'date' }," + //

43
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/CriteriaUnitTests.java

@ -23,11 +23,11 @@ import java.util.Collections; @@ -23,11 +23,11 @@ import java.util.Collections;
import org.bson.Document;
import org.junit.Test;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
import org.springframework.data.mongodb.core.geo.GeoJsonLineString;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.schema.JsonSchemaObject.Type;
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
/**
@ -90,8 +90,7 @@ public class CriteriaUnitTests { @@ -90,8 +90,7 @@ public class CriteriaUnitTests {
@Test // GH-3286
public void shouldBuildCorrectAndOperator() {
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
Criteria.where("y").is(42),
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true), Criteria.where("y").is(42),
Criteria.where("z").is("value"));
Criteria criteria = Criteria.where("foo").is("bar").andOperator(operatorCriteria);
@ -103,8 +102,7 @@ public class CriteriaUnitTests { @@ -103,8 +102,7 @@ public class CriteriaUnitTests {
@Test // GH-3286
public void shouldBuildCorrectOrOperator() {
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
Criteria.where("y").is(42),
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true), Criteria.where("y").is(42),
Criteria.where("z").is("value"));
Criteria criteria = Criteria.where("foo").is("bar").orOperator(operatorCriteria);
@ -116,8 +114,7 @@ public class CriteriaUnitTests { @@ -116,8 +114,7 @@ public class CriteriaUnitTests {
@Test // GH-3286
public void shouldBuildCorrectNorOperator() {
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
Criteria.where("y").is(42),
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true), Criteria.where("y").is(42),
Criteria.where("z").is("value"));
Criteria criteria = Criteria.where("foo").is("bar").norOperator(operatorCriteria);
@ -205,6 +202,14 @@ public class CriteriaUnitTests { @@ -205,6 +202,14 @@ public class CriteriaUnitTests {
assertThat(document).isEqualTo(new Document().append("$not", new Document("$lt", "foo")));
}
@Test // GH-4220
public void usesCorrectBsonType() {
Document document = new Criteria("foo").type(Type.BOOLEAN).getCriteriaObject();
assertThat(document).containsEntry("foo.$type", Collections.singletonList("bool"));
}
@Test // DATAMONGO-1135
public void geoJsonTypesShouldBeWrappedInGeometry() {
@ -302,8 +307,7 @@ public class CriteriaUnitTests { @@ -302,8 +307,7 @@ public class CriteriaUnitTests {
Criteria numericBitmaskCriteria = new Criteria("field").bits().allClear(0b101);
assertThat(numericBitmaskCriteria.getCriteriaObject())
.isEqualTo("{ \"field\" : { \"$bitsAllClear\" : 5} }");
assertThat(numericBitmaskCriteria.getCriteriaObject()).isEqualTo("{ \"field\" : { \"$bitsAllClear\" : 5} }");
}
@Test // DATAMONGO-1808
@ -320,8 +324,7 @@ public class CriteriaUnitTests { @@ -320,8 +324,7 @@ public class CriteriaUnitTests {
Criteria numericBitmaskCriteria = new Criteria("field").bits().allSet(0b101);
assertThat(numericBitmaskCriteria.getCriteriaObject())
.isEqualTo("{ \"field\" : { \"$bitsAllSet\" : 5} }");
assertThat(numericBitmaskCriteria.getCriteriaObject()).isEqualTo("{ \"field\" : { \"$bitsAllSet\" : 5} }");
}
@Test // DATAMONGO-1808
@ -338,8 +341,7 @@ public class CriteriaUnitTests { @@ -338,8 +341,7 @@ public class CriteriaUnitTests {
Criteria numericBitmaskCriteria = new Criteria("field").bits().anyClear(0b101);
assertThat(numericBitmaskCriteria.getCriteriaObject())
.isEqualTo("{ \"field\" : { \"$bitsAnyClear\" : 5} }");
assertThat(numericBitmaskCriteria.getCriteriaObject()).isEqualTo("{ \"field\" : { \"$bitsAnyClear\" : 5} }");
}
@Test // DATAMONGO-1808
@ -356,8 +358,7 @@ public class CriteriaUnitTests { @@ -356,8 +358,7 @@ public class CriteriaUnitTests {
Criteria numericBitmaskCriteria = new Criteria("field").bits().anySet(0b101);
assertThat(numericBitmaskCriteria.getCriteriaObject())
.isEqualTo("{ \"field\" : { \"$bitsAnySet\" : 5} }");
assertThat(numericBitmaskCriteria.getCriteriaObject()).isEqualTo("{ \"field\" : { \"$bitsAnySet\" : 5} }");
}
@Test // DATAMONGO-1808
@ -429,14 +430,10 @@ public class CriteriaUnitTests { @@ -429,14 +430,10 @@ public class CriteriaUnitTests {
@Test // GH-3414
public void shouldEqualForNestedPattern() {
Criteria left = new Criteria("a").orOperator(
new Criteria("foo").regex("value", "i"),
new Criteria("bar").regex("value")
);
Criteria right = new Criteria("a").orOperator(
new Criteria("foo").regex("value", "i"),
new Criteria("bar").regex("value")
);
Criteria left = new Criteria("a").orOperator(new Criteria("foo").regex("value", "i"),
new Criteria("bar").regex("value"));
Criteria right = new Criteria("a").orOperator(new Criteria("foo").regex("value", "i"),
new Criteria("bar").regex("value"));
assertThat(left).isEqualTo(right);
}

6
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/schema/JsonSchemaObjectUnitTests.java

@ -133,7 +133,7 @@ class JsonSchemaObjectUnitTests { @@ -133,7 +133,7 @@ class JsonSchemaObjectUnitTests {
.append("description", "Must be an object defining restrictions for name, active.").append("properties",
new Document("name", new Document("type", "string")
.append("description", "Must be a string with length unbounded-10].").append("maxLength", 10))
.append("active", new Document("type", "bool")));
.append("active", new Document("type", "boolean")));
assertThat(object().generatedDescription()
.properties(JsonSchemaProperty.string("name").maxLength(10).generatedDescription(),
@ -266,7 +266,7 @@ class JsonSchemaObjectUnitTests { @@ -266,7 +266,7 @@ class JsonSchemaObjectUnitTests {
void arrayObjectShouldRenderItemsCorrectly() {
assertThat(array().items(Arrays.asList(string(), bool())).toDocument()).isEqualTo(new Document("type", "array")
.append("items", Arrays.asList(new Document("type", "string"), new Document("type", "bool"))));
.append("items", Arrays.asList(new Document("type", "string"), new Document("type", "boolean"))));
}
@Test // DATAMONGO-2613
@ -316,7 +316,7 @@ class JsonSchemaObjectUnitTests { @@ -316,7 +316,7 @@ class JsonSchemaObjectUnitTests {
void booleanShouldRenderCorrectly() {
assertThat(bool().generatedDescription().toDocument())
.isEqualTo(new Document("type", "bool").append("description", "Must be a boolean."));
.isEqualTo(new Document("type", "boolean").append("description", "Must be a boolean."));
}
// -----------------

Loading…
Cancel
Save