Skip to content

Issues when mixing @Arg with and without name #2932

Closed
@dirk-tf

Description

@dirk-tf

i was using simple @arg(column, type). Tried to add name=... to some args that had the same type, to make things more resilient. this broke the mapper.

  1. having name's on just some args of a resultMap doesn't seem supported (would be nice)
  2. since it isn't, can an additional check be added for such situations that throws something better worded about this not being supported?
  3. a broken mapper may actually be created if another constructor matches the args with name. see test case below, that should throw a different exception right now

MyBatis version

4.5.13, current master

Test case or example project

public class PartialArgsTest {

  record Post(String section, Timestamp createdOn, String subject) {
    public Post(String section, Timestamp subject) {
      this(null, null, null); // absurd, but doesn't matter anyway
    }
  }

  interface Mapper {
    @Select("SELECT * FROM post")
    @Arg(column = "SECTION", name = "section", javaType = String.class)
    @Arg(column = "CREATED_ON", javaType = Timestamp.class)
    @Arg(column = "SUBJECT", name = "subject", javaType = String.class)
    List<Post> getPosts();
  }

  @Test()
  void shouldGetDataOrFailOnMapperCreation() throws Exception {
    final Environment environment = new Environment("test", new JdbcTransactionFactory(),
      BaseDataTest.createBlogDataSource());
    final Configuration config = new Configuration(environment);

    assertThrows(BuilderException.class, () -> {
      config.addMapper(Mapper.class); // this should throw, if all or no @Arg's must have names

      // this continuation should not throw if partial names are ok (but actually throws right now):
      var sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
      try (SqlSession session = sqlSessionFactory.openSession()) {
        Mapper mapper = session.getMapper(Mapper.class);
        var result = mapper.getPosts();
      }
    });
  }
}

Metadata

Metadata

Labels

polishingImprove a implementation code or doc without change in current behavior/content🔩 constructor

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions