validate looking for a String or Long value for id
mulcahy opened this issue · comments
JQueryRemoteValidatorController seems to be expecting a String or Long for the id parameter type. When using MongoDB as the domain object's persistence store it creates an issue as the object type is an org.bson.types.ObjectId. The check in the controller for a String fails and it then tries to convert the id value to a Long, throwing an exception.
Please find the exception below:
2013-08-27 09:50:10,926 [http-bio-8080-exec-9] ERROR StackTrace - Full Stack Trace:
java.lang.NumberFormatException: For input string: "520a5902e4b0ee2a6f3dc4f8"
Thanks for creating this issue.
You are right! JQueryRemoteValidatorController is expecting a String or Long for the id parameter type. May I know do you want the id to convert to String for your scenario?
I think a String will do. Perhaps if the Elvis order was to check for type Long and then default to String otherwise, it might fix this issue.
I (quickly) tested this approach where I switched the default from Long to String.
// def id = GrailsClassUtils.getPropertyType(validatableClass, 'id') == String?params.id:params.id.toLong()
def id = GrailsClassUtils.getPropertyType(validatableClass, 'id') == Long?params.id.toLong():params.id
This seems to work okay for me (it doesn't throw an class cast exception). However, I'm not familiar enough with the code to know if this will adversely affect some other aspect (such as performance).
Since I was looking at the code, I have a follow-on question about the next line:
validatableInstance = validatableClass.get(id)?:validatableClass.newInstance()
Does this create a DB query and retrieve the instance (again)? I would have thought the new incoming params should be associated with the validatableInstance and not the old persisted version. I'm very open to correction on this :-)
Thank you for the prompt response!
Thanks for proposed solution. I incorporated your suggestion to 1.4.7 release that just published.
The persisted version of validatableInstance is needed for unique constraint validation for non-id property.
Hope this clarify.
Thank you for that.