0

I'm moving some code from Java 11 to Java 17 and I'm having an issue using Mapstruct to convert a class to a record (that was previously a class).

Let's say I have this record

public record SomeRecord(String a, String b, String c) {}

And I have this other class using Lombok annotations:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class SomeClass{
  private String x;
  private String y;
  private String z;
}

Later in my mapping I have a function to convert from SomeClass to SomeRecord

@Mapper(componentModel = "spring")
public interface Mapper {

  @Mapping(target = "c", source = "someClass.z", qualifiedByName = "mapperFunction")
  SomeRecord toSomeRecord(SomeClass someClass);

  @Named("mapperFunction")
  static String someTransformation(String s) {
    //implementation not relevant
  }
}

And I'm getting two compiling errors:

  • First it says that "Unknown property 'c' in result type SomeRecord. Did you mean 'null'?"
  • Secondly it states that there's no "accessible parameterless constructor for SomeRecord".

I did research and found that mapstruct is supporting java records so it's not clear for me why these errors are occurring.

I'm using:

  • mapstruct 1.5.2.Final (latest as of publication date)
  • lombok 1.18.22
  • openjdk 17.0.4

Note: Maybe it is evident that I should also move SomeClass to a record, but for now please assume this is not possible.

Edit: I'm also using Mapstruct + lombok binding and these errors are happening despite.

sant016
  • 195
  • 1
  • 2
  • 14
  • It looks like MapStruct thinks it's dealing with a regular Java bean (with a no-args constructor, getters and setters). Did you regenerate the MapStruct mappers, or are you compiling the code with an old mapper that was originally built when `SomeClass` was still a regular class? – Jesper Aug 19 '22 at 07:40
  • 1
    I believe that the problem is the same as in https://stackoverflow.com/questions/47676369/mapstruct-and-lombok-not-working-together and the `lombok-mapstruct-binding` dependency is missing – Filip Aug 19 '22 at 08:26
  • This mapstruct lombok binding dependency was already added. Version is 0.2.0, which is why I don't think that doesn't resolve the issue – sant016 Aug 19 '22 at 15:45

0 Answers0