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-projects-issues opened this issue May 18, 2015 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

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-projects-issues
Copy link
Collaborator Author

Michał Sobkiewicz commented

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

@spring-projects-issues
Copy link
Collaborator Author

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-projects-issues
Copy link
Collaborator Author

Michał Sobkiewicz commented

Thank you very much!

Michał

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.2 RC1 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants