Skip to content

Improve framework for custom mapping couchbase converter. #1148

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

Merged
merged 1 commit into from
Jun 10, 2021
Merged
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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 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.
Expand Down Expand Up @@ -49,7 +49,6 @@
import org.springframework.util.StringUtils;

import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.DeserializationFeature;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.Module;
import com.couchbase.client.core.encryption.CryptoManager;
import com.couchbase.client.core.env.Authenticator;
import com.couchbase.client.core.env.PasswordAuthenticator;
Expand All @@ -60,7 +59,6 @@
import com.couchbase.client.java.env.ClusterEnvironment;
import com.couchbase.client.java.json.JacksonTransformers;
import com.couchbase.client.java.json.JsonValueModule;
import com.couchbase.client.java.json.RepackagedJsonValueModule;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
Expand All @@ -75,8 +73,6 @@
@Configuration
public abstract class AbstractCouchbaseConfiguration {

@Autowired ObjectMapper couchbaseObjectMapper;

/**
* The connection string which allows the SDK to connect to the cluster.
* <p>
Expand Down Expand Up @@ -144,7 +140,7 @@ public ClusterEnvironment couchbaseClusterEnvironment() {
if (!nonShadowedJacksonPresent()) {
throw new CouchbaseException("non-shadowed Jackson not present");
}
builder.jsonSerializer(JacksonJsonSerializer.create(couchbaseObjectMapper));
builder.jsonSerializer(JacksonJsonSerializer.create(couchbaseObjectMapper()));
configureEnvironment(builder);
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 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.
Expand All @@ -17,7 +17,6 @@
package org.springframework.data.couchbase.core.convert;

import java.util.Collections;
import java.util.Optional;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
Expand Down Expand Up @@ -68,7 +67,8 @@ public ConversionService getConversionService() {
}

/**
* Set the custom conversions.
* Set the custom conversions. Note that updating conversions requires a subsequent call to register them with the
* conversionService: conversions.registerConvertersIn(conversionService)
*
* @param conversions the conversions.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 the original author or authors.
* Copyright 2017-2021 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.
Expand Down Expand Up @@ -125,9 +125,6 @@ public class StringBasedN1qlQueryParser {
private final Collection<String> parameterNames = new HashSet<String>();
public final N1QLExpression parsedExpression;

private GenericConversionService conversionService = new DefaultConversionService();
private CustomConversions conversions = new CouchbaseCustomConversions(Collections.emptyList());

public StringBasedN1qlQueryParser(String statement, CouchbaseQueryMethod queryMethod, String bucketName,
CouchbaseConverter couchbaseConverter, String typeField, String typeValue, ParameterAccessor accessor,
SpelExpressionParser parser, QueryMethodEvaluationContextProvider evaluationContextProvider) {
Expand Down
40 changes: 28 additions & 12 deletions src/test/java/org/springframework/data/couchbase/domain/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

import java.lang.reflect.InvocationTargetException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.auditing.DateTimeProvider;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.SimpleCouchbaseClientFactory;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
Expand All @@ -33,6 +36,7 @@
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
import org.springframework.data.couchbase.domain.time.AuditingDateTimeProvider;
import org.springframework.data.couchbase.repository.auditing.EnableCouchbaseAuditing;
import org.springframework.data.couchbase.repository.auditing.EnableReactiveCouchbaseAuditing;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.repository.config.ReactiveRepositoryOperationsMapping;
import org.springframework.data.couchbase.repository.config.RepositoryOperationsMapping;
Expand All @@ -48,7 +52,12 @@
*/
@Configuration
@EnableCouchbaseRepositories
@EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef")
@EnableCouchbaseAuditing(auditorAwareRef = "auditorAwareRef", dateTimeProviderRef = "dateTimeProviderRef") // this
// activates
// auditing
@EnableReactiveCouchbaseAuditing(auditorAwareRef = "reactiveAuditorAwareRef",
dateTimeProviderRef = "dateTimeProviderRef") // this activates auditing

public class Config extends AbstractCouchbaseConfiguration {
String bucketname = "travel-sample";
String username = "Administrator";
Expand Down Expand Up @@ -104,6 +113,11 @@ public NaiveAuditorAware testAuditorAware() {
return new NaiveAuditorAware();
}

@Bean(name = "reactiveAuditorAwareRef")
public ReactiveNaiveAuditorAware testReactiveAuditorAware() {
return new ReactiveNaiveAuditorAware();
}

@Bean(name = "dateTimeProviderRef")
public DateTimeProvider testDateTimeProvider() {
return new AuditingDateTimeProvider();
Expand All @@ -113,12 +127,12 @@ public DateTimeProvider testDateTimeProvider() {
public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// ReactiveCouchbaseTemplate personTemplate =
// myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),new MappingCouchbaseConverter());
// ReactiveCouchbaseTemplate personTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("protected"),
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// ReactiveCouchbaseTemplate userTemplate = myReactiveCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
//baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
throw e;
Expand All @@ -129,11 +143,12 @@ public void configureReactiveRepositoryOperationsMapping(ReactiveRepositoryOpera
public void configureRepositoryOperationsMapping(RepositoryOperationsMapping baseMapping) {
try {
// comment out references to 'protected' and 'mybucket' - they are only to show how multi-bucket would work
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),new
// MappingCouchbaseConverter());
// CouchbaseTemplate personTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("protected"),
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
// baseMapping.mapEntity(Person.class, personTemplate); // Person goes in "protected" bucket
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),new
// MappingCouchbaseConverter());
// MappingCouchbaseConverter cvtr = (MappingCouchbaseConverter)baseMapping.getDefault().getConverter();
// CouchbaseTemplate userTemplate = myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),
// (MappingCouchbaseConverter) (baseMapping.getDefault().getConverter()));
// baseMapping.mapEntity(User.class, userTemplate); // User goes in "mybucket"
// everything else goes in getBucketName() ( which is travel-sample )
} catch (Exception e) {
Expand Down Expand Up @@ -176,10 +191,11 @@ public MappingCouchbaseConverter mappingCouchbaseConverter() {
return converter;
}

/* This uses a CustomMappingCouchbaseConverter instead of MappingCouchbaseConverter */
@Override
@Bean(name = "couchbaseMappingConverter")
@Bean(name = "mappingCouchbaseConverter")
public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingContext couchbaseMappingContext,
CouchbaseCustomConversions couchbaseCustomConversions) {
CouchbaseCustomConversions couchbaseCustomConversions /* there is a customConversions() method bean */) {
// MappingCouchbaseConverter relies on a SimpleInformationMapper
// that has an getAliasFor(info) that just returns getType().getName().
// Our CustomMappingCouchbaseConverter uses a TypeBasedCouchbaseTypeMapper that will
Expand Down