Skip to content

Commit 7d3c6ea

Browse files
mp911dechristophstrobl
authored andcommitted
Support instantiation of Kotlin class with overridden read-only property.
Closes: #4485 Original Pull Request: #4777
1 parent 9afa237 commit 7d3c6ea

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ private <S> S populateProperties(ConversionContext context, MongoPersistentEntit
559559
MongoDbPropertyValueProvider valueProvider = new MongoDbPropertyValueProvider(contextToUse, documentAccessor,
560560
evaluator, spELContext);
561561

562-
Predicate<MongoPersistentProperty> propertyFilter = isIdentifier(entity).or(isConstructorArgument(entity)).negate();
562+
Predicate<MongoPersistentProperty> propertyFilter = isIdentifier(entity).or(isConstructorArgument(entity))
563+
.or(Predicates.negate(PersistentProperty::isReadable)).negate();
563564
readProperties(contextToUse, entity, accessor, documentAccessor, valueProvider, evaluator, propertyFilter);
564565

565566
return accessor.getBean();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.convert
17+
18+
import org.assertj.core.api.Assertions.assertThat
19+
import org.bson.Document
20+
import org.junit.jupiter.api.Test
21+
import org.springframework.data.mongodb.core.mapping.MongoMappingContext
22+
23+
/**
24+
* Kotlin unit tests for [MappingMongoConverter].
25+
*
26+
* @author Mark Paluch
27+
*/
28+
class MappingMongoConverterKtUnitTests {
29+
30+
@Test // GH-4485
31+
fun shouldIgnoreNonReadableProperties() {
32+
33+
val document = Document.parse("{_id: 'baz', type: 'SOME_VALUE'}")
34+
val converter =
35+
MappingMongoConverter(NoOpDbRefResolver.INSTANCE, MongoMappingContext())
36+
37+
val tx = converter.read(SpecialTransaction::class.java, document)
38+
39+
assertThat(tx.id).isEqualTo("baz")
40+
assertThat(tx.type).isEqualTo("SOME_DEFAULT_VALUE")
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.convert
17+
18+
abstract class SomeTransaction() {
19+
20+
abstract val id: String
21+
abstract val type: String
22+
}
23+
24+
data class SpecialTransaction(override val id: String) : SomeTransaction() {
25+
override val type: String = "SOME_DEFAULT_VALUE"
26+
}

0 commit comments

Comments
 (0)