Skip to content

Commit 13c5f63

Browse files
authored
Merge pull request #817 from aleksei-averchenko-wise/comment-parsing-fix
Fix comment parsing without an explicit `type` present
2 parents eeff3e2 + 86649d8 commit 13c5f63

3 files changed

Lines changed: 109 additions & 1 deletion

File tree

src/main/java/org/zendesk/client/v2/model/Comment.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.fasterxml.jackson.annotation.JsonTypeInfo.As.EXTERNAL_PROPERTY;
44
import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME;
55

6+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
67
import com.fasterxml.jackson.annotation.JsonProperty;
78
import com.fasterxml.jackson.annotation.JsonSubTypes;
89
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@@ -16,12 +17,18 @@
1617
* @author stephenc
1718
* @since 09/04/2013 15:09
1819
*/
19-
@JsonTypeInfo(use = NAME, include = EXTERNAL_PROPERTY, property = "type", visible = true)
20+
@JsonTypeInfo(
21+
use = NAME,
22+
include = EXTERNAL_PROPERTY,
23+
property = "type",
24+
defaultImpl = Comment.class,
25+
visible = true)
2026
@JsonSubTypes({
2127
@JsonSubTypes.Type(value = Comment.class, name = "Comment"),
2228
@JsonSubTypes.Type(value = VoiceComment.class, name = "VoiceComment"),
2329
@JsonSubTypes.Type(value = VoiceComment.class, name = "TpeVoiceComment")
2430
})
31+
@JsonIgnoreProperties(ignoreUnknown = true)
2532
public class Comment implements Serializable {
2633

2734
private static final long serialVersionUID = 1L;

src/main/java/org/zendesk/client/v2/model/comments/VoiceComment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.zendesk.client.v2.model.comments;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45
import org.zendesk.client.v2.model.Comment;
56

7+
@JsonIgnoreProperties(ignoreUnknown = true)
68
public class VoiceComment extends Comment {
79

810
private VoiceCommentData data;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.zendesk.client.v2.model;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertThrows;
5+
6+
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
9+
import com.fasterxml.jackson.databind.node.ObjectNode;
10+
import java.util.function.Function;
11+
import org.jetbrains.annotations.Nullable;
12+
import org.junit.Test;
13+
import org.zendesk.client.v2.Zendesk;
14+
import org.zendesk.client.v2.model.comments.VoiceComment;
15+
16+
public class CommentTest {
17+
18+
private static final long COMMENT_ID = 123L;
19+
private static final String COMMENT_BODY = "Foo";
20+
private static final String COMMENT_VOICE_URL = "http://yourdomain.com/recordings/1.mp3";
21+
22+
private static final ObjectMapper MAPPER = Zendesk.createMapper(Function.identity());
23+
24+
@Test
25+
public void defaultType() throws JsonProcessingException {
26+
String json = createCommentJson(null);
27+
Comment comment = parseJson(json);
28+
assertEquals(Comment.class, comment.getClass());
29+
assertEquals(Long.valueOf(COMMENT_ID), comment.getId());
30+
assertEquals(COMMENT_BODY, comment.getBody());
31+
}
32+
33+
@Test
34+
public void explicitTypeComment() throws JsonProcessingException {
35+
String json = createCommentJson("Comment");
36+
Comment comment = parseJson(json);
37+
assertEquals(Comment.class, comment.getClass());
38+
assertEquals(Long.valueOf(COMMENT_ID), comment.getId());
39+
assertEquals(COMMENT_BODY, comment.getBody());
40+
}
41+
42+
@Test
43+
public void explicitTypeVoiceComment() throws JsonProcessingException {
44+
String json = createCommentJson("VoiceComment");
45+
Comment comment = parseJson(json);
46+
assertEquals(VoiceComment.class, comment.getClass());
47+
48+
VoiceComment voiceComment = (VoiceComment) comment;
49+
assertEquals(Long.valueOf(COMMENT_ID), voiceComment.getId());
50+
assertEquals(COMMENT_BODY, voiceComment.getBody());
51+
assertEquals(COMMENT_VOICE_URL, voiceComment.getData().getRecordingUrl());
52+
}
53+
54+
@Test
55+
public void explicitTypeTpeVoiceCommentType() throws JsonProcessingException {
56+
String json = createCommentJson("TpeVoiceComment");
57+
Comment comment = parseJson(json);
58+
assertEquals(VoiceComment.class, comment.getClass());
59+
60+
VoiceComment voiceComment = (VoiceComment) comment;
61+
assertEquals(Long.valueOf(COMMENT_ID), voiceComment.getId());
62+
assertEquals(COMMENT_BODY, voiceComment.getBody());
63+
assertEquals(COMMENT_VOICE_URL, voiceComment.getData().getRecordingUrl());
64+
}
65+
66+
@Test
67+
public void invalidType() throws JsonProcessingException {
68+
String json = createCommentJson("InvalidCommentType");
69+
assertThrows(InvalidFormatException.class, () -> parseJson(json));
70+
}
71+
72+
private static String createCommentJson(@Nullable String type) throws JsonProcessingException {
73+
// https://developer.zendesk.com/documentation/ticketing/managing-tickets/adding-voice-comments-to-tickets/
74+
ObjectNode voiceData =
75+
MAPPER
76+
.createObjectNode()
77+
.put("from", "+16617480240")
78+
.put("to", "+16617480123")
79+
.put("recording_url", COMMENT_VOICE_URL)
80+
.put("started_at", "2019-04-16T09:14:57Z")
81+
.put("call_duration", 42)
82+
.put("answered_by_id", 28765)
83+
.put("transcription_text", "The transcription of the call")
84+
.put("location", "Topeka, Kansas");
85+
86+
ObjectNode res =
87+
MAPPER.createObjectNode().put("id", COMMENT_ID).put("body", "Foo").set("data", voiceData);
88+
89+
if (type != null) {
90+
res.put("type", type);
91+
}
92+
93+
return MAPPER.writeValueAsString(res);
94+
}
95+
96+
private static Comment parseJson(String json) throws JsonProcessingException {
97+
return MAPPER.readValue(json, Comment.class);
98+
}
99+
}

0 commit comments

Comments
 (0)