spring-projects / spring-projects/spring-data-mongodb

Conversion failed exception from String to bson ObjectId on Mongo calls

Open
#4,018 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: feedback-provided
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Hi,
We are getting following error logged in Dynatrace on all Mongo calls even though app functions normally and we are also unable to see the exception when debugging through IDE:
Exception:
org.springframework.core.convert.ConversionFailedException
Message:
Failed to convert from type [java.lang.String] to type [org.bson.types.ObjectId] for value ''; nested exception is java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: []
Stacktrace:
org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
org.springframework.data.mongodb.core.convert.MongoConverter.convertId(MongoConverter.java:146)
org.springframework.data.mongodb.core.convert.QueryMapper.convertId(QueryMapper.java:636)
org.springframework.data.mongodb.core.convert.QueryMapper.getMappedValue(QueryMapper.java:384)
org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObjectForField(QueryMapper.java:243)
org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:152)
org.springframework.data.mongodb.core.convert.QueryMapper.getMappedKeyword(QueryMapper.java:285)
org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:124)
org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:2473)
org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:820)
org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:807)
org.springframework.data.mongodb.core.MongoTemplate$$FastClassBySpringCGLIB$$5a5a6b8.invoke()
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
com.company.config.MongoTemplateWithMetrics$$EnhancerBySpringCGLIB$$.findOne
com.company.dao.connect.AppointmentDAOConnect.findByParameterAppointment(AppointmentDAOConnect.java:689)
com.company.dao.connect.AppointmentDAOConnect$$FastClassBySpringCGLIB$$40444430.invoke()
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
com.company.dao.connect.AppointmentDAOConnect$$EnhancerBySpringCGLIB$$.findByParameterAppointment

How can I go about debugging the issue?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing MongoConverter.convertId and QueryMapper from the reported MongoTemplate.findOne call at AppointmentDAOConnect.java:689. Determine which query value is the invalid ObjectId string and whether it can be reproduced from the reported call; done means identifying the source of the value and a concrete corrective action.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb, spring
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.