Add potential fix

This commit is contained in:
Filip Hrisafov 2021-01-23 17:44:42 +01:00
parent 5aa2ca9637
commit 716d6c5f12
3 changed files with 71 additions and 9 deletions

View File

@ -262,10 +262,10 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
return null;
}
if ( !mappingReferences.isRestrictToDefinedMappings() ) {
// apply name based mapping from a source reference
applyTargetThisMapping();
// apply name based mapping from a source reference
applyTargetThisMapping();
if ( !mappingReferences.isRestrictToDefinedMappings() ) {
// map properties without a mapping
applyPropertyNameBasedMapping();

View File

@ -230,7 +230,11 @@ public class NestedTargetPropertyMappingHolder {
// do update on the defined Mappings.
if ( !groupedSourceReferences.nonNested.isEmpty() ) {
MappingReferences mappingReferences =
new MappingReferences( groupedSourceReferences.nonNested, true );
new MappingReferences(
groupedSourceReferences.nonNested.references,
groupedSourceReferences.nonNested.getTargetThisReferences(),
true
);
SourceReference reference = new SourceReference.BuilderFromProperty()
.sourceParameter( sourceParameter )
.name( targetProperty )
@ -361,8 +365,19 @@ public class NestedTargetPropertyMappingHolder {
Map<String, Set<MappingReference>> singleTargetReferences = new LinkedHashMap<>();
for ( MappingReference mapping : mappingReferences.getMappingReferences() ) {
TargetReference targetReference = mapping.getTargetReference();
SourceReference sourceReference = mapping.getSourceReference();
String property = first( targetReference.getPropertyEntries() );
MappingReference newMapping = mapping.popTargetReference();
if ( newMapping == null && method.getSourceParameters().size() == 1 && sourceReference != null && sourceReference.getParameter() != null &&
sourceReference.getPropertyEntries().isEmpty() &&
!targetReference.getPropertyEntries().isEmpty() ) {
List<String> newPathProperties = new ArrayList<>( targetReference.getPathProperties() );
newPathProperties.add( targetReference.getPropertyEntries().get( 0 ) );
TargetReference newTargetReference = new TargetReference( null, Collections.singletonList( "." ), newPathProperties);
newMapping = new MappingReference( mapping.getMapping(), newTargetReference, sourceReference );
}
if ( newMapping != null ) {
// group properties on current name.
mappingsKeyedByProperty.computeIfAbsent( property, propertyEntry -> new LinkedHashSet<>() )
@ -526,7 +541,7 @@ public class NestedTargetPropertyMappingHolder {
Map.Entry<Parameter, Set<MappingReference>> entryByParam,
Set<MappingReference> singleTargetReferences) {
Set<MappingReference> mappings = entryByParam.getValue();
Set<MappingReference> nonNested = new LinkedHashSet<>();
NestedReferences nonNested = new NestedReferences();
Set<MappingReference> appliesToAll = new LinkedHashSet<>();
Set<MappingReference> sourceParameterMappings = new LinkedHashSet<>();
// group all mappings based on the top level name before popping
@ -780,12 +795,12 @@ public class NestedTargetPropertyMappingHolder {
private static class GroupedSourceReferences {
private final Map<PropertyEntry, Set<MappingReference>> groupedBySourceReferences;
private final Set<MappingReference> nonNested;
private final NestedReferences nonNested;
private final Set<MappingReference> notProcessedAppliesToAll;
private final Set<MappingReference> sourceParameterMappings;
private GroupedSourceReferences(Map<PropertyEntry, Set<MappingReference>> groupedBySourceReferences,
Set<MappingReference> nonNested, Set<MappingReference> notProcessedAppliesToAll,
NestedReferences nonNested, Set<MappingReference> notProcessedAppliesToAll,
Set<MappingReference> sourceParameterMappings) {
this.groupedBySourceReferences = groupedBySourceReferences;
this.nonNested = nonNested;
@ -801,4 +816,37 @@ public class NestedTargetPropertyMappingHolder {
+ ", sourceParameterMappings=" + sourceParameterMappings + '}';
}
}
private static class NestedReferences {
private final Set<MappingReference> references = new LinkedHashSet<>();
private List<MappingReference> targetThisReferences;
public void add(MappingReference mapping) {
TargetReference targetReference = mapping.getTargetReference();
if ( targetReference != null && targetReference.getPropertyEntries().size() == 1 &&
".".equals( targetReference.getShallowestPropertyName() ) ) {
if ( targetThisReferences == null ) {
targetThisReferences = new ArrayList<>();
}
targetThisReferences.add( mapping );
}
else {
references.add( mapping );
}
}
public void addAll(Set<MappingReference> mappingReferences) {
mappingReferences.forEach( this::add );
}
public boolean isEmpty() {
return references.isEmpty() && (targetThisReferences == null || targetThisReferences.isEmpty());
}
public List<MappingReference> getTargetThisReferences() {
return targetThisReferences != null ? targetThisReferences : Collections.emptyList();
}
}
}

View File

@ -418,11 +418,25 @@ public class SourceReference extends AbstractReference {
public List<SourceReference> push(TypeFactory typeFactory, FormattingMessager messager, Method method ) {
List<SourceReference> result = new ArrayList<>();
PropertyEntry deepestProperty = getDeepestProperty();
String fullName;
Type type;
if ( deepestProperty != null ) {
Type type = deepestProperty.getType();
fullName = deepestProperty.getFullName();
type = deepestProperty.getType();
}
else if ( getParameter() != null ) {
fullName = getParameter().getName();
type = getParameter().getType();
}
else {
fullName = null;
type = null;
}
if ( type != null ) {
Map<String, Accessor> newDeepestReadAccessors = type.getPropertyReadAccessors();
for ( Map.Entry<String, Accessor> newDeepestReadAccessorEntry : newDeepestReadAccessors.entrySet() ) {
String newFullName = deepestProperty.getFullName() + "." + newDeepestReadAccessorEntry.getKey();
String newFullName = fullName + "." + newDeepestReadAccessorEntry.getKey();
SourceReference sourceReference = new BuilderFromMapping()
.sourceName( newFullName )
.method( method )