todo42
(Dominik Hirt)
April 23, 2024, 2:23pm
1
As mentioned in the docs a lit template can handle nested properties. This seems not to work if the bean is a java record:
my lit template with a repeating template via map()
String litLieferscheine = """
${item.lieferscheinInfos.map( (info) => html`
<a href='/document/${info.documentId}' target='_blank'> <vaadin-icon icon='vaadin:file-table'/></a> ` ) }
""";
this.addColumn(LitRenderer.<MengenGridDto>of(litLieferscheine)
.withProperty("lieferscheinInfos", dto -> dto.getLieferscheinInfos()))
.setHeader("Lieferschein")
.setAutoWidth(true)
.setFlexGrow(0)
.setResizable(true)
.setSortable(false);
info
maps to an instance of LieferscheinInfo
due to dto.getLieferscheinInfos()
is a List.
This only works if LieferscheinInfo
is a class, e.g.
@Data
public static class LieferscheinInfo {
private String nummer;
private Integer documentId;
}
It doesn’t work, if LieferscheinInfo
is a java record:
public record LieferscheinInfo(String nummer, Integer documentId) {}
It this intentionally or accidently ?
knoobie
(Christian Knoop)
April 23, 2024, 3:13pm
2
Records are not really supported… they work sometimes but not in all cases… might be related to Support for serialization of record object to Json · Issue #16879 · vaadin/flow · GitHub
Suggestion: Create a ticket in Flow
2 Likes
todo42
(Dominik Hirt)
April 24, 2024, 5:50am
3
Thanks @knoobie for your fast response …
I created the ticket
opened 06:32AM - 24 Apr 24 UTC
### Description of the bug
As mentioned in the [docs](https://vaadin.com/docs… /latest/components/grid/flow#binding-beans) a lit template can handle nested properties. This seems not to work if the _bean_ is a java record.
### Expected behavior
Java records should be first class citizens for use within Lit templates too.
### Minimal reproducible example
See the example. There are both variants for the relevant _bean_ AudiData:
a) as class
b) as record
If the class variant is used, it works and the grid shows the values.
If the record variant is used, the column remains empty.
hint: the usage of Lombok `@Data` and `@Builder` is only to shorten the example here.
```
@PageTitle("LitTemplate Test")
@Route(value = "test", layout = MainLayout.class)
@PermitAll
public class TestView extends VerticalLayout {
public TestView() {
Grid<Todo> grid = new Grid<>();
grid.addColumn(Todo::getTitle).setHeader("Title");
String litTemplate = """
<p>${item.audit.createdBy}</p>
""";
grid.addColumn(LitRenderer.<Todo>of(litTemplate)
.withProperty("audit", dto -> dto.getAudit()))
.setHeader("Audit");
// data
grid.setItems(
Todo.builder().title("todo-1").audit(new AuditData("Dominik")).build(),
Todo.builder().title("todo-2").audit(new AuditData("Dominik")).build(),
Todo.builder().title("todo-3").audit(new AuditData("Dominik")).build()
);
add(grid);
}
@Data
@Builder
public static class Todo {
private String title;
private AuditData audit;
}
@Data
public static class AuditData {
private String createdBy;
public AuditData(String createdBy) {
this.createdBy = createdBy;
}
}
// public record AuditData(String createdBy) {};
}
```
### Versions
- Vaadin / Flow version: 24.3.9
- Java version: 21
- OS version: MacOS
- Browser version (if applicable): Firefox 124
mikhail.21
(Mikhail Shabarov)
April 26, 2024, 1:07pm
4
Thanks for the issue, @todo42 !
We have been working on Java Records support in Binder, Grid and CRUD. This ticket looks like a prerequisite for Grid, thanks.
Please follow the status of this ticket. Target release for Java Records support is 24.5 (or 24.6, if it would take much efforts).