-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore
@Transient
properties in constructors.
We now ignore transient properties used in constructors. Regular transient properties default to the Java default values (null for object types, 0 for numeric primitives and so on). Using transient properties allows leveraging Kotlin's defaulting mechanism to infer default values. Record components can also be annotated with the Transient annotation to allow record construction. While this can be useful, we recommend using the Value annotation to use SpEL expressions to determine a useful value.
- Loading branch information
Showing
5 changed files
with
128 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/test/java/org/springframework/data/mapping/model/EntityInstantiatorIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2023 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 org.junit.jupiter.api.Test; | ||
import org.springframework.data.annotation.Transient; | ||
import org.springframework.data.mapping.context.SampleMappingContext; | ||
import org.springframework.data.mapping.context.SamplePersistentProperty; | ||
|
||
/** | ||
* Integration tests for {@link EntityInstantiator}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
public class EntityInstantiatorIntegrationTests { | ||
|
||
SampleMappingContext context = new SampleMappingContext(); | ||
EntityInstantiators instantiators = new EntityInstantiators(); | ||
|
||
@Test // GH-2942 | ||
void shouldDefaultTransientProperties() { | ||
|
||
WithTransientProperty instance = createInstance(WithTransientProperty.class); | ||
|
||
assertThat(instance.foo).isEqualTo(null); | ||
assertThat(instance.bar).isEqualTo(0); | ||
} | ||
|
||
@Test // GH-2942 | ||
void shouldDefaultTransientRecordProperties() { | ||
|
||
RecordWithTransientProperty instance = createInstance(RecordWithTransientProperty.class); | ||
|
||
assertThat(instance.foo).isEqualTo(null); | ||
assertThat(instance.bar).isEqualTo(0); | ||
} | ||
|
||
@Test // GH-2942 | ||
void shouldDefaultTransientKotlinProperty() { | ||
|
||
DataClassWithTransientProperties instance = createInstance(DataClassWithTransientProperties.class); | ||
|
||
// Kotlin defaulting | ||
assertThat(instance.getFoo()).isEqualTo("foo"); | ||
|
||
// Our defaulting | ||
assertThat(instance.getBar()).isEqualTo(0); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <E> E createInstance(Class<E> entityType) { | ||
|
||
var entity = context.getRequiredPersistentEntity(entityType); | ||
var instantiator = instantiators.getInstantiatorFor(entity); | ||
|
||
return (E) instantiator.createInstance(entity, | ||
new PersistentEntityParameterValueProvider<>(entity, new PropertyValueProvider<SamplePersistentProperty>() { | ||
@Override | ||
public <T> T getPropertyValue(SamplePersistentProperty property) { | ||
return null; | ||
} | ||
}, null)); | ||
} | ||
|
||
static class WithTransientProperty { | ||
|
||
@Transient String foo; | ||
@Transient int bar; | ||
|
||
public WithTransientProperty(String foo, int bar) { | ||
|
||
} | ||
} | ||
|
||
record RecordWithTransientProperty(@Transient String foo, @Transient int bar) { | ||
|
||
} | ||
|
||
} |