JSpecify support in Cucumber 8.x #2328
|
Hi all, I'm very happy about the support for JSpecify in Cucumber 8.x. I have been using JSpecify for about two years now I would say. I do have a question though: Why is the key in the return value of DataTable#asMap nullable? @Given("Foobar")
public void foobar(io.cucumber.datatable.DataTable dataTable) {
Map<@Nullable String, @Nullable String> map = dataTable.asMap();
System.out.println(map);
}If I specify a null value in the feature like so: Scenario: Foobar
Given Foobar
| | Foobar |
| Hello | World! |I get the following error:
Nullable keys in maps don't seem to make a lot of sense. Could you enlighten me about the motivation? Thanks a lot, |
Replies: 1 comment 1 reply
|
Tables may contain null values and nullable keys follows from that. Unfortunately you picked the one example where that isn't apparent. For example by leaving two cells blank: Given a table
| a | |
| | d |Then we have a table with The exception you're seeing is thrown because there are a few different ways to transform datatables. The empty first header cell helps Cucumber choose the method for creating the values in the map. Either all elements after the first cell in each row are transformed. Or all elements after the first cell are paired with their header into a map and then transformed. For the latter, the first cell is always empty. You could argue that for String values this isn't really necessary, but if you abstract away from those then the assumption becomes unavoidable. It's an imperfect heuristic but mapping flat tables to different nested structures requires some inherent guess work. As a workaround, you could let Cucumber transform the table for you by changing your step definition accept a map parameter. @Given("a table")
void aTable(Map<String, String> table) {
}
That all said, I agree that nullable keys in maps don't make much sense to me either. But I also didn't want to put that in this major just in case people do non-sensible things. But we can certainly consider rejecting nullable keys for the next major version. |
Tables may contain null values and nullable keys follows from that. Unfortunately you picked the one example where that isn't apparent. For example by leaving two cells blank:
Then we have a table with
[[a, null], [null, d]]. Then usingDataTable.asMap()yields{a=null, null=d}.The exception you're seeing is thrown because there are a few different ways to transform datatables. The empty first header cell helps Cucumber choose the method for creating the values in the map. Either all elements after the first cell in each row are transformed. Or all elements after the first cell are paired with their header into a map and then transformed. For the lat…