Skip to content

Fix property lookup for projections on Kotlin types. #3129

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-3127-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author Oliver Gierke
* @author Mark Paluch
* @author Johannes Englmeier
* @author Christoph Strobl
* @since 1.10
*/
class PropertyAccessingMethodInterceptor implements MethodInterceptor {
Expand All @@ -54,12 +55,11 @@ public PropertyAccessingMethodInterceptor(Object target) {
@Override
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {

Method method = invocation.getMethod();

if (ReflectionUtils.isObjectMethod(method)) {
if (ReflectionUtils.isObjectMethod(invocation.getMethod())) {
return invocation.proceed();
}

Method method = lookupTargetMethod(invocation, target.getWrappedClass());
PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method);

if (descriptor == null) {
Expand All @@ -81,4 +81,12 @@ public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) thro
private static boolean isSetterMethod(Method method, PropertyDescriptor descriptor) {
return method.equals(descriptor.getWriteMethod());
}

private static Method lookupTargetMethod(MethodInvocation invocation, Class<?> targetType) {

Method method = BeanUtils.findMethod(targetType, invocation.getMethod().getName(),
invocation.getMethod().getParameterTypes());

return method != null ? method : invocation.getMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
*/
@ExtendWith(MockitoExtension.class)
class PropertyAccessingMethodInterceptorUnitTests {
Expand Down Expand Up @@ -111,6 +112,15 @@ void throwsAppropriateExceptionIfThePropertyCannotWritten() throws Throwable {
.isThrownBy(() -> new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation));
}

@Test // GH-3127
void detectsKotlinPropertiesWithLeadingIsOnTargetType() throws Throwable {

var source = new WithIsNamedProperty(true);
when(invocation.getMethod()).thenReturn(WithIsNamedPropertyProjection.class.getMethod("isValid"));

assertThat(new PropertyAccessingMethodInterceptor(source).invoke(invocation)).isEqualTo(true);
}

static class Source {

String firstname;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2024 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.projection

interface WithIsNamedPropertyProjection {
val isValid: Boolean
}

class WithIsNamedProperty(val isValid : Boolean)
Loading