2

In my Micronaut Kotlin project I have two repositories. I am using MapStruct in both of them.

Source Class

@JsonInclude
data class Source(

var id: String,
var no: String,
var value: String,
)

Destination Class

@JsonInclude
data class Destination(

var id: String,
var no: String,
var value: String,
)

Mapper

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
interface SourceMapper {

fun convertToDestination(doc: Source): Destination
}
  • One of them first extracted the values of source class attributes and then passes these values to the parameterised constructor generated by kotlin data class
public Destination convertToDestination(Source doc){
if ( doc = = null ) {
    return null;
}

String id = null;
String no = null;
String value = null;

id = doc.getId();
no = doc.getNo();
value= doc.getValue();
Destination d = new Destination(id, no,value);
}
  • Another one first creates the object then uses setter to assign value which fails at compile time as default constructor is not present for the data class.
  public Destination convertToDestination(Source doc)
  {
    if ( doc == null ) {
        return null;
    } 
    Destination d = new Destination();

  d.setId(doc.getId());
  d.setNo(doc.getNo());
  d.setValue(doc.getValue());
  return d;
}
I have compared both the Gradle files and I am using same version of mapstruct.
```kapt("org.mapstruct:mapstruct-processor:1.5.0.RC1")``` and ```implementation("org.mapstruct:mapstruct:1.5.0.RC1")```

Please help me understand the behaviour and how we can control it.

  • One solution for the build issue is to add default values to every attribute in Kotlin data class but still the behaviour varies in both Microservices. Unsure on how to control that. – Sankalp Bhatt Sep 05 '22 at 08:42

0 Answers0