spring-batch icon indicating copy to clipboard operation
spring-batch copied to clipboard

Prevent using both, TargetType and FieldSetMapper, during compilation

Open patbaumgartner opened this issue 5 months ago • 1 comments

Having a custom DSL (builder) for FlatFileItemReaderBuilder to prevent using targetType() and fieldSetMapper() at the same time. This should provide compile-time feedback for this breaking change.

To make things more consistent, one should also adjust the JdbcBatchItemWriterBuilder. This would affect collumnMapped() and beanMapped().

Expected Behavior

The following code snippet should not compile.

new FlatFileItemReaderBuilder<Foo>().name("fooReader")
			.resource(getResource("1;2;3"))
			.lineTokenizer(line -> new DefaultFieldSet(line.split(";")))
			.targetType(Foo.class)
			.fieldSetMapper(fieldSet -> new Foo());

Current Behavior

Currently, an IllegalStateException is thrown at runtime.

patbaumgartner avatar Jun 13 '25 14:06 patbaumgartner

I believe it is a very good point.

Regarding the issue with FlatFileItemReaderBuilder, I’d like to ask for your thoughts on a possible direction.

Currently, targetType() and fieldSetMapper() can both be called on the same builder instance, which may lead to runtime errors. To prevent this, I’m considering using a step builder pattern where:

  • After calling targetType(), fieldSetMapper() becomes inaccessible (and vice versa).
  • This would be achieved by defining separate interfaces to represent different builder states.
  • Deprecation period would be needed for targetType() and fieldSetMapper() of FlatFileItemReaderBuilder.

Would this direction make sense?

HyunSangHan avatar Jun 15 '25 18:06 HyunSangHan

Hi! I've created PR #4929 to address this issue using a stage-based DSL pattern that prevents using targetType() and fieldSetMapper() simultaneously.

snowykte0426 avatar Jul 24 '25 05:07 snowykte0426