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

OptionWriter.renderFromMap fails when map key is null [SPR-7024] #11686

Closed
spring-projects-issues opened this issue Mar 23, 2010 · 3 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Shai Yallin opened SPR-7024 and commented

When using a Map as the items values for an OptionWriter-based tag and ommiting the itemValue property (valueProperty is null), OptionWriter still creates BeanWrappers around the map key and values although they are never used.

As a result of that, one is unable to use null as a map key, which may be useful for instance when the map contains enum values as keys and their localized names as values. In this scenario, it may be useful to include null as a map key which represents an empty field value.

affected code:
{{
Object mapKey = entry.getKey();
Object mapValue = entry.getValue();
BeanWrapper mapKeyWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapKey);
BeanWrapper mapValueWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapValue);
Object renderValue = (this.valueProperty != null ? mapKeyWrapper.getPropertyValue(this.valueProperty) :
mapKey.toString());
Object renderLabel = (this.labelProperty != null ? mapValueWrapper.getPropertyValue(this.labelProperty) :
mapValue.toString());
renderOption(tagWriter, mapKey, renderValue, renderLabel);
}}

fix suggestion: only create the wrappers if this.valueProperty != null


Affects: 3.0.1

Referenced from: commits a614430

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

We'll also have to make the mapKey.toString() and mapValue.toString() code null-safe then, since that would be executed instead of the BeanWrapper calls... I suppose you would rather expect null to turn into an empty String there, i.e. not into "null" or the like?

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Shai Yallin commented

Definitely. As I said, my use case is enums as map keys and their localized names (via a message source) as map values. Such a field is bound to an enum field on the model attribute, so an empty string in the option values should be translated to a null enum value. I hope that the property editor already does that...

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I've adapted this for 3.0.2 now. This will be available in tonight's 3.0.2 snapshot - feel free to give it an early try if you chance, and let me know whether it works for you.

Juergen

@spring-projects-issues spring-projects-issues added type: bug A general bug in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0.2 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: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants