jmapper-framework / jmapper-core

Elegance, high performance and robustness all in one java bean mapper
http://jmapper-framework.github.io/jmapper-core
Apache License 2.0
227 stars 41 forks source link

[Question] Nested object mapping/conversion not working #95

Closed zhenyabodukhin closed 3 years ago

zhenyabodukhin commented 3 years ago

I have a problem. As you see below, I have tried to map Classes with Jmap Annotations, all working ok, except conversion method for List vehicleType field in destination class. Jmapper do not call this method and I get this field null. All required fields in Source.class have values. https://stackoverflow.com/questions/66273658/jmapper-conversoin-of-nested-fields-not-working

`public class Destination {

@JMap("vehicleVariantDetails")
protected VehicleVariantDetailsType vehicleVariantDetails;

@JMapConversion(from = {"vehicleVariantDetails"}, to = {"vehicleVariantDetails"})
public VehicleVariantDetailsType conversion(List<VehicleVariantDetails> list) {
    if(!list.isEmpty()) {
        JMapper<VehicleVariantDetailsType, VehicleVariantDetails> mapper = new JMapper<>(
                VehicleVariantDetailsType.class, VehicleVariantDetails.class);
        return mapper.getDestination(list.get(0));
    }
    return null;
}

}

public class VehicleVariantDetailsType {

@JMap("vehicleRunningGearDetails")
protected VehicleRunningGearDetailsType vehicleRunningGearDetails;

}

public class VehicleRunningGearDetailsType {

@JMap("vehicleAxleDetails")
protected List<VehicleAxleDetailsType> vehicleAxleDetails;

@JMapConversion(from = {"vehicleAxleDetails"}, to = {"vehicleAxleDetails"})
public List<VehicleAxleDetailsType> conversion(List<VehicleAxleDetails> source) {
    List<VehicleAxleDetailsType> target = new ArrayList<>();

    for(VehicleAxleDetails axle : source) {
        JMapper<VehicleAxleDetailsType, VehicleAxleDetails> mapper = new JMapper<>(
                VehicleAxleDetailsType.class, VehicleAxleDetails.class);

        VehicleAxleDetailsType dest = mapper.getDestination(axle);
        target.add(dest);
    }
    return target;
}

}

public class VehicleAxleDetailsType {

@JMap(value = "vehicleAxleDetails", classes = VehicleRunningGearDetails.class) protected List vehicleTyre;

@JMapConversion(from = {"vehicleAxleDetails"}, to = {"vehicleTyre"}) public List conversion2(List sourceList) { List target = new ArrayList<>(); VehicleAxleDetails source = null; if(!sourceList.isEmpty()) { source = sourceList.get(0); } if(source!=null) { VehicleTyreType dest = new VehicleTyreType(); dest.setVehicleTyreKindCode(source.getVehicleTyreKindCode()); dest.setVehicleTyreKindSize(source.getVehicleTyre().getVehicleTyreKindSize()); target.add(dest); } return target; } }

public class VehicleTyreType {

protected String vehicleTyreKindCode; protected String vehicleTyreKindSize; }

public class Source {

private List vehicleVariantDetails; }

public class VehicleVariantDetails {

private VehicleRunningGearDetails vehicleRunningGearDetails; }

public class VehicleRunningGearDetails {

private List vehicleAxleDetails; }

public class VehicleAxleDetails {

private String vehicleTyreKindCode; private VehicleTyre vehicleTyre; }

public class VehicleTyre {

private String vehicleTyreKindSize; }`

joshone commented 3 years ago

i use the JMap annotation and JMapAccessor when the list's name is larger, all the class attributes that you need should be tagged

like this

` public class ArrayOfSomeClass{ @JMapAccessor(name = "aicr0P01V01S03", get = "getAICR0P01V01S03", set = "setAICR0P01V01S03") @JMap List aicr0P01V01S03;

public List<AICR0P01V01S03> getAICR0P01V01S03(){
       return aicr0P01V01S03;
}

public void setAICR0P01V01S03(List<AICR0P01V01S03> aicr0P01V01S03){
       this.aicr0P01V01S03 = aicr0P01V01S03;
}

} `

` public class AICR0P01V01S03 {

@JMap private String codigoRetorno;
@JMap private Long cantidadRegistros;
@JMap private String valorConsultado;

/*
 constructor
 getter and setter
**/

} `

zhenyabodukhin commented 3 years ago

Thx for your comment, i will try add this. But my other List fields working ok without this annotation, i think, the problem is that JMapper do not use my method annotated with JMapConversion(@JMapConversion(from = {"vehicleAxleDetails"}, to = {"vehicleTyre"})), where i try to map some fields

joshone commented 3 years ago

i never used JMapConversoin, try using the JMapAccessor, this work when the list object has a larger name as VehicleAxleDetailsType