Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong ObjectFactory generated when destination object has no args and all args constructor #389

Open
knfs9 opened this issue May 28, 2022 · 1 comment

Comments

@knfs9
Copy link

knfs9 commented May 28, 2022

Source class:

public class Data {
  private List<Entity> entities;

  public List<Entity> getEntities() {
    return entities;
  }

  public void setEntities(List<Entity> entities) {
    this.entities = entities;
  }
}

Destination class:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DataDTO {

  private List<InnerEntity> innerDtoEntities;

}
public class Entity {
  private List<InnerEntity> innerEntities;

  public List<InnerEntity> getInnerEntities() {
    return innerEntities;
  }

  public void setInnerEntities(List<InnerEntity> innerEntities) {
    this.innerEntities = innerEntities;
  }
}
public class InnerEntity {
  private String field;

  public String getField() {
    return field;
  }

  public void setField(String field) {
    this.field = field;
  }
}

Mapping written like this:

mapperFactory.classMap(Data.class, DataDTO.class)
        .field("entities{innerEntities}", "innerDtoEntities")
        .mapNulls(true)
        .mapNullsInReverse(true)
        .byDefault()
        .register();

Generated ObjectFactory method:

public Object create(Object s, ma.glasnost.orika.MappingContext mappingContext) {if(s == null) throw new java.lang.IllegalArgumentException("source object must be not null");if (s instanceof test.Data) {test.Data source = (test.Data) s;
try {

java.util.List arg0 = null; if ( !(((java.util.List)source.getInnerEntities()) == null)) {

java.util.List new_arg0 = ((java.util.List)new java.util.ArrayList()); 

new_arg0.addAll(mapperFacade.mapAsList(((java.util.List)source.getInnerEntities()), ((ma.glasnost.orika.metadata.Type)usedTypes[0]), ((ma.glasnost.orika.metadata.Type)usedTypes[0]), mappingContext)); 
arg0 = new_arg0; 
} else {
 if ( !(arg0 == null)) {
arg0 = null;
};
}return new test.DataDTO(arg0);
} catch (java.lang.Exception e) {

if (e instanceof RuntimeException) {

throw (RuntimeException)e;

} else {
throw new java.lang.RuntimeException("Error while constructing new DataDTO instance", e);
}
}
}return new test.DataDTO();
}

I'm getting Caused by: javassist.compiler.CompileError: getInnerEntities() not found in test.Data

In generated ObjectFactory it tries to getInnerEntities from Data - (java.util.List)source.getInnerEntities(), howevever, InnerEntity dos not belong to Data directly

Not sure if I'm doing the right mapping here and it is not supported by Orika or it's a bug.

@knfs9
Copy link
Author

knfs9 commented May 28, 2022

It's fixed by removing @AllArgsConstructor, so ma.glasnost.orika.impl.DefaultMapperFactory#lookupObjectFactory(ma.glasnost.orika.metadata.Type<T>, ma.glasnost.orika.metadata.Type<S>, ma.glasnost.orika.MappingContext) returns DefaultConstructorObjectFactory when there is only one constructor, instead of generated ObjectFactory.
But still i believe it could be bug in generated ObjectFactory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant