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
@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.

@fmbenhassine
Copy link
Contributor

I suppose when this feature goes to a dedicated class, there won't be any mapping static method, right?

That's right. The mapping function would be passed to the MappingItemWriter, just like a Classifier is passed to a Classifier-based item writer.

This won't be a breaking change, so we still have time to include in v6. Looking forward to your update on this. Thanks again for everything you do for Spring Batch! 🙏

@scordio scordio force-pushed the gh-4735-mapping-adapters branch from 6859951 to 7db407e Compare October 17, 2025 10:03
@scordio scordio changed the title Add mapping adapter to ItemWriter Add MappingItemWriter Oct 17, 2025
@scordio scordio force-pushed the gh-4735-mapping-adapters branch from 7db407e to 1bd5627 Compare October 17, 2025 10:07
Signed-off-by: Stefano Cordio <[email protected]>
@scordio scordio force-pushed the gh-4735-mapping-adapters branch from 1bd5627 to 020d492 Compare October 18, 2025 09:07
@fmbenhassine fmbenhassine added status: feedback-provided Issues for which the feedback requested from the reporter was provided and removed status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter labels Oct 19, 2025
@fmbenhassine
Copy link
Contributor

Thank you for the updates! Rebased and merged as 2621b20.

I realise that this new item writer allows for a kind of "deconstruction pattern" where a big item can be deconstructed into smaller parts (thanks to the mapping function, and the tests are great examples) where each part is written to a different location. This is awesome!

@scordio
Copy link
Contributor Author

scordio commented Oct 20, 2025

@fmbenhassine thanks for merging it! I realized only now that this might deserve an entry in the user guide. If you agree, I would raise another small PR for it.

@scordio scordio deleted the gh-4735-mapping-adapters branch October 20, 2025 09:00
@fmbenhassine
Copy link
Contributor

I was planning to update that as part of the big documentation overhaul for v6, but if you want to open a PR you are welcome!

@scordio
Copy link
Contributor Author

scordio commented Oct 20, 2025

Sure, I can take care of it 🙂

@scordio
Copy link
Contributor Author

scordio commented Oct 20, 2025

I reviewed my use cases to reconfirm these changes would integrate correctly and I realized one more detail a bit late, sorry about it 🙁

There is one case where the existing writer is an ItemStream, implementing open(), update(), and close(). Ideally, MappingItemWriter should also implement ItemStream and check if downstream is an ItemStream too, delegating the corresponding calls if that's the case.

@fmbenhassine do you have any concerns with this? If not, I'd be happy to raise a PR for it (combined with the docs updates).

@fmbenhassine
Copy link
Contributor

State management is not mandatory, we have some decorators that are not restartable. But if you can make this mapping writer an item stream, then yes that would be great!

@fmbenhassine
Copy link
Contributor

@scordio Just FYI about documentation updates, I added a section about this new item writer in the reference docs: 114f0f3

@scordio
Copy link
Contributor Author

scordio commented Oct 22, 2025

Oh, you beat me to it 😄 I just raised #5046, I'll rebase it soon and align it with your changes.

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: feedback-provided Issues for which the feedback requested from the reporter was provided

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use existing ItemWriter with CompositeItemWriter expecting different item types

2 participants