Skip to content

Conversation

scordio
Copy link
Contributor

@scordio scordio commented Jun 14, 2025

@scordio scordio force-pushed the gh-4735-mapping-adapters branch 2 times, most recently from 7e9ea88 to 0870af5 Compare June 15, 2025 13:25
@scordio scordio marked this pull request as ready for review July 19, 2025 14:58
@scordio
Copy link
Contributor Author

scordio commented Jul 19, 2025

@fmbenhassine this is now ready for review.

See #4735 (comment) on why I didn't add the same to ItemReader and ItemProcessor.

@scordio scordio force-pushed the gh-4735-mapping-adapters branch from 0870af5 to 437ea68 Compare July 19, 2025 15:01
Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
@scordio scordio force-pushed the gh-4735-mapping-adapters branch from 768897f to 6859951 Compare September 6, 2025 21:30
@fmbenhassine
Copy link
Contributor

Hi @scordio , this is interesting! Thank you for the feature request and the PR!

Since this is a specific use case, I don't think adding the method to the item writer interface is the best option. I see this as a very similar to the ClassifierCompositeItemWriter but with a mapping function instead of a classifier.

What about adding a MappingCompositeItemWriter that uses a mapping Function in the org.springframework.batch.infrastructure.item.support package (next to other support writers).

Let me know what you think about that.

@fmbenhassine fmbenhassine added pr-for: feature status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter in: infrastructure labels Oct 7, 2025
@scordio
Copy link
Contributor Author

scordio commented Oct 7, 2025

I see this as a very similar to the ClassifierCompositeItemWriter but with a mapping function instead of a classifier.

What about adding a MappingCompositeItemWriter that uses a mapping Function

I think in my use case it would just be a MappingItemWriter, without composition, as the mapping function is specific to each adapted writer.

This could still be combined with a CompositeItemWriter, but not every delegate would have to use it, e.g.:

@Bean
ItemWriter<Person> personItemWriter() {
  return new CompositeItemWriter<>(List.of(
    new MappingItemWriter(Person::name, nameItemWriter), // ItemWriter<String>
    new MappingItemWriter(Person::age, ageItemWriter),   // ItemWriter<Integer>
    new PersonAddressItemWriter()                        // ItemWriter<Person>
  )); 
}

In this example, it's still possible to use an ItemWriter<Person> like PersonAddressItemWriter alongside the other mapped item writers.

Or did you imagine something different?

I suppose when this feature goes to a dedicated class, there won't be any mapping static method, right? I considered it valuable for the readability of the configuration code, but that probably makes sense only if it's acting as a factory method in a "central" place like the ItemWriter interface (which indeed deviates from the rest of Spring Batch).

Summarizing, if you agree, I'd create a MappingItemWriter under org.springframework.batch.infrastructure.item.support, without any factory method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: infrastructure pr-for: feature status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use existing ItemWriter with CompositeItemWriter expecting different item types
2 participants