-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
Lenient property name casing (beyond standard JavaBeans conventions) [SPR-6491] #11157
Comments
Juergen Hoeller commented This is unfortunately exactly what the JavaBeans conventions say. See java.beans.Introspector.decapitalize and its javadoc: [quote]This normally means converting the first character from upper case to lower case, but in the (unusual) special case when there is more than one character and both the first and second characters are upper case, we leave it alone. Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".[/quote] We are simply calling the standard JavaBeans Introspector which detects properties that way. We can of course consider being more lenient here through custom fallback code after the Introspector did its job. Juergen |
Stefan Schmidt commented Hi Juergen, I have done some more tests and there is still a case in the script above that does produce the NotReadablePropertyException. The Introspector.decapitalize() method handles the fields as follows:
However, defining a form field for the nInStock field like so:
.. fails with: org.springframework.beans.NotReadablePropertyException: Invalid property 'nInStock' of bean class [com.springsource.demo.library.domain.Book]: Bean property 'nInStock' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? The nInStock field is capitalized correctly as per the Introspector.decapitalize method, however Spring MVC data binding still fails... |
Stefan Schmidt commented So it appears I have to check for the method name and not the field name itself. In that case it would be NInStock when stripping off the get/set and therefore the 'special' case rule applies as described in Introspector.decapitalize()... |
Juergen Hoeller commented Yes it's always the method name that counts. The JavaBeans Introspector doesn't even look at the fields; it just matches getter and setter methods against each other. I have actually committed a more lenient lookup mechanism there when I marked this issue as 'resolved'. So Spring 3.0 trunk should already accept any such match, even if the first letter's casing doesn't follow the JavaBeans conventions. Juergen |
Stefan Schmidt commented Thanks for making this a little more lenient, I am sure most MVC users are not aware of the exact conventions used here so your adjustment will help them. In the meantime I have adjusted my code to comply with the Introspector.decapitalize reules against method names instead of field names so I should be completely compliant now ;). -Stefan |
Stefan Schmidt opened SPR-6491 and commented
Problems arise when using MVC form-objects are bound through the data binder when the fields use 'unusual' capitalizations:
Only the following representation of these fields in a MVC form works:
Note, the Title needs to be lowercased whereas ISBN needs to remain uppercase (ie iSBN raises a NotReadablePropertyException). Same for the nInStock field which needs to be uppercased (first letter) and nInStock would raise a NotReadablePropertyException).
This seems to be inconsistent w/r to the conventions used here. Why is title handled different to the other two? Also, why can't we allow a convention where title, iSBN and nInStock all work assuming that the get methods follow JavaBean conventions with correct accessors and mutators as shown above? The data binder would just uppercase the first letter of the form field and prepend get and set accordingly.
Maybe I am missing a pattern here but this seems to be handled very inconsistently.
Affects: 3.0 RC3
Issue Links:
Referenced from: commits 388edd7
The text was updated successfully, but these errors were encountered: