Skip to content

Commit

Permalink
#666 Test and fix reading nested objects from request
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiostalla committed Oct 4, 2023
1 parent 904eb67 commit 2747546
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ public void setValue(Object value) {
@Override
public void readFromRequest(HttpServletRequest req) {
super.readFromRequest(req);
if (mode.isView(insertable, updatable)) {
return;
}
form.readFromRequest(req);
setValueFromForm();
}

@Override
Expand All @@ -97,10 +101,17 @@ public void readFrom(KeyValueAccessor keyValueAccessor) {
if (value == null) {
setValue(null);
} else {
Object object = classAccessor.newInstance();
form.readFrom(keyValueAccessor.inner(value));
form.writeToObject(object);
this.value = object;
setValueFromForm();
}
}

protected void setValueFromForm() {
Object object = classAccessor.newInstance();
if (object == null) {
throw new IllegalStateException("Could not create an instance using " + classAccessor);
}
form.writeToObject(object);
this.value = object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,43 +86,43 @@ public void testRequiredEdit() {
text);
}

/*
public void testRequiredReadFromRequest() {
booleanField = new BooleanField(myPropertyAccessor, Mode.EDIT);
public void testRequiredReadFromRequest() {
var field = new ObjectField(myPropertyAccessor, Mode.EDIT);

booleanField.setRequired(true);
assertTrue(booleanField.isRequired());
field.setRequired(true);
assertTrue(field.isRequired());

MutableHttpServletRequest req = new MutableHttpServletRequest();
MutableHttpServletRequest req = new MutableHttpServletRequest();

booleanField.setValue(true);
assertTrue(booleanField.getValue());
NestedBean n1 = new NestedBean("n1", true);
field.setValue(n1);
assertEquals(n1, field.getValue());

req.setParameter("myBoolean", "");
booleanField.readFromRequest(req);
assertFalse(booleanField.getValue());
req.setParameter("nested.flag", "");
field.readFromRequest(req);
assertFalse(((NestedBean) field.getValue()).flag);

req.setParameter("myBoolean", "true");
booleanField.readFromRequest(req);
assertTrue(booleanField.getValue());
req.setParameter("nested.flag", "true");
field.readFromRequest(req);
assertTrue(((NestedBean) field.getValue()).flag);

req.setParameter("myBoolean", "false");
booleanField.readFromRequest(req);
assertFalse(booleanField.getValue());
req.setParameter("nested.flag", "false");
field.readFromRequest(req);
assertFalse(((NestedBean) field.getValue()).flag);

req.setParameter("myBoolean", (String) null);
req.setParameter(BooleanField.CHECK_PREFIX + "myBoolean", "true");
booleanField.readFromRequest(req);
assertFalse(booleanField.getValue());
booleanField.setValue(true);
req.setParameter("myBoolean", (String) null);
req.setParameter(BooleanField.CHECK_PREFIX + "myBoolean", (String) null);
booleanField.readFromRequest(req);
assertTrue(booleanField.getValue());
}
req.setParameter("nested.flag", (String) null);
req.setParameter("nested." + BooleanField.CHECK_PREFIX + "flag", "true");
field.readFromRequest(req);
assertFalse(((NestedBean) field.getValue()).flag);

field.setValue(n1);
req.setParameter("nested.flag", (String) null);
req.setParameter("nested." + BooleanField.CHECK_PREFIX + "flag", (String) null);
field.readFromRequest(req);
assertTrue(((NestedBean) field.getValue()).flag);
}

/*
//--------------------------------------------------------------------------
// Not required
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -229,6 +229,8 @@ public static class NestedBean {
public String name;
public boolean flag;

public NestedBean() {}

public NestedBean(String name, boolean flag) {
this.name = name;
this.flag = flag;
Expand Down

0 comments on commit 2747546

Please sign in to comment.