-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 IndexOutOfBoundsException #3327
base: master
Are you sure you want to change the base?
Fix IndexOutOfBoundsException #3327
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR, @luozongle01 ,
I agree that MyBatis should avoid throwing IndexOutOfBoundsException
, however, calling findUsableConstructorByArgTypes()
could break backward compatibility (type handler is not always registered globally).
Just checking the number of constructor arguments should be enough, am I right?
Also, for the tests, please create a new independent package under the org.apache.ibatis.submitted
and use HSQLDB like the other tests instead of mocking.
p.s.
Personally, I would recommend using the argNameBasedConstructorAutoMapping
if you rely on constructor auto-mapping.
Please see #2196 for the details.
Calling Please make the requested change for the tests. |
Hi @harawata , thank you very much for your review. I will modify the test. However, I found that my changes caused the build test to fail, and then I found some problems that my changes may cause These only appear when the resultType class has only one constructor with parameters.
So I'm wondering if I should make changes similar to the following to keep the previous behavior? private Optional<? extends Constructor<?>> findConstructorForAutomapping(final Class<?> resultType,
ResultSetWrapper rsw) {
Constructor<?>[] constructors = resultType.getDeclaredConstructors();
if (constructors.length == 1) {
int constructorsParameterLength = constructors[0].getParameterTypes().length;
if (constructorsParameterLength > rsw.getJdbcTypes().size()) {
return Optional.empty();
}
if (configuration.isArgNameBasedConstructorAutoMapping() ||
constructorsParameterLength < rsw.getJdbcTypes().size()) {
return Optional.of(constructors[0]);
}
return Optional.of(constructors[0]).filter(x -> findUsableConstructorByArgTypes(x, rsw.getJdbcTypes()));
}
Other unchanged parts...
} |
IMO using lombok with one constructor with all attributes is poorly designed usage, regardless they need the lombok noArgConstructor as using one with all attributes loses that unless directly called which new javadocs and other aspects of jvm is no requiring users be specific and not have auto generated constructors at all. Anyway, open for improvement here but think as noted users are at fault for poor design of their objects. |
Yes, you are right, and the embarrassing thing is that some users do not notice that using the |
Thanks for the reply! |
@harawata |
I think the array sizes can and should be checked in the mybatis-3/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java Lines 783 to 796 in 3e54e62
A few notes:
Please let me know if I am missing something! |
Hi, I have noticed that some people, when using
Lombok
, only have one constructor with all parameters due to the use of@Data
and@Builder
annotations. If the class property of the resultType does not match the query result, anIndexOutOfBoundsException
exception will be thrown.I have had several colleagues who have encountered this issue and spent some time troubleshooting it. Although it is indeed the user's problem, I think perhaps we should optimize the prompt to clearly indicate that this is the cause of the constructor, so that users can quickly find the problem when they encounter it.
Abnormal example: https://github.com/luozongle01/mybatis_index_out_of_bounds