Use primary copy method for Kotlin data classes.
We now resolve the copy method for Kotlin data classes that match the primary constructor. Previously, copy method resolution could find a secondary copy method as we didn't check for the primary constructor structure (parameter names and types). Closes #2324.
This commit is contained in:
@@ -18,10 +18,12 @@ package org.springframework.data.mapping.model;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.PersistentPropertyAccessor;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
import org.springframework.data.repository.core.EntityInformation;
|
||||
@@ -32,6 +34,7 @@ import org.springframework.data.repository.core.support.PersistentEntityInformat
|
||||
*
|
||||
* @author John Blum
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
|
||||
|
||||
@@ -53,6 +56,17 @@ public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
|
||||
assertThat(getEntityInformation(Person.class).getId(jonDoe)).isEqualTo(jonDoe.name);
|
||||
}
|
||||
|
||||
@Test // #2324
|
||||
public void shouldGeneratePropertyAccessorForKotlinClassWithMultipleCopyMethods() {
|
||||
|
||||
ClassGeneratingPropertyAccessorFactory factory = new ClassGeneratingPropertyAccessorFactory();
|
||||
PersistentPropertyAccessor<WithCustomCopyMethod> propertyAccessor = factory.getPropertyAccessor(
|
||||
mappingContext.getRequiredPersistentEntity(WithCustomCopyMethod.class),
|
||||
new WithCustomCopyMethod("", "", "", 1, LocalDateTime.MAX, LocalDateTime.MAX, ""));
|
||||
|
||||
assertThat(propertyAccessor).isNotNull();
|
||||
}
|
||||
|
||||
private EntityInformation<Object, Serializable> getEntityInformation(Class<?> type) {
|
||||
|
||||
PersistentEntity<Object, SamplePersistentProperty> entity = mappingContext.getRequiredPersistentEntity(type);
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 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.
|
||||
* 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.mapping.model;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link KotlinCopyMethod}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class KotlinCopyMethodUnitTests {
|
||||
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
|
||||
@Test // #2324
|
||||
void shouldLookupPrimaryConstructor() {
|
||||
|
||||
Optional<KotlinCopyMethod> optional = KotlinCopyMethod.findCopyMethod(DataClassKt.class);
|
||||
|
||||
assertThat(optional).hasValueSatisfying(actual -> {
|
||||
// $this, 1 component, 1 defaulting mask, 1 Object marker
|
||||
assertThat(actual.getSyntheticCopyMethod().getParameterCount()).isEqualTo(4);
|
||||
|
||||
// $this, 1 component
|
||||
assertThat(actual.getCopyFunction().getParameters()).hasSize(2);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // #2324
|
||||
void shouldLookupPrimaryConstructorWhenTwoCopyMethodsArePresent() {
|
||||
|
||||
Optional<KotlinCopyMethod> optional = KotlinCopyMethod.findCopyMethod(WithCustomCopyMethod.class);
|
||||
|
||||
assertThat(optional).hasValueSatisfying(actual -> {
|
||||
// $this, 7 components, 1 defaulting mask, 1 Object marker
|
||||
assertThat(actual.getSyntheticCopyMethod().getParameterCount()).isEqualTo(10);
|
||||
|
||||
// $this, 7 components
|
||||
assertThat(actual.getCopyFunction().getParameters()).hasSize(8);
|
||||
|
||||
assertThat(
|
||||
actual.shouldUsePublicCopyMethod(mappingContext.getRequiredPersistentEntity(WithCustomCopyMethod.class)))
|
||||
.isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@Test // #2324
|
||||
void shouldUsePublicKotlinMethodForSinglePropertyEntities() {
|
||||
|
||||
KotlinCopyMethod copyMethod = KotlinCopyMethod.findCopyMethod(DataClassKt.class).get();
|
||||
|
||||
assertThat(copyMethod.shouldUsePublicCopyMethod(mappingContext.getRequiredPersistentEntity(DataClassKt.class)))
|
||||
.isTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user