Skip to content
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

#1176 Allow Javers customization from Spring Context #1177

Merged
merged 1 commit into from
Jan 25, 2022
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
Expand Up @@ -4,9 +4,14 @@
import com.mongodb.client.MongoDatabase;
import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.JaversBuilderPlugin;
import org.javers.repository.mongo.MongoRepository;
import org.javers.spring.RegisterJsonTypeAdaptersPlugin;
import org.javers.spring.auditable.*;
import org.javers.spring.auditable.AuthorProvider;
import org.javers.spring.auditable.CommitPropertiesProvider;
import org.javers.spring.auditable.EmptyPropertiesProvider;
import org.javers.spring.auditable.MockAuthorProvider;
import org.javers.spring.auditable.SpringSecurityAuthorProvider;
import org.javers.spring.auditable.aspect.JaversAuditableAspect;
import org.javers.spring.auditable.aspect.springdata.JaversSpringDataAuditableRepositoryAspect;
import org.javers.spring.mongodb.TransactionalMongoJaversBuilder;
Expand All @@ -29,6 +34,8 @@
import org.springframework.data.mongodb.MongoDatabaseUtils;
import org.springframework.data.mongodb.MongoTransactionManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.javers.repository.mongo.MongoRepository.mongoRepositoryWithDocumentDBCompatibility;
Expand Down Expand Up @@ -60,8 +67,8 @@ public class JaversMongoAutoConfiguration {
@Autowired
private MongoDatabaseFactory dbFactory;

@Autowired
private RegisterJsonTypeAdaptersPlugin registerJsonTypeAdaptersPlugin;
@Autowired(required = false)
private List<JaversBuilderPlugin> plugins = new ArrayList<>();

@Autowired
private Optional<MongoTransactionManager> mongoTransactionManager;
Expand All @@ -81,7 +88,8 @@ public Javers javers() {
.withProperties(javersMongoProperties)
.withObjectAccessHook(javersMongoProperties.createObjectAccessHookInstance());

registerJsonTypeAdaptersPlugin.beforeAssemble(javersBuilder);
plugins.forEach(plugin -> plugin.beforeAssemble(javersBuilder));

return javersBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.javers.spring.boot.mongo

import com.mongodb.client.MongoDatabase
import org.javers.core.Javers
import org.javers.core.metamodel.type.EntityType
import org.javers.repository.jql.QueryBuilder
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
Expand All @@ -11,6 +9,10 @@ import spock.lang.Specification
@SpringBootTest(classes = [TestApplication])
class JaversMongoStarterIntegrationTest extends Specification{

class ValueObject {
BigDecimal value
}

@Autowired
Javers javers

Expand All @@ -35,4 +37,9 @@ class JaversMongoStarterIntegrationTest extends Specification{
expect:
javers.jsonConverter.toJson(new TestApplication.DummyBigDecimalEntity(BigDecimal.TEN)) == '"10"'
}

def "should allow javers customization from spring context" () {
expect:
javers.compare(new ValueObject(value: 1.123), new ValueObject(value: 1.124)).changes.size() == 0
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.javers.spring.boot.mongo;

import org.javers.common.collections.Maps;
import org.javers.core.JaversBuilderPlugin;
import org.javers.core.diff.custom.CustomBigDecimalComparator;
import org.javers.core.json.BasicStringTypeAdapter;
import org.javers.core.json.JsonTypeAdapter;
import org.javers.spring.auditable.CommitPropertiesProvider;
Expand Down Expand Up @@ -62,4 +64,10 @@ public Class getValueType() {
}
};
}

@Bean
JaversBuilderPlugin javersBuilderPlugin() {
return builder -> builder
.registerValue(BigDecimal.class, new CustomBigDecimalComparator(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.JaversBuilderPlugin;
import org.javers.repository.sql.ConnectionProvider;
import org.javers.repository.sql.DialectName;
import org.javers.repository.sql.JaversSqlRepository;
import org.javers.repository.sql.SqlRepositoryBuilder;
import org.javers.spring.RegisterJsonTypeAdaptersPlugin;
import org.javers.spring.auditable.*;
import org.javers.spring.auditable.AuthorProvider;
import org.javers.spring.auditable.CommitPropertiesProvider;
import org.javers.spring.auditable.EmptyPropertiesProvider;
import org.javers.spring.auditable.MockAuthorProvider;
import org.javers.spring.auditable.SpringSecurityAuthorProvider;
import org.javers.spring.auditable.aspect.JaversAuditableAspect;
import org.javers.spring.auditable.aspect.springdatajpa.JaversSpringDataJpaAuditableRepositoryAspect;
import org.javers.spring.jpa.JpaHibernateConnectionProvider;
Expand All @@ -25,10 +30,15 @@
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.EntityManagerFactory;
import java.util.ArrayList;
import java.util.List;

/**
* @author pawelszymczyk
Expand All @@ -46,8 +56,8 @@ public class JaversSqlAutoConfiguration {
@Autowired
private JaversSqlProperties javersSqlProperties;

@Autowired
private RegisterJsonTypeAdaptersPlugin registerJsonTypeAdaptersPlugin;
@Autowired(required = false)
private List<JaversBuilderPlugin> plugins = new ArrayList<>();

@Autowired
private EntityManagerFactory entityManagerFactory;
Expand Down Expand Up @@ -90,7 +100,9 @@ public Javers javers(JaversSqlRepository sqlRepository,
.registerJaversRepository(sqlRepository)
.withObjectAccessHook(javersSqlProperties.createObjectAccessHookInstance())
.withProperties(javersSqlProperties);
registerJsonTypeAdaptersPlugin.beforeAssemble(javersBuilder);

plugins.forEach(plugin -> plugin.beforeAssemble(javersBuilder));

return javersBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.javers.spring.sql


import org.javers.core.Javers
import org.javers.spring.boot.DummyEntity
import org.javers.spring.boot.TestApplication
import org.javers.spring.boot.sql.DummyEntityRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import spock.lang.Specification

import javax.transaction.Transactional
Expand All @@ -21,6 +19,10 @@ import static org.javers.repository.jql.QueryBuilder.byInstanceId
@Transactional
class JaversSqlStarterIntegrationTest extends Specification {

class ValueObject {
BigDecimal value
}

@Autowired
Javers javers

Expand Down Expand Up @@ -59,4 +61,9 @@ class JaversSqlStarterIntegrationTest extends Specification {
expect:
javers.jsonConverter.toJson(new TestApplication.DummyBigDecimalEntity(BigDecimal.TEN)) == '"10"'
}

def "should allow javers customization from spring context" () {
expect:
javers.compare(new ValueObject(value: 1.123), new ValueObject(value: 1.124)).changes.size() == 0
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.javers.spring.boot;

import org.apache.groovy.util.Maps;
import org.javers.core.JaversBuilderPlugin;
import org.javers.core.diff.custom.CustomBigDecimalComparator;
import org.javers.core.json.BasicStringTypeAdapter;
import org.javers.core.json.JsonTypeAdapter;
import org.javers.spring.auditable.CommitPropertiesProvider;
Expand Down Expand Up @@ -55,4 +57,10 @@ public Class getValueType() {
}
};
}

@Bean
JaversBuilderPlugin javersBuilderPlugin() {
return builder -> builder
.registerValue(BigDecimal.class, new CustomBigDecimalComparator(2));
}
}