diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java index c76020c03..2e83bb1f9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java @@ -115,7 +115,7 @@ class SpringDataMongodbSerializer extends MongodbDocumentSerializer { return asReference(constant, null); } - protected DBRef asReference(Object constant, Path path) { + protected DBRef asReference(Object constant, @Nullable Path path) { return converter.toDBRef(constant, getPropertyForPotentialDbRef(path)); } @@ -135,7 +135,7 @@ class SpringDataMongodbSerializer extends MongodbDocumentSerializer { MongoPersistentProperty property = getPropertyFor(path); - return property.isIdProperty() ? key.replaceAll("." + ID_KEY + "$", "") : key; + return property != null && property.isIdProperty() ? key.replaceAll("." + ID_KEY + "$", "") : key; } @Override @@ -144,20 +144,26 @@ class SpringDataMongodbSerializer extends MongodbDocumentSerializer { return propertyFor == null ? super.isId(arg) : propertyFor.isIdProperty(); } + @Override + @Nullable protected Object convert(@Nullable Path path, @Nullable Constant constant) { + if (constant == null) { + return null; + } + if (!isReference(path)) { MongoPersistentProperty property = getPropertyFor(path); - if(property == null) { + if (property == null) { return super.convert(path, constant); } - if(property.isIdProperty()) { + if (property.isIdProperty()) { return mapper.convertId(constant.getConstant(), property.getFieldType()); } - if(property.hasExplicitWriteTarget()) { + if (property.hasExplicitWriteTarget()) { return converter.convertToMongoType(constant.getConstant(), TypeInformation.of(property.getFieldType())); } @@ -166,17 +172,19 @@ class SpringDataMongodbSerializer extends MongodbDocumentSerializer { MongoPersistentProperty property = getPropertyFor(path); - if (property.isDocumentReference()) { - return converter.toDocumentPointer(constant.getConstant(), property).getPointer(); - } + if (property != null) { + if (property.isDocumentReference()) { + return converter.toDocumentPointer(constant.getConstant(), property).getPointer(); + } - if (property.isIdProperty()) { + if (property.isIdProperty()) { - MongoPersistentProperty propertyForPotentialDbRef = getPropertyForPotentialDbRef(path); - if (propertyForPotentialDbRef != null && propertyForPotentialDbRef.isDocumentReference()) { - return converter.toDocumentPointer(constant.getConstant(), propertyForPotentialDbRef).getPointer(); + MongoPersistentProperty propertyForPotentialDbRef = getPropertyForPotentialDbRef(path); + if (propertyForPotentialDbRef != null && propertyForPotentialDbRef.isDocumentReference()) { + return converter.toDocumentPointer(constant.getConstant(), propertyForPotentialDbRef).getPointer(); + } + return asReference(constant.getConstant(), path.getMetadata().getParent()); } - return asReference(constant.getConstant(), path.getMetadata().getParent()); } return asReference(constant.getConstant(), path); @@ -203,7 +211,8 @@ class SpringDataMongodbSerializer extends MongodbDocumentSerializer { * @param path * @return */ - private MongoPersistentProperty getPropertyForPotentialDbRef(Path path) { + @Nullable + private MongoPersistentProperty getPropertyForPotentialDbRef(@Nullable Path path) { if (path == null) { return null;