Skip to content

Commit da4ba33

Browse files
authored
docs: add Block Kit type instructions for slack-api-model (#1623)
1 parent 5998ea6 commit da4ba33

2 files changed

Lines changed: 340 additions & 22 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,29 +173,9 @@ All dependency versions are managed in root `pom.xml` `<properties>` section.
173173

174174
The `slack-api-model` module contains all core data types. When adding or modifying model objects, follow these patterns.
175175

176-
### Block Kit Elements
176+
### Block Kit Types
177177

178-
Directory: `slack-api-model/src/main/java/com/slack/api/model/block/element/`
179-
180-
All block elements extend `BlockElement` and use a `TYPE` constant:
181-
182-
```java
183-
@Data
184-
@Builder
185-
@NoArgsConstructor
186-
@AllArgsConstructor
187-
@EqualsAndHashCode(callSuper = false)
188-
public class DatePickerElement extends BlockElement {
189-
public static final String TYPE = "datepicker";
190-
private final String type = TYPE;
191-
192-
private String actionId;
193-
private PlainTextObject placeholder;
194-
private String initialDate;
195-
private ConfirmationDialogObject confirm;
196-
private Boolean focusOnLoad;
197-
}
198-
```
178+
Block Kit layout blocks, elements, and composition objects are defined in this module under `src/main/java/com/slack/api/model/block/`. See `slack-api-model/AGENTS.md` for detailed steps on adding new Block Kit types, covering class definition, Gson factory registration, static helpers, Kotlin DSL support, and tests.
199179

200180
### Event Types
201181

‎slack-api-model/AGENTS.md‎

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
# AGENTS.md — slack-api-model
2+
3+
Instructions for AI coding agents working on this module.
4+
5+
## Module Overview
6+
7+
The `slack-api-model` module contains all core Slack data types: Block Kit blocks, elements, composition objects, event types, views, and other shared model objects. All other modules in the SDK depend on this one.
8+
9+
## Architecture
10+
11+
### Source Layout
12+
13+
```txt
14+
src/main/java/com/slack/api/model/
15+
├── block/ # Block Kit UI framework types
16+
│ ├── element/ # Interactive elements (buttons, menus, inputs, etc.)
17+
│ ├── composition/ # Reusable objects (text, options, confirmations, etc.)
18+
│ ├── LayoutBlock.java # Interface for all layout blocks
19+
│ ├── BlockElement.java # Abstract base for interactive elements (in element/)
20+
│ ├── ContextBlockElement.java # Interface for elements in context blocks
21+
│ ├── ContextActionsBlockElement.java # Interface for elements in context actions blocks
22+
│ ├── Blocks.java # Static factory helpers for layout blocks
23+
│ ├── *Block.java # Concrete layout block types
24+
│ └── Unknown*.java # Fallback types for unknown JSON type values
25+
├── event/ # Slack event type definitions
26+
├── view/ # Modal and Home tab view types
27+
└── ... # Other model types (File, Message, User, etc.)
28+
```
29+
30+
### Key Design Principles
31+
32+
- **Gson deserialization via type factories** — Block Kit uses a manual discriminator pattern. Each category has a Gson factory (`GsonLayoutBlockFactory`, `GsonBlockElementFactory`, etc.) that reads the JSON `"type"` field and maps it to the concrete Java class. Unknown types fall through to `Unknown*` classes instead of throwing.
33+
- **Lombok annotations** — All model types use `@Data`, `@Builder`, `@NoArgsConstructor`, `@AllArgsConstructor`. Layout blocks don't need `@EqualsAndHashCode(callSuper = false)` (they implement an interface), but elements do (they extend an abstract class).
34+
- **Static factory helpers** — `Blocks.java` and `BlockElements.java` provide convenience methods for constructing types via the `ModelConfigurator` lambda pattern.
35+
36+
## Adding a New Block Kit Type
37+
38+
Block Kit types span three categories. The steps differ slightly for each:
39+
40+
| Category | Location | Base Type | Gson Factory |
41+
|----------|----------|-----------|--------------|
42+
| Layout blocks | `block/*.java` | `LayoutBlock` (interface) | `GsonLayoutBlockFactory` |
43+
| Block elements | `block/element/*.java` | `BlockElement` (abstract class) | `GsonBlockElementFactory` |
44+
| Composition objects | `block/composition/*.java` | N/A (standalone `@Data` classes) | `GsonTextObjectFactory` (text objects only) |
45+
46+
### Steps
47+
48+
#### 1. Look up the Block Kit documentation
49+
50+
Check the Slack API reference:
51+
- Layout blocks: `https://docs.slack.dev/reference/block-kit/blocks/{block-type}`
52+
- Elements: `https://docs.slack.dev/reference/block-kit/block-elements/{element-type}`
53+
- Composition objects: `https://docs.slack.dev/reference/block-kit/composition-objects/{object-type}`
54+
55+
#### 2. Create the Java class
56+
57+
##### For a layout block
58+
59+
File: `src/main/java/com/slack/api/model/block/{Name}Block.java`
60+
61+
```java
62+
package com.slack.api.model.block;
63+
64+
import lombok.AllArgsConstructor;
65+
import lombok.Builder;
66+
import lombok.Data;
67+
import lombok.NoArgsConstructor;
68+
69+
/**
70+
* https://docs.slack.dev/reference/block-kit/blocks/{type}-block
71+
*/
72+
@Data
73+
@Builder
74+
@NoArgsConstructor
75+
@AllArgsConstructor
76+
public class CardBlock implements LayoutBlock {
77+
public static final String TYPE = "card";
78+
private final String type = TYPE;
79+
80+
private String blockId;
81+
82+
// Add fields from the API docs (camelCase in Java, auto-mapped to snake_case JSON)
83+
}
84+
```
85+
86+
For container blocks (those whose primary purpose is holding child elements), initialize the list with `@Builder.Default`:
87+
88+
```java
89+
@Builder.Default
90+
private List<ContextActionsBlockElement> elements = new ArrayList<>();
91+
```
92+
93+
##### For a block element
94+
95+
File: `src/main/java/com/slack/api/model/block/element/{Name}Element.java`
96+
97+
```java
98+
package com.slack.api.model.block.element;
99+
100+
import com.slack.api.model.block.composition.PlainTextObject;
101+
import com.slack.api.model.block.composition.ConfirmationDialogObject;
102+
import lombok.*;
103+
104+
/**
105+
* https://docs.slack.dev/reference/block-kit/block-elements/{type}-element
106+
*/
107+
@Data
108+
@Builder
109+
@NoArgsConstructor
110+
@AllArgsConstructor
111+
@EqualsAndHashCode(callSuper = false)
112+
public class DatePickerElement extends BlockElement {
113+
public static final String TYPE = "datepicker";
114+
private final String type = TYPE;
115+
116+
private String actionId;
117+
private PlainTextObject placeholder;
118+
private String initialDate;
119+
private ConfirmationDialogObject confirm;
120+
private Boolean focusOnLoad;
121+
}
122+
```
123+
124+
##### Key conventions
125+
126+
- The `TYPE` constant must match exactly what the Slack API returns in the `"type"` JSON field.
127+
- Java fields are camelCase. Gson's `LOWER_CASE_WITH_UNDERSCORES` policy handles snake_case mapping automatically.
128+
- **List initialization depends on the field's role:**
129+
- Container blocks whose primary purpose is holding child elements (e.g., `ActionsBlock`, `ContextBlock`, `ContextActionsBlock`) initialize their `elements` list with `@Builder.Default` and `new ArrayList<>()`.
130+
- Optional/secondary list fields (e.g., `SectionBlock.fields`) are left as `null` — null vs empty has semantic meaning for optional fields.
131+
- Use `@SerializedName` only when the API field name doesn't follow standard snake_case.
132+
- Nested objects use inner `@Data` classes within the block/element class.
133+
134+
#### 3. Register in the Gson deserialization factory
135+
136+
**This is the most critical step.** Without it, the type will deserialize as `UnknownBlock` or `UnknownBlockElement`.
137+
138+
##### For layout blocks
139+
140+
File: `src/main/java/com/slack/api/util/json/GsonLayoutBlockFactory.java`
141+
142+
Add a `case` to the `getLayoutClassInstance` switch statement:
143+
144+
```java
145+
case CardBlock.TYPE:
146+
return CardBlock.class;
147+
```
148+
149+
No import needed — the factory uses `import com.slack.api.model.block.*` which covers all block classes.
150+
151+
##### For block elements
152+
153+
File: `src/main/java/com/slack/api/util/json/GsonBlockElementFactory.java`
154+
155+
Add a `case` to the switch statement in `getContextBlockElementClassInstance` (note: the method name is a historical misnomer — it handles all block elements, not just context ones):
156+
157+
```java
158+
case SomeNewElement.TYPE:
159+
return SomeNewElement.class;
160+
```
161+
162+
No import needed — the factory uses `import com.slack.api.model.block.element.*` which covers all element classes.
163+
164+
##### Other factories
165+
166+
Depending on where the element can appear, you may also need to register it in:
167+
- `GsonContextBlockElementFactory.java` — for elements allowed inside `ContextBlock`
168+
- `GsonContextActionsBlockElementFactory.java` — for elements allowed inside `ContextActionsBlock`
169+
- `GsonRichTextElementFactory.java` — for rich text sub-elements
170+
- `GsonTextObjectFactory.java` — for new text/composition object types
171+
172+
All factories are in `src/main/java/com/slack/api/util/json/` and are registered via `registerTypeAdapter()` in `slack-api-client/src/main/java/com/slack/api/util/json/GsonFactory.java`.
173+
174+
**Important:** If you are adding a `case` to an existing factory, no further registration is needed. If you create an entirely new Gson factory (rare — only when introducing a new interface like `ContextActionsBlockElement`), you must register it in both:
175+
- `slack-api-client/src/main/java/com/slack/api/util/json/GsonFactory.java` (production)
176+
- `slack-api-model/src/test/java/test_locally/unit/GsonFactory.java` (tests)
177+
178+
#### 4. Add static helper methods
179+
180+
##### For layout blocks — `Blocks.java`
181+
182+
File: `src/main/java/com/slack/api/model/block/Blocks.java`
183+
184+
```java
185+
// CardBlock
186+
187+
public static CardBlock card(ModelConfigurator<CardBlock.CardBlockBuilder> configurator) {
188+
return configurator.configure(CardBlock.builder()).build();
189+
}
190+
```
191+
192+
##### For block elements — `BlockElements.java`
193+
194+
File: `src/main/java/com/slack/api/model/block/element/BlockElements.java`
195+
196+
```java
197+
// SomeNewElement
198+
199+
public static SomeNewElement someNew(ModelConfigurator<SomeNewElement.SomeNewElementBuilder> configurator) {
200+
return configurator.configure(SomeNewElement.builder()).build();
201+
}
202+
```
203+
204+
##### For composition objects — `BlockCompositions.java`
205+
206+
File: `src/main/java/com/slack/api/model/block/composition/BlockCompositions.java`
207+
208+
```java
209+
// SomeNewObject
210+
211+
public static SomeNewObject someNew(ModelConfigurator<SomeNewObject.SomeNewObjectBuilder> configurator) {
212+
return configurator.configure(SomeNewObject.builder()).build();
213+
}
214+
```
215+
216+
#### 5. Add Kotlin DSL support (layout blocks only, optional)
217+
218+
The `slack-api-model-kotlin-extension` module provides a Kotlin DSL for building blocks. This step is optional — some blocks (e.g., `MarkdownBlock`) skip it — but recommended for blocks with configurable fields so Kotlin users can build them via `withBlocks { }`. For new layout blocks, update three files:
219+
220+
##### 5a. Create a builder class
221+
222+
File: `slack-api-model-kotlin-extension/src/main/kotlin/com/slack/api/model/kotlin_extension/block/{Name}BlockBuilder.kt`
223+
224+
```kotlin
225+
package com.slack.api.model.kotlin_extension.block
226+
227+
import com.slack.api.model.block.CardBlock
228+
229+
@BlockLayoutBuilder
230+
class CardBlockBuilder : Builder<CardBlock> {
231+
private var _blockId: String? = null
232+
// private fields for each property
233+
234+
fun blockId(blockId: String) {
235+
_blockId = blockId
236+
}
237+
238+
// setter methods for each property
239+
240+
override fun build(): CardBlock {
241+
return CardBlock.builder()
242+
.blockId(_blockId)
243+
// .otherField(_otherField)
244+
.build()
245+
}
246+
}
247+
```
248+
249+
##### 5b. Add method to `LayoutBlockDsl` interface
250+
251+
File: `slack-api-model-kotlin-extension/src/main/kotlin/com/slack/api/model/kotlin_extension/block/dsl/LayoutBlockDsl.kt`
252+
253+
```kotlin
254+
/**
255+
* @see <a href="https://docs.slack.dev/reference/block-kit/blocks/card-block">Card documentation</a>
256+
*/
257+
fun card(builder: CardBlockBuilder.() -> Unit)
258+
```
259+
260+
##### 5c. Implement in `MultiLayoutBlockContainer`
261+
262+
File: `slack-api-model-kotlin-extension/src/main/kotlin/com/slack/api/model/kotlin_extension/block/container/MultiLayoutBlockContainer.kt`
263+
264+
```kotlin
265+
override fun card(builder: CardBlockBuilder.() -> Unit) {
266+
underlying += CardBlockBuilder().apply(builder).build()
267+
}
268+
```
269+
270+
#### 6. Add tests
271+
272+
##### Deserialization test
273+
274+
File: `src/test/java/test_locally/api/model/block/BlockKitTest.java`
275+
276+
Test that JSON with the new type deserializes correctly:
277+
278+
```java
279+
@Test
280+
public void parseCardBlock() {
281+
String blocks = "[{\n" +
282+
" \"type\": \"card\",\n" +
283+
" \"block_id\": \"card123\"\n" +
284+
"}]";
285+
String json = "{blocks: " + blocks + "}";
286+
Message message = GsonFactory.createSnakeCase().fromJson(json, Message.class);
287+
assertThat(message, is(notNullValue()));
288+
assertThat(message.getBlocks().size(), is(1));
289+
CardBlock cardBlock = (CardBlock) message.getBlocks().get(0);
290+
assertThat(cardBlock.getBlockId(), is("card123"));
291+
}
292+
```
293+
294+
##### Static helper test
295+
296+
File: `src/test/java/test_locally/api/model/block/BlocksTest.java`
297+
298+
Test the `Blocks.java` convenience method:
299+
300+
```java
301+
@Test
302+
public void testCard() {
303+
assertThat(card(c -> c.blockId("block-id")), is(notNullValue()));
304+
}
305+
```
306+
307+
#### 7. Verify
308+
309+
```bash
310+
# Install all modules locally (required for cross-module dependencies)
311+
./scripts/install_local.sh
312+
313+
# Run the block kit tests
314+
./mvnw test -pl slack-api-model '-Dtest=test_locally.api.model.block.BlockKitTest,test_locally.api.model.block.BlocksTest'
315+
316+
# If you added Kotlin DSL support, also test:
317+
./mvnw test -pl slack-api-model-kotlin-extension '-Dtest=test_locally.**.*Test'
318+
```
319+
320+
### Summary checklist
321+
322+
When adding a new Block Kit type, make sure you have:
323+
324+
- [ ] Looked up the type docs at `https://docs.slack.dev/reference/block-kit/blocks/{type}` or `.../block-elements/{type}`
325+
- [ ] Created the Java class in the appropriate package (`block/` for layout blocks, `block/element/` for elements, `block/composition/` for composition objects)
326+
- [ ] Added a `case` in the appropriate Gson factory (`GsonLayoutBlockFactory` for layout blocks, `GsonBlockElementFactory` for elements)
327+
- [ ] Added static helper method(s) in `Blocks.java` (for layout blocks), `BlockElements.java` (for elements), or `BlockCompositions.java` (for composition objects)
328+
- [ ] (Optional) Added Kotlin DSL support: builder class, `LayoutBlockDsl` method, and `MultiLayoutBlockContainer` implementation (layout blocks with configurable fields)
329+
- [ ] Added deserialization test in `BlockKitTest.java`
330+
- [ ] Added static helper test in `BlocksTest.java`
331+
- [ ] Verified tests pass
332+
333+
## Common Pitfalls
334+
335+
- **Forgetting the Gson factory registration** — the type will silently deserialize as `UnknownBlock`/`UnknownBlockElement` with no error. Always add the `case` statement.
336+
- **Wrong factory** — layout blocks go in `GsonLayoutBlockFactory`, elements in `GsonBlockElementFactory`. Registering in the wrong one means it won't deserialize in the right context.
337+
- **Missing Kotlin DSL** — if you add a layout block with configurable fields but skip the Kotlin extension, users of `withBlocks { }` DSL won't have access to the new type. This is optional but recommended.
338+
- **Java 9+ APIs** — the project targets Java 8. Do not use `List.of()`, `Map.of()`, `var`, etc.

0 commit comments

Comments
 (0)