|
19 | 19 | import java.util.Map.Entry; |
20 | 20 | import java.util.regex.Matcher; |
21 | 21 | import java.util.regex.Pattern; |
| 22 | +import java.util.stream.Collectors; |
22 | 23 |
|
23 | 24 | import org.bson.BsonValue; |
24 | 25 | import org.bson.Document; |
25 | 26 | import org.bson.conversions.Bson; |
26 | 27 | import org.bson.types.ObjectId; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
27 | 30 | import org.springframework.core.convert.ConversionService; |
28 | 31 | import org.springframework.core.convert.converter.Converter; |
29 | 32 | import org.springframework.data.domain.Example; |
|
68 | 71 | */ |
69 | 72 | public class QueryMapper { |
70 | 73 |
|
| 74 | + protected static final Logger LOGGER = LoggerFactory.getLogger(QueryMapper.class); |
| 75 | + |
71 | 76 | private static final List<String> DEFAULT_ID_NAMES = Arrays.asList("id", "_id"); |
72 | 77 | private static final Document META_TEXT_SCORE = new Document("$meta", "textScore"); |
73 | 78 | static final ClassTypeInformation<?> NESTED_DOCUMENT = ClassTypeInformation.from(NestedDocument.class); |
@@ -1173,38 +1178,55 @@ private PersistentPropertyPath<MongoPersistentProperty> getPath(String pathExpre |
1173 | 1178 | removePlaceholders(DOT_POSITIONAL_PATTERN, pathExpression)); |
1174 | 1179 |
|
1175 | 1180 | if (sourceProperty != null && sourceProperty.getOwner().equals(entity)) { |
1176 | | - return mappingContext |
1177 | | - .getPersistentPropertyPath(PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation())); |
| 1181 | + return mappingContext.getPersistentPropertyPath( |
| 1182 | + PropertyPath.from(Pattern.quote(sourceProperty.getName()), entity.getTypeInformation())); |
1178 | 1183 | } |
1179 | 1184 |
|
1180 | 1185 | PropertyPath path = forName(rawPath); |
1181 | 1186 | if (path == null || isPathToJavaLangClassProperty(path)) { |
1182 | 1187 | return null; |
1183 | 1188 | } |
1184 | 1189 |
|
1185 | | - try { |
| 1190 | + PersistentPropertyPath<MongoPersistentProperty> propertyPath = tryToResolvePersistentPropertyPath(path); |
1186 | 1191 |
|
1187 | | - PersistentPropertyPath<MongoPersistentProperty> propertyPath = mappingContext.getPersistentPropertyPath(path); |
| 1192 | + if (propertyPath == null) { |
1188 | 1193 |
|
1189 | | - Iterator<MongoPersistentProperty> iterator = propertyPath.iterator(); |
1190 | | - boolean associationDetected = false; |
| 1194 | + if (QueryMapper.LOGGER.isInfoEnabled()) { |
| 1195 | + |
| 1196 | + String types = StringUtils.collectionToDelimitedString( |
| 1197 | + path.stream().map(it -> it.getType().getSimpleName()).collect(Collectors.toList()), " -> "); |
| 1198 | + QueryMapper.LOGGER.info( |
| 1199 | + "Could not map '{}'. Maybe a fragment in '{}' is considered a simple type. Mapper continues with {}.", |
| 1200 | + path, types, pathExpression); |
| 1201 | + } |
| 1202 | + return null; |
| 1203 | + } |
1191 | 1204 |
|
1192 | | - while (iterator.hasNext()) { |
| 1205 | + Iterator<MongoPersistentProperty> iterator = propertyPath.iterator(); |
| 1206 | + boolean associationDetected = false; |
1193 | 1207 |
|
1194 | | - MongoPersistentProperty property = iterator.next(); |
| 1208 | + while (iterator.hasNext()) { |
1195 | 1209 |
|
1196 | | - if (property.isAssociation()) { |
1197 | | - associationDetected = true; |
1198 | | - continue; |
1199 | | - } |
| 1210 | + MongoPersistentProperty property = iterator.next(); |
1200 | 1211 |
|
1201 | | - if (associationDetected && !property.isIdProperty()) { |
1202 | | - throw new MappingException(String.format(INVALID_ASSOCIATION_REFERENCE, pathExpression)); |
1203 | | - } |
| 1212 | + if (property.isAssociation()) { |
| 1213 | + associationDetected = true; |
| 1214 | + continue; |
1204 | 1215 | } |
1205 | 1216 |
|
1206 | | - return propertyPath; |
1207 | | - } catch (InvalidPersistentPropertyPath e) { |
| 1217 | + if (associationDetected && !property.isIdProperty()) { |
| 1218 | + throw new MappingException(String.format(INVALID_ASSOCIATION_REFERENCE, pathExpression)); |
| 1219 | + } |
| 1220 | + } |
| 1221 | + |
| 1222 | + return propertyPath; |
| 1223 | + } |
| 1224 | + |
| 1225 | + private PersistentPropertyPath<MongoPersistentProperty> tryToResolvePersistentPropertyPath(PropertyPath path) { |
| 1226 | + |
| 1227 | + try { |
| 1228 | + return mappingContext.getPersistentPropertyPath(path); |
| 1229 | + } catch (MappingException e) { |
1208 | 1230 | return null; |
1209 | 1231 | } |
1210 | 1232 | } |
|
0 commit comments