foreach unexpectedly treats List as Map

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

foreach unexpectedly treats List as Map

Josh Littlefield
When upgrading a project from iBATIS 3.0 to MyBatis 3.4.6, we had an unexpected issue. A <foreach> in a Mapper XML file was causing a runtime error.
The issue was because the Collection being iterated was a List<Pair<Integer,Intger>>, where Pair is org.apache.commons.lang3.tuple.Pair, which implements Map.Entry.

We were expecting to get the List items and then refer to #{item.left} and #{item.right}, but we instead got the Map iteration behavior, because we (sort of unknowingly -- old code) were iterating a Collection of Map.Entry objects. This took some time to figure out.
The fix was a simple change to how we use the <foreach>, but the problem is hard to find in complex mappers (this was turned up in automated testing, but that depends on really good test coverage). I wonder if a property on the foreach to force an iterator behavior would help. Even a Map would work with this by using #{item.key} and #{item.value}, and it would make the usage more obvious in the Mapper XML.

Thought I'd ask this list before opening an issue or proposing an enhancement.

Thanks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: foreach unexpectedly treats List as Map

Iwao AVE!
Hi Josh,

I'm sorry about your troubles.
Could you post the mapper method declaration, XML statement and stack trace?

We are trying very hard to keep the number of options minimum and, if I understand correctly, the proposed foreach property couldn't have saved your time spent on figuring out the cause of the error.
So, I think we should look for a way to improve the error message instead.

Regards,
Iwao



On Thu, Dec 13, 2018 at 12:20 AM Josh Littlefield <[hidden email]> wrote:
When upgrading a project from iBATIS 3.0 to MyBatis 3.4.6, we had an unexpected issue. A <foreach> in a Mapper XML file was causing a runtime error.
The issue was because the Collection being iterated was a List<Pair<Integer,Intger>>, where Pair is org.apache.commons.lang3.tuple.Pair, which implements Map.Entry.

We were expecting to get the List items and then refer to #{item.left} and #{item.right}, but we instead got the Map iteration behavior, because we (sort of unknowingly -- old code) were iterating a Collection of Map.Entry objects. This took some time to figure out.
The fix was a simple change to how we use the <foreach>, but the problem is hard to find in complex mappers (this was turned up in automated testing, but that depends on really good test coverage). I wonder if a property on the foreach to force an iterator behavior would help. Even a Map would work with this by using #{item.key} and #{item.value}, and it would make the usage more obvious in the Mapper XML.

Thought I'd ask this list before opening an issue or proposing an enhancement.

Thanks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.