Skip to content

Support Iterable interface as an input #3376

@SergeS

Description

@SergeS

Use case

We are using records with collections defined as Iterable<T> , and it would be handy to somehow make mapstruct able to convert from Iterable<T> to any other Iterable type. For example.

record B (
  String value;
) {}

record A (
  Iterable<B> listOfB
) {}

class C {
  List<String> listOfB
}

@Mapper
interface Mapper {
  C mapAtoC(A input);

  default String mapBToSting(B in) {
    return in.value();
  }
}

Generated Code

interface MapperImpl {
  public C mapAtoC(A input) {
    ....

    C c = new C();
    
    List<String> listOfB = StreamSupport.stream(labels.spliterator(), false).map(this::mapBToSting).toList();
    c.setListOfB(listOfB);

    ....
  }
}

Possible workarounds

Currently I need to create such functions manually

MapStruct Version

1.5.5.Final

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions