Skip to content
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

Fix ClassCastException of BeanReadPropertyImpl to BeanProperty #11396

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ default <P> BeanWriteProperty<T, P> getRequiredWriteProperty(@NonNull String nam
final BeanReadProperty<T, ?> prop = getReadProperty(name).orElse(null);
if (prop != null && type.isAssignableFrom(prop.getType())) {
//noinspection unchecked
return Optional.of((BeanProperty<T, P>) prop);
return Optional.of((BeanReadProperty<T, P>) prop);
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,59 @@ class Test {
introspection.getProperty("foo").get().type == String.class
}

void "test read property by type is defined by its reader field"() {
given:
def introspection = buildBeanIntrospection('test.Test', '''
package test;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.context.annotation.Executable;
import io.micronaut.core.annotation.Nullable;
import java.util.Optional;

@Introspected(accessKind = {Introspected.AccessKind.METHOD, Introspected.AccessKind.FIELD})
class Test {
@Nullable
String foo;

public Optional<String> getFoo() {
return Optional.ofNullable(foo);
}

}
''')
expect:
introspection.getReadProperty("foo", String.class).isEmpty()
introspection.getReadProperty("foo", Optional.class).get().type == Optional.class

}

void "test read property type is defined by its field"() {
given:
def introspection = buildBeanIntrospection('test.Test', '''
package test;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.context.annotation.Executable;
import io.micronaut.core.annotation.Nullable;
import java.util.Optional;

@Introspected(accessKind = {Introspected.AccessKind.METHOD, Introspected.AccessKind.FIELD})
class Test {
@Nullable
String foo;

public void setFoo(Optional<String> foo) {
this.foo = foo.orElse(null);
}

}
''')
expect:
introspection.getReadProperty("foo", String.class).get().type == String.class
introspection.getReadProperty("foo", Optional.class).isEmpty()
}

void "test optional property type is defined by its setter"() {
given:
def introspection = buildBeanIntrospection('test.Test', '''
Expand Down
Loading