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

fix convert of Boolean #846 #922

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private boolean flatten(Map<String, String> map, String prefix, String name,
Object value, TypeDescriptor sourceType) {
String key = StringUtils.isEmpty(prefix) ? name : prefix + "." + name;
if (value == null) {
String resetKey = "_" + key;
String resetKey = determineResetKey(key, sourceType);
map.put(resetKey, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be as follows.
Because map.put(resetKey, ""); and formattingBoolean#map.put(name, ""); has the same role.
It Makes easier to understand with WebDataBinder#getEmptyValue.

String key = StringUtils.isEmpty(prefix) ? name : prefix + "." + name;
if (value == null) {
  String resetKey =   determineResetKey(key, sourceType);
  map.put(resetKey, "");
  return true;
}
// omit

// Determine whether to convert null value to field marker.
private String determineResetKey(String key, TypeDescriptor sourceType) {
  // Should not convert Boolean null value to field marker.
  // WebDataBinder bind boolean & Boolean field marker as same false value.
  if (Boolean.class == sourceType.getType()) {
    return key;
  }
  return "_" + key;
}

How do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you.

Only null check for sourceType is added to the implementation.
5556457

// the value has been flatten
return true;
Expand Down Expand Up @@ -323,6 +323,24 @@ private boolean flatten(Map<String, String> map, String prefix, String name,
return true;
}

/**
* Determine whether to convert null value to field marker.
* <p>
* Should not convert Boolean null value to field marker.<br>
* {@link org.springframework.web.bind.WebDataBinder} bind boolean & Boolean field marker as same false value.
* @param key Property name with prefix
* @param sourceType {@link TypeDescriptor} to use
* @return ResetKey
*/
private String determineResetKey(String key, TypeDescriptor sourceType) {
if (sourceType != null) {
if (Boolean.class == sourceType.getType()) {
return key;
}
}
return "_" + key;
}

/**
* Convert any array to {@code List<Object>}
* @param arrayObject array to convert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ public void test10_propertiesIsEmptyElement() {
assertThat(map, hasEntry("nestedForm.array", ""));
}

@Test
public void testConvert11_Boolean() {
BooleanForm11 form = new BooleanForm11(true, false, null);
Map<String, String> map = converter.convert(form);

assertThat(map.size(), is(3));
assertThat(map, hasEntry("bool1", "true"));
assertThat(map, hasEntry("bool2", "false"));
assertThat(map, hasEntry("bool3", ""));
}

public static class SearchUserForm0 {
private String name;

Expand Down Expand Up @@ -1086,4 +1097,43 @@ public void setArray(int[] array) {
}
}

public static class BooleanForm11 {
private Boolean bool1;

private Boolean bool2;

private Boolean bool3;

public BooleanForm11(Boolean bool1, Boolean bool2, Boolean bool3) {
this.bool1 = bool1;
this.bool2 = bool2;
this.bool3 = bool3;
}

public Boolean getBool1() {
return bool1;
}

public void setBool1(Boolean bool1) {
this.bool1 = bool1;
}

public Boolean getBool2() {
return bool2;
}

public void setBool2(Boolean bool2) {
this.bool2 = bool2;
}

public Boolean getBool3() {
return bool3;
}

public void setBool3(Boolean bool3) {
this.bool3 = bool3;
}

}

}