Skip to content

Commit 300552b

Browse files
authored
Set nodeType for rich text resolver correctly. (#321)
1 parent 7070b5e commit 300552b

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

src/main/java/com/contentful/java/cda/rich/CDARichNode.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,21 @@
66
* A leaf node of the rich text hierarchy.
77
*/
88
public class CDARichNode implements Serializable {
9+
private String nodeType;
10+
11+
/**
12+
* Get the original node type from the API response.
13+
* @return the node type (e.g., "embedded-entry-block", "embedded-entry-inline", etc.)
14+
*/
15+
public String getNodeType() {
16+
return nodeType;
17+
}
18+
19+
/**
20+
* Set the node type from the API response.
21+
* @param nodeType the original node type
22+
*/
23+
public void setNodeType(String nodeType) {
24+
this.nodeType = nodeType;
25+
}
926
}

src/main/java/com/contentful/java/cda/rich/RichTextFactory.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ public class RichTextFactory {
2828

2929
static {
3030
// add leafs
31-
RESOLVER_MAP.put("text", raw -> new CDARichText(
31+
RESOLVER_MAP.put("text", raw -> {
32+
CDARichText text = new CDARichText(
3233
(CharSequence) raw.get("value"),
3334
resolveMarks((List<Map<String, Object>>) raw.get("marks"))
34-
));
35+
);
36+
text.setNodeType((String) raw.get("nodeType"));
37+
return text;
38+
});
3539
RESOLVER_MAP.put("hr", raw -> new CDARichHorizontalRule());
3640

3741
// add blocks
@@ -329,6 +333,9 @@ public BlockResolver(Supplier<T> supplier) {
329333
@Override
330334
public CDARichNode resolve(Map<String, Object> raw) {
331335
final T resolved = getCDAType(raw);
336+
if (resolved instanceof CDARichNode) {
337+
((CDARichNode) resolved).setNodeType((String) raw.get("nodeType"));
338+
}
332339

333340
final List<Map<String, Object>> contents = (List<Map<String, Object>>)
334341
raw.get("content");
@@ -372,6 +379,15 @@ public CDARichHeading get() {
372379
});
373380
this.level = level;
374381
}
382+
383+
@Override
384+
public CDARichNode resolve(Map<String, Object> raw) {
385+
CDARichNode result = super.resolve(raw);
386+
if (result instanceof CDARichNode) {
387+
((CDARichNode) result).setNodeType((String) raw.get("nodeType"));
388+
}
389+
return result;
390+
}
375391
}
376392

377393
/**
@@ -404,7 +420,11 @@ private static class BlockAndDataResolver<T extends CDARichBlock>
404420
*/
405421
@Override
406422
T getCDAType(Map<String, Object> raw) {
407-
return supplier.get(raw.get(dataFieldKey));
423+
T result = supplier.get(raw.get(dataFieldKey));
424+
if (result instanceof CDARichNode) {
425+
((CDARichNode) result).setNodeType((String) raw.get("nodeType"));
426+
}
427+
return result;
408428
}
409429
}
410430
}

0 commit comments

Comments
 (0)