Skip to content

Commit

Permalink
Fix KotlinCopyMethod detection for single-association property classes.
Browse files Browse the repository at this point in the history
KotlinCopyMethod.shouldUsePublicCopyMethod(…) now considers single-association arrangements. Also, the method now early exists if pre-conditions aren't met.

Closes #3131
  • Loading branch information
mp911de committed Aug 6, 2024
1 parent a976759 commit 5ddede1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.core.ResolvableType;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.SimpleAssociationHandler;
import org.springframework.data.mapping.SimplePropertyHandler;
import org.springframework.data.util.KotlinReflectionUtils;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -158,8 +159,9 @@ boolean shouldUsePublicCopyMethod(PersistentEntity<?, ?> entity) {

List<PersistentProperty<?>> persistentProperties = new ArrayList<>();
entity.doWithProperties((SimplePropertyHandler) persistentProperties::add);
entity.doWithAssociations((SimpleAssociationHandler) it -> persistentProperties.add(it.getInverse()));

if (persistentProperties.size() > 1) {
if (persistentProperties.size() != 1) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.function.Function;
import java.util.stream.Stream;

import org.jmolecules.ddd.types.Association;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.BeanUtils;
Expand Down Expand Up @@ -283,6 +284,27 @@ void shouldUnwrapValueTypeIfNecessary(PersistentPropertyAccessorFactory factory)
propertyAccessor.setProperty(createdBy, BeanUtils.instantiateClass(declaredConstructor, "baz"));
}

@MethodSource("factories")
@ParameterizedTest // GH-3131
void shouldApplyCopyForSinglePropertyClass(PersistentPropertyAccessorFactory factory) {

BasicPersistentEntity<Object, SamplePersistentProperty> entity = mappingContext
.getRequiredPersistentEntity(DataClassWithAssociation.class);

var foo = Association.forAggregate(new DataClassAggregate(new DataClassId("foo")));
var bar = Association.forAggregate(new DataClassAggregate(new DataClassId("bar")));
Object instance = createInstance(entity, parameter -> foo);

var propertyAccessor = factory.getPropertyAccessor(entity, instance);
var persistentProperty = entity.getRequiredPersistentProperty("assoc");

assertThat(propertyAccessor).isNotNull();
assertThat(propertyAccessor.getProperty(persistentProperty)).isEqualTo(foo);

propertyAccessor.setProperty(persistentProperty, bar);
assertThat(propertyAccessor.getProperty(persistentProperty)).isEqualTo(bar);
}

private Object createInstance(BasicPersistentEntity<?, SamplePersistentProperty> entity,
Function<Parameter<?, ?>, Object> parameterProvider) {
return instantiators.getInstantiatorFor(entity).createInstance(entity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.mapping.model

import org.jmolecules.ddd.types.AggregateRoot
import org.jmolecules.ddd.types.Identifier
import org.springframework.data.annotation.Id
import java.time.LocalDateTime

Expand All @@ -36,6 +38,19 @@ data class DataClassWithLazy(
val foo by lazy { 123 }
}

data class DataClassWithAssociation(
val assoc: org.jmolecules.ddd.types.Association<DataClassAggregate, DataClassId>
)

data class DataClassId(val id: String) : Identifier {

}

data class DataClassAggregate(val identifier: DataClassId) :
AggregateRoot<DataClassAggregate, DataClassId> {
override fun getId() = this.identifier
}

data class SingleSettableProperty constructor(val id: Double = Math.random()) {
val version: Int? = null
}
Expand Down

0 comments on commit 5ddede1

Please sign in to comment.