0

how can we ignore the collections in target response.

public class ClassSource{
  private int id;
}


Destination :

 public class ClassDestination {
  private int id;
  @JsonProperty("instructions")
  @Valid
  private List<Instructions> instructions = new ArrayList<>();
}

in Target want to ignore instructions list completely in the result but its returning empty array...how can we ignore the collections.

Output :
 "id": "1245",
 "instructions": []

expecting output like,
Output :
 "id": "1245"


@Mapper(componentModel = "spring" ,  unmappedTargetPolicy = ReportingPolicy.WARN)

 public interface PersonMapper {
  @Mapping(target = "instructions, expression = "java(null)"")
 ClassDestination  map(ClassSource source);
}

attempt2:

public interface PersonMapper {
  @Mapping(target = "instructions, igonore =true")
 ClassDestination  map(ClassSource source);
}
jcrshankar
  • 1,165
  • 8
  • 25
  • 45
  • The reason the list is present but empty, is because it's initialized as an empty `ArrayList` and never changed. See https://stackoverflow.com/a/43353867/1180351 for an answer (the one I expected myself). – Rob Spoor Aug 02 '22 at 19:21
  • Does this answer your question? [Set null to target using mastruct](https://stackoverflow.com/questions/43347620/set-null-to-target-using-mastruct) – Rob Spoor Aug 02 '22 at 19:21
  • sorry i updated my attempts, yes before posting i did tried with expression = "java(null)" but that doesnt worked – jcrshankar Aug 02 '22 at 19:41
  • classDestination .setInstructions( null ); the generated class assigning null but it retuns empty list – jcrshankar Aug 02 '22 at 19:46
  • You can ignore property in MapStrcut by using `ignore = true` in the `@Mapping` annotation, for example `@Mapping(target = "instructions", ignore = true)`. MapStruct will [generate](https://gist.github.com/fpecek/0b3c20a27b9f95f52994acaccf130142) method that doesn't set instructions field. The problem here is that instructions list is always initialized, as Rob Spoor already pointed out, and when MapStruct creating new instance of ClassDestination object, it will have list already initialized. – frenky Aug 02 '22 at 22:20
  • You can try without default initialization, only `private List instructions;` and then add `@JsonInclude(Include.NON_NULL)` on field or if you never want to serialize this field, you can use `@JsonIgnore` on field. – frenky Aug 02 '22 at 22:24

1 Answers1

0

As others suggest to you, you obtain an empty list instead of null because it is initialized as a new ArrayList in the ClassDestination.

If you don't want to remove the initialization from ClassDestination, but still want a null value, maybe you can try:

@Mapper(componentModel = "spring" ,  unmappedTargetPolicy = ReportingPolicy.WARN)
public interface PersonMapper {

    @Mapping(target = "instructions", ignore = true)
    ClassDestination  map(ClassSource source);

    @AfterMapping
    default void setNullValue(ClassSource source, @MappingTarget ClassDestination destination) {        
        destination.setInstructions(null); 
    }

}
Paul Marcelin Bejan
  • 990
  • 2
  • 7
  • 14