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

Support Element.setProperty for lists and maps #8587

Merged
merged 1 commit into from
Jun 25, 2020
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
59 changes: 55 additions & 4 deletions flow-server/src/main/java/com/vaadin/flow/dom/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -67,6 +68,8 @@ public class Element extends Node<Element> {

static final String ATTRIBUTE_NAME_CANNOT_BE_NULL = "The attribute name cannot be null";

private static final String USE_SET_PROPERTY_WITH_JSON_NULL = "setProperty(name, Json.createNull()) must be used to set a property to null";

// Can't set $name as a property, use $replacement instead.
private static final Map<String, String> illegalPropertyReplacements = new HashMap<>();

Expand Down Expand Up @@ -658,8 +661,7 @@ public Element setProperty(String name, double value) {
// Distinct name so setProperty("foo", null) is not ambiguous
public Element setPropertyJson(String name, JsonValue value) {
if (value == null) {
throw new IllegalArgumentException(
"Json.createNull() must be used instead of null for JSON values");
throw new IllegalArgumentException(USE_SET_PROPERTY_WITH_JSON_NULL);
}

setRawProperty(name, value);
Expand All @@ -684,12 +686,61 @@ public Element setPropertyJson(String name, JsonValue value) {
// Distinct name so setProperty("foo", null) is not ambiguous
public Element setPropertyBean(String name, Object value) {
if (value == null) {
throw new IllegalArgumentException(
"setProperty(name, Json.createNull()) must be used to set a property to null");
throw new IllegalArgumentException(USE_SET_PROPERTY_WITH_JSON_NULL);
joheriks marked this conversation as resolved.
Show resolved Hide resolved
}
return setPropertyJson(name, JsonUtils.beanToJson(value));
}

/**
* Sets the given property to the given list of beans or primitive values,
* converted to a JSON array.
* <p>
* Note that properties changed on the server are updated on the client but
* changes made on the client side are not reflected back to the server
* unless configured using
* {@link #addPropertyChangeListener(String, String, PropertyChangeListener)}
* or {@link DomListenerRegistration#synchronizeProperty(String)}.
*
* @param <T>
* the type of items in the list
* @param name
* the property name, not <code>null</code>
* @param value
* the property value, not <code>null</code>
* @return this element
*/
public <T> Element setPropertyList(String name, List<T> value) {
if (value == null) {
throw new IllegalArgumentException(USE_SET_PROPERTY_WITH_JSON_NULL);
}

return setPropertyJson(name, JsonUtils.listToJson(value));
}

/**
* Sets the given property to the given map of beans or primitive values,
* converted to a JSON object.
* <p>
* Note that properties changed on the server are updated on the client but
* changes made on the client side are not reflected back to the server
* unless configured using
* {@link #addPropertyChangeListener(String, String, PropertyChangeListener)}
* or {@link DomListenerRegistration#synchronizeProperty(String)}.
*
* @param name
* the property name, not <code>null</code>
* @param value
* the property value, not <code>null</code>
* @return this element
*/
public Element setPropertyMap(String name, Map<String, ?> value) {
if (value == null) {
throw new IllegalArgumentException(USE_SET_PROPERTY_WITH_JSON_NULL);
}

return setPropertyJson(name, JsonUtils.mapToJson(value));
}

/**
* Adds a property change listener which is triggered when the property's
* value is updated on the server side.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.AbstractList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -48,6 +49,8 @@
*/
public final class JsonUtils {

private static final String CANNOT_CONVERT_NULL_TO_A_JSON_OBJECT = "Cannot convert null to JSON";

private static final ObjectMapper objectMapper = new ObjectMapper();

/**
Expand Down Expand Up @@ -290,12 +293,44 @@ public static <T> JsonObject createObject(Map<String, T> map,
* @return a JSON representation of the bean
*/
public static JsonObject beanToJson(Object bean) {
Objects.requireNonNull(bean, "Cannot convert null to a JSON object");
Objects.requireNonNull(bean, CANNOT_CONVERT_NULL_TO_A_JSON_OBJECT);

try {
return Json.parse(objectMapper.writeValueAsString(bean));
} catch (JsonProcessingException e) {
throw new RuntimeException("Error converting bean to JSON", e);
Copy link
Collaborator

Choose a reason for hiding this comment

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

MAJOR Define and throw a dedicated exception instead of using a generic one. rule

}
}

/**
* Converts the given list to JSON.
*
* @param list
* the list to convert, not {@code null}
* @return a JSON representation of the bean
*/
public static JsonArray listToJson(List<?> list) {
Objects.requireNonNull(list, CANNOT_CONVERT_NULL_TO_A_JSON_OBJECT);
try {
return Json.instance().parse(objectMapper.writeValueAsString(list));
} catch (JsonProcessingException e) {
throw new RuntimeException("Error converting list to JSON", e);
Copy link
Collaborator

Choose a reason for hiding this comment

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

MAJOR Define and throw a dedicated exception instead of using a generic one. rule

}
}

/**
* Converts the given map to JSON.
*
* @param map
* the map to convert, not {@code null}
* @return a JSON representation of the bean
*/
public static JsonObject mapToJson(Map<String, ?> map) {
Objects.requireNonNull(map, CANNOT_CONVERT_NULL_TO_A_JSON_OBJECT);
try {
return Json.instance().parse(objectMapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
throw new RuntimeException("Error converting map to JSON", e);
Copy link
Collaborator

Choose a reason for hiding this comment

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

MAJOR Define and throw a dedicated exception instead of using a generic one. rule

}
}
}
27 changes: 27 additions & 0 deletions flow-server/src/test/java/com/vaadin/flow/dom/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -50,6 +52,7 @@
import com.vaadin.tests.util.TestUtil;

import elemental.json.Json;
import elemental.json.JsonArray;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
import elemental.json.impl.JreJsonObject;
Expand Down Expand Up @@ -573,6 +576,30 @@ public void propertyRawValues() {
Assert.assertEquals(1.0, json.getNumber("number"), 0.0);
Assert.assertEquals(2.3, json.getNumber("flt"), 0.0);
Assert.assertEquals(4.56, json.getNumber("dbl"), 0.0);

List<SimpleBean> list = new ArrayList<>();
SimpleBean bean1 = new SimpleBean();
bean1.string = "bean1";
SimpleBean bean2 = new SimpleBean();
bean2.string = "bean2";
list.add(bean1);
list.add(bean2);
element.setPropertyList("p", list);
JsonArray jsonArray = (JsonArray) element.getPropertyRaw("p");
Assert.assertEquals("bean1",
jsonArray.getObject(0).getString("string"));
Assert.assertEquals("bean2",
jsonArray.getObject(1).getString("string"));

Map<String, SimpleBean> map = new HashMap<>();
map.put("one", bean1);
map.put("two", bean2);
element.setPropertyMap("p", map);
JsonObject jsonObject = (JsonObject) element.getPropertyRaw("p");
Assert.assertEquals("bean1",
jsonObject.getObject("one").getString("string"));
Assert.assertEquals("bean2",
jsonObject.getObject("two").getString("string"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,35 @@ public void beanWithListAndMap() {
((JsonObject) childBeanList.get(1)).getString("childValue"));
}

@Test
public void simpleBeanListToJson() {
ArrayList<SimpleBean> list = new ArrayList<>();
SimpleBean bean1 = new SimpleBean();
bean1.string = "bean1";
SimpleBean bean2 = new SimpleBean();
bean2.string = "bean2";
list.add(bean1);
list.add(bean2);
JsonArray json = JsonUtils.listToJson(list);

Assert.assertEquals("bean1", json.getObject(0).getString("string"));
Assert.assertEquals("bean2", json.getObject(1).getString("string"));
}

@Test
public void simpleMapToJson() {
Map<String, Object> map = new HashMap<>();
SimpleBean bean1 = new SimpleBean();
bean1.string = "bean1";
SimpleBean bean2 = new SimpleBean();
bean2.string = "bean2";

map.put("one", bean1);
map.put("two", bean2);
JsonObject json = JsonUtils.mapToJson(map);

Assert.assertEquals("bean1", json.getObject("one").getString("string"));
Assert.assertEquals("bean2", json.getObject("two").getString("string"));
}

}