Skip to content

Commit be714cf

Browse files
committed
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
1 parent 157684a commit be714cf

File tree

6 files changed

+52
-29
lines changed

6 files changed

+52
-29
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ public Criteria type(Collection<Type> types) {
450450

451451
Assert.notNull(types, "Types must not be null!");
452452

453-
criteria.put("$type", types.stream().map(Type::value).collect(Collectors.toList()));
453+
criteria.put("$type", types.stream().map(Type::toBsonType).map(Type::value).collect(Collectors.toList()));
454454
return this;
455455
}
456456

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ interface Type {
295295
Type OBJECT = jsonTypeOf("object");
296296
Type ARRAY = jsonTypeOf("array");
297297
Type NUMBER = jsonTypeOf("number");
298-
Type BOOLEAN = jsonTypeOf("bool");
298+
Type BOOLEAN = jsonTypeOf("boolean");
299299
Type STRING = jsonTypeOf("string");
300300
Type NULL = jsonTypeOf("null");
301301

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

+26
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.junit.jupiter.api.extension.ExtendWith;
3030
import org.springframework.data.annotation.Id;
3131
import org.springframework.data.mongodb.core.mapping.Field;
32+
import org.springframework.data.mongodb.core.query.Criteria;
33+
import org.springframework.data.mongodb.core.schema.JsonSchemaProperty;
3234
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
3335
import org.springframework.data.mongodb.test.util.Client;
3436
import org.springframework.data.mongodb.test.util.MongoTemplateExtension;
@@ -83,6 +85,28 @@ public void setUp() {
8385
template.save(jellyBelly);
8486
template.save(roseSpringHeart);
8587
template.save(kazmardBoombub);
88+
89+
}
90+
91+
@Test // DATAMONGO-1835
92+
public void createsWorkingSchema() {
93+
94+
try {
95+
template.dropCollection("person_schema");
96+
} catch (Exception e) {}
97+
98+
MongoJsonSchema schema = MongoJsonSchemaCreator.create(template.getConverter()).createSchemaFor(Person.class);
99+
100+
template.createCollection("person_schema", CollectionOptions.empty().schema(schema));
101+
}
102+
103+
@Test // DATAMONGO-1835
104+
public void queriesBooleanType() {
105+
106+
MongoJsonSchema schema = MongoJsonSchema.builder().properties(JsonSchemaProperty.bool("alive")).build();
107+
108+
assertThat(template.find(query(matchingDocumentStructure(schema)), Person.class)).hasSize(3);
109+
assertThat(template.find(query(Criteria.where("alive").type(Type.BOOLEAN)), Person.class)).hasSize(3);
86110
}
87111

88112
@Test // DATAMONGO-1835
@@ -201,6 +225,8 @@ static class Person {
201225
Gender gender;
202226
Address address;
203227
Object value;
228+
229+
boolean alive;
204230
}
205231

206232
@Data

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ enum JustSomeEnum {
292292
" 're-named-property' : { 'type' : 'string' }," + //
293293
" 'retypedProperty' : { 'bsonType' : 'javascript' }," + //
294294
" 'primitiveInt' : { 'bsonType' : 'int' }," + //
295-
" 'booleanProperty' : { 'type' : 'bool' }," + //
295+
" 'booleanProperty' : { 'type' : 'boolean' }," + //
296296
" 'longProperty' : { 'bsonType' : 'long' }," + //
297297
" 'intProperty' : { 'bsonType' : 'int' }," + //
298298
" 'dateProperty' : { 'bsonType' : 'date' }," + //

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

+20-23
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
import org.bson.Document;
2525
import org.junit.Test;
26-
2726
import org.springframework.data.geo.Point;
2827
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
2928
import org.springframework.data.mongodb.core.geo.GeoJsonLineString;
3029
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
30+
import org.springframework.data.mongodb.core.schema.JsonSchemaObject.Type;
3131
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
3232

3333
/**
@@ -90,8 +90,7 @@ public void equalIfCriteriaMatches() {
9090
@Test // GH-3286
9191
public void shouldBuildCorrectAndOperator() {
9292

93-
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
94-
Criteria.where("y").is(42),
93+
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true), Criteria.where("y").is(42),
9594
Criteria.where("z").is("value"));
9695

9796
Criteria criteria = Criteria.where("foo").is("bar").andOperator(operatorCriteria);
@@ -103,8 +102,7 @@ public void shouldBuildCorrectAndOperator() {
103102
@Test // GH-3286
104103
public void shouldBuildCorrectOrOperator() {
105104

106-
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
107-
Criteria.where("y").is(42),
105+
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true), Criteria.where("y").is(42),
108106
Criteria.where("z").is("value"));
109107

110108
Criteria criteria = Criteria.where("foo").is("bar").orOperator(operatorCriteria);
@@ -116,8 +114,7 @@ public void shouldBuildCorrectOrOperator() {
116114
@Test // GH-3286
117115
public void shouldBuildCorrectNorOperator() {
118116

119-
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true),
120-
Criteria.where("y").is(42),
117+
Collection<Criteria> operatorCriteria = Arrays.asList(Criteria.where("x").is(true), Criteria.where("y").is(42),
121118
Criteria.where("z").is("value"));
122119

123120
Criteria criteria = Criteria.where("foo").is("bar").norOperator(operatorCriteria);
@@ -205,6 +202,14 @@ public void getCriteriaObjectShouldRespectNotWhenNoKeyPresent() {
205202
assertThat(document).isEqualTo(new Document().append("$not", new Document("$lt", "foo")));
206203
}
207204

205+
@Test // GH-4220
206+
public void usesCorrectBsonType() {
207+
208+
Document document = new Criteria("foo").type(Type.BOOLEAN).getCriteriaObject();
209+
210+
assertThat(document).containsEntry("foo.$type", Collections.singletonList("bool"));
211+
}
212+
208213
@Test // DATAMONGO-1135
209214
public void geoJsonTypesShouldBeWrappedInGeometry() {
210215

@@ -302,8 +307,7 @@ public void shouldAppendBitsAllClearWithIntBitmaskCorrectly() {
302307

303308
Criteria numericBitmaskCriteria = new Criteria("field").bits().allClear(0b101);
304309

305-
assertThat(numericBitmaskCriteria.getCriteriaObject())
306-
.isEqualTo("{ \"field\" : { \"$bitsAllClear\" : 5} }");
310+
assertThat(numericBitmaskCriteria.getCriteriaObject()).isEqualTo("{ \"field\" : { \"$bitsAllClear\" : 5} }");
307311
}
308312

309313
@Test // DATAMONGO-1808
@@ -320,8 +324,7 @@ public void shouldAppendBitsAllSetWithIntBitmaskCorrectly() {
320324

321325
Criteria numericBitmaskCriteria = new Criteria("field").bits().allSet(0b101);
322326

323-
assertThat(numericBitmaskCriteria.getCriteriaObject())
324-
.isEqualTo("{ \"field\" : { \"$bitsAllSet\" : 5} }");
327+
assertThat(numericBitmaskCriteria.getCriteriaObject()).isEqualTo("{ \"field\" : { \"$bitsAllSet\" : 5} }");
325328
}
326329

327330
@Test // DATAMONGO-1808
@@ -338,8 +341,7 @@ public void shouldAppendBitsAnyClearWithIntBitmaskCorrectly() {
338341

339342
Criteria numericBitmaskCriteria = new Criteria("field").bits().anyClear(0b101);
340343

341-
assertThat(numericBitmaskCriteria.getCriteriaObject())
342-
.isEqualTo("{ \"field\" : { \"$bitsAnyClear\" : 5} }");
344+
assertThat(numericBitmaskCriteria.getCriteriaObject()).isEqualTo("{ \"field\" : { \"$bitsAnyClear\" : 5} }");
343345
}
344346

345347
@Test // DATAMONGO-1808
@@ -356,8 +358,7 @@ public void shouldAppendBitsAnySetWithIntBitmaskCorrectly() {
356358

357359
Criteria numericBitmaskCriteria = new Criteria("field").bits().anySet(0b101);
358360

359-
assertThat(numericBitmaskCriteria.getCriteriaObject())
360-
.isEqualTo("{ \"field\" : { \"$bitsAnySet\" : 5} }");
361+
assertThat(numericBitmaskCriteria.getCriteriaObject()).isEqualTo("{ \"field\" : { \"$bitsAnySet\" : 5} }");
361362
}
362363

363364
@Test // DATAMONGO-1808
@@ -429,14 +430,10 @@ public void shouldEqualForSamePatternAndFlags() {
429430
@Test // GH-3414
430431
public void shouldEqualForNestedPattern() {
431432

432-
Criteria left = new Criteria("a").orOperator(
433-
new Criteria("foo").regex("value", "i"),
434-
new Criteria("bar").regex("value")
435-
);
436-
Criteria right = new Criteria("a").orOperator(
437-
new Criteria("foo").regex("value", "i"),
438-
new Criteria("bar").regex("value")
439-
);
433+
Criteria left = new Criteria("a").orOperator(new Criteria("foo").regex("value", "i"),
434+
new Criteria("bar").regex("value"));
435+
Criteria right = new Criteria("a").orOperator(new Criteria("foo").regex("value", "i"),
436+
new Criteria("bar").regex("value"));
440437

441438
assertThat(left).isEqualTo(right);
442439
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void objectObjectShouldRenderPropertiesCorrectly() {
133133
.append("description", "Must be an object defining restrictions for name, active.").append("properties",
134134
new Document("name", new Document("type", "string")
135135
.append("description", "Must be a string with length unbounded-10].").append("maxLength", 10))
136-
.append("active", new Document("type", "bool")));
136+
.append("active", new Document("type", "boolean")));
137137

138138
assertThat(object().generatedDescription()
139139
.properties(JsonSchemaProperty.string("name").maxLength(10).generatedDescription(),
@@ -266,7 +266,7 @@ void numberObjectShouldRenderMinimumCorrectly() {
266266
void arrayObjectShouldRenderItemsCorrectly() {
267267

268268
assertThat(array().items(Arrays.asList(string(), bool())).toDocument()).isEqualTo(new Document("type", "array")
269-
.append("items", Arrays.asList(new Document("type", "string"), new Document("type", "bool"))));
269+
.append("items", Arrays.asList(new Document("type", "string"), new Document("type", "boolean"))));
270270
}
271271

272272
@Test // DATAMONGO-2613
@@ -316,7 +316,7 @@ void arrayObjectShouldRenderAdditionalItemsItemsCorrectly() {
316316
void booleanShouldRenderCorrectly() {
317317

318318
assertThat(bool().generatedDescription().toDocument())
319-
.isEqualTo(new Document("type", "bool").append("description", "Must be a boolean."));
319+
.isEqualTo(new Document("type", "boolean").append("description", "Must be a boolean."));
320320
}
321321

322322
// -----------------

0 commit comments

Comments
 (0)