Skip to content

@DocumentReference does not work as a drop-in replacement of @DBRef #3792

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

Closed
ntf opened this issue Aug 30, 2021 · 6 comments
Closed

@DocumentReference does not work as a drop-in replacement of @DBRef #3792

ntf opened this issue Aug 30, 2021 · 6 comments
Labels
in: references DBRef and @DocumentReference status: feedback-provided Feedback has been provided

Comments

@ntf
Copy link

ntf commented Aug 30, 2021

Hi there,

I came across the new feature @DocumentReference as I was working on something that needs support of another new feature TimeSeries .

I was trying to use @DocumentReference on a new field as I want to store ["612cf1ef637b391363f1392e"] instead of the DbRef equivalent ["{ \"$ref\" : \"COLLECTION_NAME\", \"$id\" : \"612cf1ef637b391363f1392e\" }"]

Following the documentation here: https://docs.spring.io/spring-data/mongodb/docs/3.3.0-M2/reference/html/#mapping-usage.document-references

I thought I could use (Kotlin)

@DocumentReference(collection = "COLLECTION_NAME", lookup = "{ '_id' : ?#{#target} }")
var myList: List<MyListItem> = mutableListOf()

as a drop-in replacement of

@DBRef

But unfortunately it doesn't work as I expected.

I tried both MongoRepository and MongoTemplate and both complain about missing a converter from a String to MyListItem.

With the help of a debugger, I think it should be related to this block of code.

https://github.com/spring-projects/spring-data-mongodb/blob/3.3.0-M2/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java#L2068

I was expecting there is a block of code similar to the DbRef that help resolving DocumentReference.

if (source instanceof DBRef) {
    return (S) dbRefConverter.convert(this, (DBRef) source, typeHint);
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 30, 2021
@christophstrobl
Copy link
Member

christophstrobl commented Aug 31, 2021

From reading the description I cannot spot what exactly the issue might be, given that the tests, in MongoTemplateDocumentReferenceTests that should cover this, pass.
Do you have a minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem? This would help us identify the issue.

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 31, 2021
@ntf
Copy link
Author

ntf commented Aug 31, 2021

I crafted this example just now with Spring Initializr.

test-mongo-documentreference.zip

Attached belo MongoDb datatabase view on Intellij IDEA.
image

Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.example.demo.models.Instrument]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322) ~[spring-core-5.3.9.jar:5.3.9]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-5.3.9.jar:5.3.9]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) ~[spring-core-5.3.9.jar:5.3.9]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.doConvert(MappingMongoConverter.java:1679) ~[spring-data-mongodb-3.3.0-M2.jar:3.3.0-M2]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.doConvert(MappingMongoConverter.java:1671) ~[spring-data-mongodb-3.3.0-M2.jar:3.3.0-M2]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:1192) ~[spring-data-mongodb-3.3.0-M2.jar:3.3.0-M2]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:1166) ~[spring-data-mongodb-3.3.0-M2.jar:3.3.0-M2]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$ConversionContext.convert(MappingMongoConverter.java:2068) ~[spring-data-mongodb-3.3.0-M2.jar:3.3.0-M2]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:1276) ~[spring-data-mongodb-3.3.0-M2.jar:3.3.0-M2]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$ConversionContext.convert(MappingMongoConverter.java:2038) ~[spring-data-mongodb-3.3.0-M2.jar:3.3.0-M2]
.....
at com.example.demo.DemoApplication.run(DemoApplication.kt:36) ~[main/:na]

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Aug 31, 2021
@mp911de mp911de added the in: references DBRef and @DocumentReference label Sep 8, 2021
@mp911de mp911de changed the title @DocumentReference does not work as a drop-in replacement of @DBRef @DocumentReference does not work as a drop-in replacement of @DBRef Sep 8, 2021
@christophstrobl
Copy link
Member

christophstrobl commented Sep 28, 2021

Thanks for the sample. There's been an update on constructor argument handling in the recent M3 release that fixes parameter conversion. However the sample reveals another issue in expression evaluation (#3842) that needs to be targeted before RC1.

@ntf
Copy link
Author

ntf commented Sep 28, 2021

You are welcome and glad this helps.

@christophstrobl
Copy link
Member

I think we can close this one because #3842 has been resolved meanwhile. If you're still having trouble please feel free to drop a note and reopen the issue.

@ntf
Copy link
Author

ntf commented Nov 11, 2021

Thanks @christophstrobl , although I don't understand how the change set in #3842 fixed this issue but I will take some time to upgrade and revert if it doesn't fix the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: references DBRef and @DocumentReference status: feedback-provided Feedback has been provided
Projects
None yet
Development

No branches or pull requests

4 participants