Skip to content

<foreach> does not properly close IN-clause if the collection is empty. #415

@gitolathe

Description

@gitolathe

The <foreach> tag changed behavior between version 3.2.7 and 3.2.8. Having a <select> like below in a mapper file:

<select id="getReports" parameterType="list" resultMap="reportResultMap">
    select * from REPORTS where reportId IN
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item.id}
    </foreach>
</select>

the IN-clause is rendered invalid with version 3.2.8 but works just fine with 3.2.7.

It seems quite likely that the changed behavior is caused by the fix for #195 which issued the if-statement

    if (!iterable.iterator().hasNext()) {
      return true;
    }

to be added to ForEachSqlNode.apply() in commit abf2ef3616 at row 55.

3.2.7
The IN clause is properly closed with an empty collection:

select * from REPORTS where REPORTID IN ( ) 

which is valid SQL.

3.2.8
The open and close attributes are not applied thus the SQL is not valid since there are no opening and ending parenthesis following the IN:

### Error querying database.  Cause: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT * FROM REPORTS WHERE REPORTID IN [*]"; expected "("; SQL statement:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions