Skip to content

Support automatic registration of converters #4181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
import org.bson.conversions.Bson;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.dao.DataAccessException;
Expand Down Expand Up @@ -70,16 +72,7 @@
import org.springframework.data.mongodb.core.aggregation.AggregationPipeline;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.JsonSchemaMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper;
import org.springframework.data.mongodb.core.convert.MongoWriter;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.convert.UpdateMapper;
import org.springframework.data.mongodb.core.convert.*;
import org.springframework.data.mongodb.core.index.IndexOperations;
import org.springframework.data.mongodb.core.index.IndexOperationsProvider;
import org.springframework.data.mongodb.core.index.MongoMappingEventPublisher;
Expand Down Expand Up @@ -159,6 +152,7 @@
* @author Yadhukrishna S Pai
* @author Anton Barkan
* @author Bartłomiej Mazur
* @author Sangyong Choi
*/
public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider {

Expand All @@ -185,6 +179,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
private @Nullable EntityCallbacks entityCallbacks;
private @Nullable ResourceLoader resourceLoader;
private @Nullable MongoPersistentEntityIndexCreator indexCreator;
private @Nullable ListableBeanFactory listableBeanFactory;

private SessionSynchronization sessionSynchronization = SessionSynchronization.ON_ACTUAL_TRANSACTION;

Expand Down Expand Up @@ -331,7 +326,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws

eventPublisher = applicationContext;
eventDelegate.setPublisher(eventPublisher);

listableBeanFactory = applicationContext;
if (entityCallbacks == null) {
setEntityCallbacks(EntityCallbacks.create(applicationContext));
}
Expand Down Expand Up @@ -2761,11 +2756,20 @@ private MongoPersistentEntity<?> getPersistentEntity(@Nullable Class<?> type) {
return type != null ? mappingContext.getPersistentEntity(type) : null;
}

private static MongoConverter getDefaultMongoConverter(MongoDatabaseFactory factory) {
private MongoConverter getDefaultMongoConverter(MongoDatabaseFactory factory) {
List<DocumentFieldConverter> converters;
if (listableBeanFactory != null) {
converters = listableBeanFactory
.getBeansOfType(DocumentFieldConverter.class)
.values()
.stream()
.toList();
} else {
converters = Collections.emptyList();
}

DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList());

MongoCustomConversions conversions = new MongoCustomConversions(converters);
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
mappingContext.afterPropertiesSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import static org.springframework.data.mongodb.core.query.SerializationUtils.*;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.core.convert.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
Expand Down Expand Up @@ -85,14 +88,6 @@
import org.springframework.data.mongodb.core.aggregation.RelaxedTypeBasedAggregationOperationContext;
import org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.convert.MongoWriter;
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.convert.UpdateMapper;
import org.springframework.data.mongodb.core.index.MongoMappingEventPublisher;
import org.springframework.data.mongodb.core.index.ReactiveIndexOperations;
import org.springframework.data.mongodb.core.index.ReactiveMongoPersistentEntityIndexCreator;
Expand Down Expand Up @@ -157,6 +152,7 @@
* @author Roman Puchkovskiy
* @author Mathieu Ouellet
* @author Yadhukrishna S Pai
* @author Sangyong Choi
* @since 2.0
*/
public class ReactiveMongoTemplate implements ReactiveMongoOperations, ApplicationContextAware {
Expand Down Expand Up @@ -185,6 +181,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
private @Nullable ApplicationEventPublisher eventPublisher;
private @Nullable ReactiveEntityCallbacks entityCallbacks;
private @Nullable ReactiveMongoPersistentEntityIndexCreator indexCreator;
private @Nullable ListableBeanFactory listableBeanFactory;

private SessionSynchronization sessionSynchronization = SessionSynchronization.ON_ACTUAL_TRANSACTION;

Expand Down Expand Up @@ -352,7 +349,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws

eventPublisher = applicationContext;
eventDelegate.setPublisher(eventPublisher);

listableBeanFactory = applicationContext;
if (entityCallbacks == null) {
setEntityCallbacks(ReactiveEntityCallbacks.create(applicationContext));
}
Expand Down Expand Up @@ -2581,8 +2578,18 @@ private MongoPersistentEntity<?> getPersistentEntity(@Nullable Class<?> type) {
}

private MappingMongoConverter getDefaultMongoConverter() {
List<DocumentFieldConverter> converters;
if (listableBeanFactory != null) {
converters = listableBeanFactory
.getBeansOfType(DocumentFieldConverter.class)
.values()
.stream()
.toList();
} else {
converters = Collections.emptyList();
}

MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList());
MongoCustomConversions conversions = new MongoCustomConversions(converters);

MongoMappingContext context = new MongoMappingContext();
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2011-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.convert;

import org.springframework.core.convert.converter.Converter;

/**
* @author Sangyong choi
*/
public interface DocumentFieldConverter<S, T> extends Converter<S, T> {
}