Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 645d3a5

Browse files
committed
Analysis: Add missing n-a to comment wp com rest response fields
FYI: 'n-a' stands for 'nullability annotations'. PS: This 'NotNullFieldNotInitialized' warning got suppressed because a 'response' class can never have its fields initialized via a constructor initialization, or otherwise for that matter.
1 parent 4f4070a commit 645d3a5

File tree

5 files changed

+70
-66
lines changed

5 files changed

+70
-66
lines changed

example/src/test/java/org/wordpress/android/fluxc/common/CommentsMapperTest.kt

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ class CommentsMapperTest {
4343

4444
@Test
4545
fun `xmlrpc dto list is converted to entity list`() {
46-
val commentList = getDefaultCommentList(false).map { it.copy(id = 0, authorProfileImageUrl = null) }
46+
val commentList = getDefaultCommentList(false).map { it.copy(
47+
id = 0,
48+
authorProfileImageUrl = null
49+
) }
4750
val site = SiteModel().apply {
4851
id = commentList.first().localSiteId
4952
selfHostedSiteId = commentList.first().remoteSiteId
@@ -120,27 +123,27 @@ class CommentsMapperTest {
120123
val entity = this
121124
return CommentWPComRestResponse().apply {
122125
ID = entity.remoteCommentId
123-
URL = entity.url
126+
URL = entity.url ?: ""
124127
author = Author().apply {
125128
ID = entity.authorId
126-
URL = entity.authorUrl
127-
avatar_URL = entity.authorProfileImageUrl
128-
email = entity.authorEmail
129-
name = entity.authorName
129+
URL = entity.authorUrl ?: ""
130+
avatar_URL = entity.authorProfileImageUrl ?: ""
131+
email = entity.authorEmail ?: ""
132+
name = entity.authorName ?: ""
130133
}
131-
content = entity.content
132-
date = entity.datePublished
134+
content = entity.content ?: ""
135+
date = entity.datePublished ?: ""
133136
i_like = entity.iLike
134137
parent = CommentParent().apply {
135138
ID = entity.parentId
136139
}
137140
post = Post().apply {
138141
type = "post"
139-
title = entity.postTitle
142+
title = entity.postTitle ?: ""
140143
link = "https://public-api.wordpress.com/rest/v1.1/sites/185464053/posts/85"
141144
ID = entity.remotePostId
142145
}
143-
status = entity.status
146+
status = entity.status ?: ""
144147
}
145148
}
146149

@@ -188,22 +191,22 @@ class CommentsMapperTest {
188191

189192
private fun getDefaultComment(allowNulls: Boolean): CommentEntity {
190193
return CommentEntity(
191-
id = 0,
192-
remoteCommentId = 10,
193-
remotePostId = 100,
194-
authorId = 44,
194+
id = 0L,
195+
remoteCommentId = 10L,
196+
remotePostId = 100L,
197+
authorId = 44L,
195198
localSiteId = 10_000,
196-
remoteSiteId = 100_000,
197-
authorUrl = if (allowNulls) null else "https://test-debug-site.wordpress.com",
198-
authorName = if (allowNulls) null else "authorname",
199-
authorEmail = if (allowNulls) null else "[email protected]",
200-
authorProfileImageUrl = if (allowNulls) null else "https://gravatar.com/avatar/111222333",
201-
postTitle = if (allowNulls) null else "again",
199+
remoteSiteId = 100_000L,
200+
authorUrl = if (allowNulls) "" else "https://test-debug-site.wordpress.com",
201+
authorName = if (allowNulls) "" else "authorname",
202+
authorEmail = if (allowNulls) "" else "[email protected]",
203+
authorProfileImageUrl = if (allowNulls) "" else "https://gravatar.com/avatar/111222333",
204+
postTitle = if (allowNulls) "" else "again",
202205
status = APPROVED.toString(),
203-
datePublished = if (allowNulls) null else "2021-05-12T15:10:40+02:00",
206+
datePublished = if (allowNulls) "" else "2021-05-12T15:10:40+02:00",
204207
publishedTimestamp = 1_000_000,
205-
content = if (allowNulls) null else "content example",
206-
url = if (allowNulls) null else "https://test-debug-site.wordpress.com/2021/02/25/again/#comment-137",
208+
content = if (allowNulls) "" else "content example",
209+
url = if (allowNulls) "" else "https://test-debug-site.wordpress.com/2021/02/25/again/#comment-137",
207210
hasParent = true,
208211
parentId = 1_000L,
209212
iLike = false
@@ -213,10 +216,10 @@ class CommentsMapperTest {
213216
private fun getDefaultCommentList(allowNulls: Boolean): CommentEntityList {
214217
val comment = getDefaultComment(allowNulls)
215218
return listOf(
216-
comment.copy(id = 1, remoteCommentId = 10, datePublished = "2021-07-24T00:51:43+02:00"),
217-
comment.copy(id = 2, remoteCommentId = 20, datePublished = "2021-07-24T00:51:43+02:00"),
218-
comment.copy(id = 3, remoteCommentId = 30, datePublished = "2021-07-24T00:51:43+02:00"),
219-
comment.copy(id = 4, remoteCommentId = 40, datePublished = "2021-07-24T00:51:43+02:00")
219+
comment.copy(id = 1L, remoteCommentId = 10L, datePublished = "2021-07-24T00:51:43+02:00"),
220+
comment.copy(id = 2L, remoteCommentId = 20L, datePublished = "2021-07-24T00:51:43+02:00"),
221+
comment.copy(id = 3L, remoteCommentId = 30L, datePublished = "2021-07-24T00:51:43+02:00"),
222+
comment.copy(id = 4L, remoteCommentId = 40L, datePublished = "2021-07-24T00:51:43+02:00")
220223
)
221224
}
222225
}

example/src/test/java/org/wordpress/android/fluxc/network/rest/wpcom/comments/CommentsRestClientTest.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class CommentsRestClientTest {
9696
assertThat(payload.isError).isFalse
9797

9898
val comments = payload.response!!
99-
assertThat(comments.size).isEqualTo(commentsResponse.comments.size)
99+
assertThat(comments.size).isEqualTo(commentsResponse.comments?.size)
100100
assertThat(urlCaptor.lastValue).isEqualTo(
101101
"https://public-api.wordpress.com/rest/v1.1/sites/${site.siteId}/comments/"
102102
)
@@ -600,10 +600,10 @@ class CommentsRestClientTest {
600600

601601
private fun getDefaultDto(): CommentWPComRestResponse {
602602
return CommentWPComRestResponse().apply {
603-
ID = 137
603+
ID = 137L
604604
URL = "https://test-site.wordpress.com/2021/02/25/again/#comment-137"
605605
author = Author().apply {
606-
ID = 0
606+
ID = 0L
607607
URL = "https://debugging-test.wordpress.com"
608608
avatar_URL = "https://gravatar.com/avatar/avatarurl"
609609
@@ -612,9 +612,9 @@ class CommentsRestClientTest {
612612
content = "example content"
613613
date = "2021-05-12T15:10:40+02:00"
614614
i_like = true
615-
parent = CommentParent().apply { ID = 41 }
615+
parent = CommentParent().apply { ID = 41L }
616616
post = Post().apply {
617-
ID = 85
617+
ID = 85L
618618
link = "https://public-api.wordpress.com/rest/v1.1/sites/11111111/posts/85"
619619
title = "again"
620620
type = "post"
@@ -627,31 +627,31 @@ class CommentsRestClientTest {
627627
return CommentLikeWPComRestResponse().apply {
628628
success = true
629629
i_like = iLike
630-
like_count = 100
630+
like_count = 100L
631631
}
632632
}
633633

634634
private fun CommentWPComRestResponse.toEntity(): CommentEntity {
635635
val dto = this
636636
return CommentEntity(
637-
id = 0,
637+
id = 0L,
638638
remoteCommentId = dto.ID,
639-
remotePostId = dto.post.ID,
640-
authorId = dto.author.ID,
639+
remotePostId = dto.post?.ID ?: 0L,
640+
authorId = dto.author?.ID ?: 0L,
641641
localSiteId = 10,
642-
remoteSiteId = 200,
643-
authorUrl = dto.author.URL,
644-
authorName = dto.author.name,
645-
authorEmail = dto.author.email,
646-
authorProfileImageUrl = dto.author.avatar_URL,
647-
postTitle = dto.post.title,
642+
remoteSiteId = 200L,
643+
authorUrl = dto.author?.URL,
644+
authorName = dto.author?.name,
645+
authorEmail = dto.author?.email,
646+
authorProfileImageUrl = dto.author?.avatar_URL,
647+
postTitle = dto.post?.title,
648648
status = dto.status,
649649
datePublished = dto.date,
650-
publishedTimestamp = 132456,
650+
publishedTimestamp = 132456L,
651651
content = dto.content,
652652
url = dto.URL,
653653
hasParent = dto.parent != null,
654-
parentId = dto.parent.ID,
654+
parentId = dto.parent?.ID ?: 0L,
655655
iLike = dto.i_like
656656
)
657657
}

fluxc/src/main/java/org/wordpress/android/fluxc/model/comments/CommentsMapper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CommentsMapper @Inject constructor(
3737
}
3838
},
3939
authorProfileImageUrl = commentDto.author?.avatar_URL,
40-
remotePostId = commentDto.post?.ID ?: 0,
40+
remotePostId = commentDto.post?.ID ?: 0L,
4141
postTitle = StringEscapeUtils.unescapeHtml4(commentDto.post?.title),
4242
status = commentDto.status,
4343
datePublished = commentDto.date,
@@ -46,7 +46,7 @@ class CommentsMapper @Inject constructor(
4646
url = commentDto.URL,
4747
authorId = commentDto.author?.ID ?: 0L,
4848
hasParent = commentDto.parent != null,
49-
parentId = commentDto.parent?.ID ?: 0,
49+
parentId = commentDto.parent?.ID ?: 0L,
5050
iLike = commentDto.i_like
5151
)
5252
}
@@ -139,7 +139,7 @@ class CommentsMapper @Inject constructor(
139139
content = XMLRPCUtils.safeGetMapValue(commentMap, "content", ""),
140140
url = XMLRPCUtils.safeGetMapValue(commentMap, "link", ""),
141141
hasParent = remoteParentCommentId > 0,
142-
parentId = if (remoteParentCommentId > 0) remoteParentCommentId else 0,
142+
parentId = if (remoteParentCommentId > 0) remoteParentCommentId else 0L,
143143
iLike = false
144144
)
145145
}

fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpcom/comment/CommentRestClient.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ private CommentModel commentResponseToComment(
296296
comment.setPublishedTimestamp(DateTimeUtils.timestampFromIso8601(response.date));
297297

298298
if (response.author != null) {
299+
comment.setAuthorId(response.author.ID);
299300
comment.setAuthorUrl(response.author.URL);
300301
comment.setAuthorName(StringEscapeUtils.unescapeHtml4(response.author.name));
301302
if ("false".equals(response.author.email)) {
@@ -311,10 +312,6 @@ private CommentModel commentResponseToComment(
311312
comment.setPostTitle(StringEscapeUtils.unescapeHtml4(response.post.title));
312313
}
313314

314-
if (response.author != null) {
315-
comment.setAuthorId(response.author.ID);
316-
}
317-
318315
if (response.parent != null) {
319316
comment.setHasParent(true);
320317
comment.setParentId(response.parent.ID);
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
package org.wordpress.android.fluxc.network.rest.wpcom.comment;
22

3+
import androidx.annotation.NonNull;
4+
import androidx.annotation.Nullable;
5+
36
import java.util.List;
47

8+
@SuppressWarnings("NotNullFieldNotInitialized")
59
public class CommentWPComRestResponse {
610
public static class CommentsWPComRestResponse {
7-
public List<CommentWPComRestResponse> comments;
11+
@Nullable public List<CommentWPComRestResponse> comments;
812
}
913

1014
public static class Post {
1115
public long ID;
12-
public String title;
13-
public String type;
14-
public String link;
16+
@NonNull public String title;
17+
@NonNull public String type;
18+
@NonNull public String link;
1519
}
1620

1721
public static class Author {
1822
public long ID;
19-
public String email; // can be boolean "false" if not set
20-
public String name;
21-
public String URL;
22-
public String avatar_URL;
23+
@NonNull public String email; // can be boolean "false" if not set
24+
@NonNull public String name;
25+
@NonNull public String URL;
26+
@NonNull public String avatar_URL;
2327
}
2428

2529
public long ID;
26-
public CommentParent parent;
27-
public Post post;
28-
public Author author;
29-
public String date;
30-
public String status;
31-
public String content;
30+
@Nullable public CommentParent parent;
31+
@Nullable public Post post;
32+
@Nullable public Author author;
33+
@NonNull public String date;
34+
@NonNull public String status;
35+
@NonNull public String content;
3236
public boolean i_like;
33-
public String URL;
37+
@NonNull public String URL;
3438
}

0 commit comments

Comments
 (0)