Skip to content
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

DataBinder should use ConversionService if PropertyEditor cannot produce required type [SPR-13042] #17634

Closed
spring-issuemaster opened this issue May 18, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented May 18, 2015

Michał Sobkiewicz opened SPR-13042 and commented

When there is PropertyEditor registered for String, TypeConverterDelegate uses it to convert from every possible class... to String:
https://github.com/spring-projects/spring-framework/blob/v4.1.6.RELEASE/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java#L164

Thus, it doesn't even try to use custom ConversionService:
https://github.com/spring-projects/spring-framework/blob/v4.1.6.RELEASE/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java#L170

It makes no sense. IMHO, TypeConverterDelegate should use editors registered for types other than String (for instance, where there is PropertyEditor registered for Integer, it is ok to use it for String -> Integer and Integer -> String; however it is not ok to use it to convert MyEntity -> String).

Attached test is based on:
https://github.com/spring-projects/spring-framework/blob/v4.1.6.RELEASE/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/support/RedirectAttributesModelMapTests.java

If you want to show my use case, just let me know.


Affects: 4.1.6

Attachments:

Referenced from: commits 6418b54

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented May 18, 2015

Michał Sobkiewicz commented

Oh! There is one catch. TypeConverterDelegate should still use editor registered for String, but only if converting from String.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented May 18, 2015

Juergen Hoeller commented

This is a bit of a tricky affair since custom String editors can produce a proper result in such a scenario: A custom String editor may produce a String presentation on a setValue call. StringTrimmerEditor is unable to handle such a case but that's hard to generalize.

So for a defensive change, I've added a fallback call to the ConversionService in case the registered editor failed to produce a result of the desired type (that is, when a setValue / getValue sequence just returned the original value). This makes sense in general when we haven't tried a ConversionService call initially simply because an editor was found: If that editor turns out to be inappropriate for the current attempt, we're effectively ignoring it now.

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented May 19, 2015

Michał Sobkiewicz commented

Thank you very much!

Michał

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.