DATAMONGO-2613 - Fix single element ArrayJsonSchemaObject to document mapping.

Now toDocument calls toDocument on items correctly.

Original Pull Request: #883
This commit is contained in:
Michal Kurcius
2020-08-19 23:08:00 +02:00
committed by Christoph Strobl
parent 553912a380
commit 25840d61fc
2 changed files with 11 additions and 1 deletions

View File

@@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch * @author Mark Paluch
* @author Michał Kurcius
* @since 2.1 * @since 2.1
*/ */
public class TypedJsonSchemaObject extends UntypedJsonSchemaObject { public class TypedJsonSchemaObject extends UntypedJsonSchemaObject {
@@ -1194,7 +1195,7 @@ public class TypedJsonSchemaObject extends UntypedJsonSchemaObject {
Document doc = new Document(super.toDocument()); Document doc = new Document(super.toDocument());
if (!CollectionUtils.isEmpty(items)) { if (!CollectionUtils.isEmpty(items)) {
doc.append("items", items.size() == 1 ? items.iterator().next() doc.append("items", items.size() == 1 ? items.iterator().next().toDocument()
: items.stream().map(JsonSchemaObject::toDocument).collect(Collectors.toList())); : items.stream().map(JsonSchemaObject::toDocument).collect(Collectors.toList()));
} }

View File

@@ -22,6 +22,7 @@ import static org.springframework.data.mongodb.core.schema.JsonSchemaObject.of;
import static org.springframework.data.mongodb.test.util.Assertions.*; import static org.springframework.data.mongodb.test.util.Assertions.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import org.bson.Document; import org.bson.Document;
import org.junit.Test; import org.junit.Test;
@@ -33,6 +34,7 @@ import org.springframework.data.domain.Range.*;
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch * @author Mark Paluch
* @author Michał Kurcius
*/ */
public class JsonSchemaObjectUnitTests { public class JsonSchemaObjectUnitTests {
@@ -209,6 +211,13 @@ public class JsonSchemaObjectUnitTests {
.append("items", Arrays.asList(new Document("type", "string"), new Document("type", "boolean")))); .append("items", Arrays.asList(new Document("type", "string"), new Document("type", "boolean"))));
} }
@Test // DATAMONGO-2613
public void arrayObjectShouldRenderItemsCorrectlyAsObjectIfContainsOnlyOneElement() {
assertThat(array().items(Collections.singletonList(string())).toDocument()).isEqualTo(new Document("type", "array")
.append("items", new Document("type", "string")));
}
@Test // DATAMONGO-1835 @Test // DATAMONGO-1835
public void arrayObjectShouldRenderMaxItemsCorrectly() { public void arrayObjectShouldRenderMaxItemsCorrectly() {