|
69 | 69 | import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
|
70 | 70 | import org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext;
|
71 | 71 | import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
|
72 |
| -import org.springframework.data.mongodb.core.convert.DbRefProxyHandler; |
73 |
| -import org.springframework.data.mongodb.core.convert.DbRefResolver; |
74 |
| -import org.springframework.data.mongodb.core.convert.DbRefResolverCallback; |
75 |
| -import org.springframework.data.mongodb.core.convert.MappingMongoConverter; |
76 |
| -import org.springframework.data.mongodb.core.convert.MongoConverter; |
77 |
| -import org.springframework.data.mongodb.core.convert.MongoCustomConversions; |
78 |
| -import org.springframework.data.mongodb.core.convert.MongoWriter; |
79 |
| -import org.springframework.data.mongodb.core.convert.QueryMapper; |
80 |
| -import org.springframework.data.mongodb.core.convert.UpdateMapper; |
| 72 | +import org.springframework.data.mongodb.core.convert.*; |
81 | 73 | import org.springframework.data.mongodb.core.index.IndexOperationsAdapter;
|
82 | 74 | import org.springframework.data.mongodb.core.index.MongoMappingEventPublisher;
|
83 | 75 | import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator;
|
|
125 | 117 | import com.mongodb.client.model.FindOneAndUpdateOptions;
|
126 | 118 | import com.mongodb.client.model.ReturnDocument;
|
127 | 119 | import com.mongodb.client.model.UpdateOptions;
|
| 120 | +import com.mongodb.client.model.ValidationOptions; |
128 | 121 | import com.mongodb.client.result.DeleteResult;
|
129 | 122 | import com.mongodb.client.result.UpdateResult;
|
130 | 123 | import com.mongodb.reactivestreams.client.AggregatePublisher;
|
@@ -176,6 +169,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
176 | 169 | private final PersistenceExceptionTranslator exceptionTranslator;
|
177 | 170 | private final QueryMapper queryMapper;
|
178 | 171 | private final UpdateMapper updateMapper;
|
| 172 | + private final JsonSchemaMapper schemaMapper; |
179 | 173 | private final SpelAwareProxyProjectionFactory projectionFactory;
|
180 | 174 |
|
181 | 175 | private @Nullable WriteConcern writeConcern;
|
@@ -220,6 +214,7 @@ public ReactiveMongoTemplate(ReactiveMongoDatabaseFactory mongoDatabaseFactory,
|
220 | 214 | this.mongoConverter = mongoConverter == null ? getDefaultMongoConverter() : mongoConverter;
|
221 | 215 | this.queryMapper = new QueryMapper(this.mongoConverter);
|
222 | 216 | this.updateMapper = new UpdateMapper(this.mongoConverter);
|
| 217 | + this.schemaMapper = new MongoJsonSchemaMapper(this.mongoConverter); |
223 | 218 | this.projectionFactory = new SpelAwareProxyProjectionFactory();
|
224 | 219 |
|
225 | 220 | // We always have a mapping context in the converter, whether it's a simple one or not
|
@@ -486,23 +481,24 @@ public <T> Mono<MongoCollection<Document>> createCollection(Class<T> entityClass
|
486 | 481 | */
|
487 | 482 | public <T> Mono<MongoCollection<Document>> createCollection(Class<T> entityClass,
|
488 | 483 | @Nullable CollectionOptions collectionOptions) {
|
489 |
| - return createCollection(determineCollectionName(entityClass), collectionOptions); |
| 484 | + return doCreateCollection(determineCollectionName(entityClass), |
| 485 | + convertToCreateCollectionOptions(collectionOptions, entityClass)); |
490 | 486 | }
|
491 | 487 |
|
492 | 488 | /*
|
493 | 489 | * (non-Javadoc)
|
494 | 490 | * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#createCollection(java.lang.String)
|
495 | 491 | */
|
496 |
| - public Mono<MongoCollection<Document>> createCollection(final String collectionName) { |
| 492 | + public Mono<MongoCollection<Document>> createCollection(String collectionName) { |
497 | 493 | return doCreateCollection(collectionName, new CreateCollectionOptions());
|
498 | 494 | }
|
499 | 495 |
|
500 | 496 | /*
|
501 | 497 | * (non-Javadoc)
|
502 | 498 | * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#createCollection(java.lang.String, org.springframework.data.mongodb.core.CollectionOptions)
|
503 | 499 | */
|
504 |
| - public Mono<MongoCollection<Document>> createCollection(final String collectionName, |
505 |
| - final CollectionOptions collectionOptions) { |
| 500 | + public Mono<MongoCollection<Document>> createCollection(String collectionName, |
| 501 | + @Nullable CollectionOptions collectionOptions) { |
506 | 502 | return doCreateCollection(collectionName, convertToCreateCollectionOptions(collectionOptions));
|
507 | 503 | }
|
508 | 504 |
|
@@ -814,8 +810,7 @@ protected <O> Flux<O> aggregate(Aggregation aggregation, String collectionName,
|
814 | 810 | }
|
815 | 811 |
|
816 | 812 | ReadDocumentCallback<O> readCallback = new ReadDocumentCallback<>(mongoConverter, outputType, collectionName);
|
817 |
| - return execute(collectionName, |
818 |
| - collection -> aggregateAndMap(collection, pipeline, options, readCallback)); |
| 813 | + return execute(collectionName, collection -> aggregateAndMap(collection, pipeline, options, readCallback)); |
819 | 814 | }
|
820 | 815 |
|
821 | 816 | private <O> Flux<O> aggregateAndMap(MongoCollection<Document> collection, List<Document> pipeline,
|
@@ -1995,17 +1990,36 @@ private Document addFieldsForProjection(Document fields, Class<?> domainType, Cl
|
1995 | 1990 | }
|
1996 | 1991 |
|
1997 | 1992 | protected CreateCollectionOptions convertToCreateCollectionOptions(@Nullable CollectionOptions collectionOptions) {
|
| 1993 | + return convertToCreateCollectionOptions(collectionOptions, Object.class); |
| 1994 | + } |
1998 | 1995 |
|
1999 |
| - CreateCollectionOptions result = new CreateCollectionOptions(); |
| 1996 | + protected CreateCollectionOptions convertToCreateCollectionOptions(@Nullable CollectionOptions collectionOptions, |
| 1997 | + Class<?> entityType) { |
2000 | 1998 |
|
2001 |
| - if (collectionOptions != null) { |
| 1999 | + CreateCollectionOptions result = new CreateCollectionOptions(); |
2002 | 2000 |
|
2003 |
| - collectionOptions.getCapped().ifPresent(result::capped); |
2004 |
| - collectionOptions.getSize().ifPresent(result::sizeInBytes); |
2005 |
| - collectionOptions.getMaxDocuments().ifPresent(result::maxDocuments); |
2006 |
| - collectionOptions.getCollation().map(Collation::toMongoCollation).ifPresent(result::collation); |
| 2001 | + if (collectionOptions == null) { |
| 2002 | + return result; |
2007 | 2003 | }
|
2008 | 2004 |
|
| 2005 | + collectionOptions.getCapped().ifPresent(result::capped); |
| 2006 | + collectionOptions.getSize().ifPresent(result::sizeInBytes); |
| 2007 | + collectionOptions.getMaxDocuments().ifPresent(result::maxDocuments); |
| 2008 | + collectionOptions.getCollation().map(Collation::toMongoCollation).ifPresent(result::collation); |
| 2009 | + |
| 2010 | + collectionOptions.getValidator().ifPresent(it -> { |
| 2011 | + |
| 2012 | + ValidationOptions validationOptions = new ValidationOptions(); |
| 2013 | + |
| 2014 | + it.getValidationAction().ifPresent(validationOptions::validationAction); |
| 2015 | + it.getValidationLevel().ifPresent(validationOptions::validationLevel); |
| 2016 | + |
| 2017 | + it.getSchema() |
| 2018 | + .ifPresent(val -> validationOptions.validator(schemaMapper.mapSchema(val.toDocument(), entityType))); |
| 2019 | + |
| 2020 | + result.validationOptions(validationOptions); |
| 2021 | + }); |
| 2022 | + |
2009 | 2023 | return result;
|
2010 | 2024 | }
|
2011 | 2025 |
|
|
0 commit comments