mapstruct / mapstruct/mapstruct-examples
Add example for SolrDocument + Mapstruct
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.4k
- Forks
- 504
- PR merge metrics
- No merged PRs in 30d
Description
It would be nice to have an example that shows how to use SolrDocument with Mapstruct.
I was trying to use MapStruct with SolrDocument and couldn't find a comprehensive example that could guide me . I was facing below issue during compiling:
**Can't generate mapping method from iterable type to non-iterable type.**
Reason is [SolrDocument](https://github.com/apache/lucene-solr/blob/master/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java) implements Iterable interface and Target DTOs doesn't .
So i came up with below solution to deal with this.
**Source Class**
[SolrDocument](https://github.com/apache/lucene-solr/blob/master/solr/solrj/src/java/org/apache/solr/common/SolrDocument.java)
Here SolrDocument internally has Map _fields;
**Destination Class**:
`public class ItemDTO {
private String id;
private String displayName;
private String availability;
private String price;
//getter
//setter
}
}`
**Error : Can't generate mapping method from iterable type to non-iterable type.**
**Solution**:
Created SolrResponseWrapper around SolrDocument like below:
`public class SolrResponseWrapper {
private SolrDocument document;
public SolrResponseWrapper(SolrDocument document) {
this.document = document;
}
public String get(String key) {
return this.document.get(key).toString();
}
}`
**Now Mapper is defined below for SolResponseWrapper to ItemDTO.**
`@Mapper
public interface ItemMapper {
ItemMapper INSTANCE = Mappers.getMapper(ItemMapper);
@Mappings({
@Mapping(expression = "java(record.get(\"product_id\"))", target = "id"),
@Mapping(expression = "java(record.get(\"display_name\"))", target = "displayName"),
@Mapping(expression = "java(record.get(\"availability\"))", target = "availability"),
@Mapping(expression = "java(record.get(\"price\"))", target = "price")
})
ItemDTO convertToItemDTO(SolrResponseWrapper record);
}`
If you think this is a good addition, please let me know. I will raise PR to have it included as an example.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing existing Java example layouts in the repository, then use the issue's SolrDocument, SolrResponseWrapper, ItemDTO, and ItemMapper example as the starting point. Add a focused example showing the wrapper-based mapping and verify that the example demonstrates the reported iterable-to-non-iterable problem and its resolution.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100