Skip to content
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 @@ -3,12 +3,14 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.hubspot.immutables.style.HubSpotStyle;
import com.hubspot.slack.client.models.blocks.table.DataTableCell;
import com.hubspot.slack.client.models.blocks.table.RawTextTableCell;
import com.hubspot.slack.client.models.blocks.table.deserializer.NullSafeDataTableCellRowDeserializer;
import java.util.Optional;
import org.immutables.value.Value;
import org.immutables.value.Value.Check;
Expand All @@ -31,6 +33,7 @@ default String getType() {
Optional<String> getCaption();

@Value.Parameter(order = 2)
@JsonDeserialize(contentUsing = NullSafeDataTableCellRowDeserializer.class)
ImmutableList<ImmutableList<DataTableCell>> getRows();

@JsonProperty("page_size")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.hubspot.slack.client.models.blocks;

import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.hubspot.immutables.style.HubSpotStyle;
import com.hubspot.slack.client.models.blocks.table.TableCell;
import com.hubspot.slack.client.models.blocks.table.TableColumnSetting;
import com.hubspot.slack.client.models.blocks.table.deserializer.NullSafeTableCellRowDeserializer;
import org.immutables.value.Value;
import org.immutables.value.Value.Check;
import org.immutables.value.Value.Immutable;
Expand All @@ -24,6 +26,7 @@ default String getType() {
}

@Value.Parameter
@JsonDeserialize(contentUsing = NullSafeTableCellRowDeserializer.class)
ImmutableList<ImmutableList<TableCell>> getRows();

ImmutableList<TableColumnSetting> getColumnSettings();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.hubspot.slack.client.models.blocks.table.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.google.common.collect.ImmutableList;
import com.hubspot.slack.client.models.blocks.table.TableCell;
import java.io.IOException;

abstract class AbstractNullSafeTableCellRowDeserializer<T extends TableCell>
extends StdDeserializer<ImmutableList<T>> {

private final Class<T> cellType;

protected AbstractNullSafeTableCellRowDeserializer(Class<T> cellType) {
super(Object.class);
this.cellType = cellType;
}

protected abstract T emptyCell();

@Override
public ImmutableList<T> deserialize(JsonParser parser, DeserializationContext context)
throws IOException {
ImmutableList.Builder<T> builder = ImmutableList.builder();
while (parser.nextToken() != JsonToken.END_ARRAY) {
if (parser.currentToken() == JsonToken.VALUE_NULL) {
builder.add(emptyCell());
} else {
builder.add(context.readValue(parser, cellType));
}
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hubspot.slack.client.models.blocks.table.deserializer;

import com.hubspot.slack.client.models.blocks.table.DataTableCell;
import com.hubspot.slack.client.models.blocks.table.RawTextTableCell;

public class NullSafeDataTableCellRowDeserializer
extends AbstractNullSafeTableCellRowDeserializer<DataTableCell> {

public NullSafeDataTableCellRowDeserializer() {
super(DataTableCell.class);
}

@Override
protected DataTableCell emptyCell() {
return RawTextTableCell.of("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hubspot.slack.client.models.blocks.table.deserializer;

import com.hubspot.slack.client.models.blocks.table.RawTextTableCell;
import com.hubspot.slack.client.models.blocks.table.TableCell;

public class NullSafeTableCellRowDeserializer
extends AbstractNullSafeTableCellRowDeserializer<TableCell> {

public NullSafeTableCellRowDeserializer() {
super(TableCell.class);
}

@Override
protected TableCell emptyCell() {
return RawTextTableCell.of("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class DataTableBlockTest {
private static final int WITH_OPTIONS_INDEX = 2;
private static final int UNKNOWN_CELL_INDEX = 3;
private static final int NO_CAPTION_INDEX = 4;
private static final int NULL_CELL_INDEX = 5;
private static DataTable[] blocks;

@BeforeClass
Expand Down Expand Up @@ -121,6 +122,23 @@ public void itDeserializesUnknownCellAsUnknownTableCell() {
.isInstanceOf(UnknownTableCell.class);
}

@Test
public void itDeserializesNullCellsAsBlankRawTextCells() {
assertThat(blocks[NULL_CELL_INDEX].getRows()).hasSize(2);
assertThat(blocks[NULL_CELL_INDEX].getRows().get(0))
.containsExactly(
RawTextTableCell.of("Name"),
RawTextTableCell.of(""),
RawTextTableCell.of("Department")
);
assertThat(blocks[NULL_CELL_INDEX].getRows().get(1))
.containsExactly(
RawTextTableCell.of("Alice"),
RawTextTableCell.of(""),
RawTextTableCell.of("Engineering")
);
}

@Test
public void itSerializesAndDeserializes() throws IOException {
DataTable original = DataTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class TableBlockTest {
private static final int COLUMN_SETTINGS_INDEX = 2;
private static final int BLOCK_ID_INDEX = 3;
private static final int UNKNOWN_CELL_INDEX = 4;
private static final int NULL_CELL_INDEX = 5;
private static Table[] blocks;

@BeforeClass
Expand Down Expand Up @@ -174,6 +175,17 @@ public void itFailsToBuildWithTooManyColumnSettings() {
fail("Expected IllegalStateException for too many column settings");
}

@Test
public void itDeserializesNullCellsAsBlankRawTextCells() {
assertThat(blocks[NULL_CELL_INDEX].getRows()).hasSize(1);
assertThat(blocks[NULL_CELL_INDEX].getRows().get(0))
.containsExactly(
RawTextTableCell.of("Name"),
RawTextTableCell.of(""),
RawTextTableCell.of("Department")
);
}

@Test
public void itFailsToBuildWithBlockIdExceedingMaxLength() {
try {
Expand Down
15 changes: 15 additions & 0 deletions slack-base/src/test/resources/data_table_block.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,20 @@
{"type": "raw_text", "text": "Body"}
]
]
},
{
"type": "data_table",
"rows": [
[
{"type": "raw_text", "text": "Name"},
null,
{"type": "raw_text", "text": "Department"}
],
[
{"type": "raw_text", "text": "Alice"},
null,
{"type": "raw_text", "text": "Engineering"}
]
]
}
]
10 changes: 10 additions & 0 deletions slack-base/src/test/resources/table_block.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,15 @@
{"type": "future_slack_cell_type"}
]
]
},
{
"type": "table",
"rows": [
[
{"type": "raw_text", "text": "Name"},
null,
{"type": "raw_text", "text": "Department"}
]
]
}
]