Skip to content

Commit 2a6bedc

Browse files
authored
Fix the NPE while post content is null (#7321)
#### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: This PR fixes the NPE while post content is null. See #7320 for more. #### Which issue(s) this PR fixes: Fixes #7320 #### Does this PR introduce a user-facing change? ```release-note 修复通过接口创建文章可能导致无法发布和删除的问题 ```
1 parent d8bfecb commit 2a6bedc

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Diff for: application/src/main/java/run/halo/app/core/reconciler/PostReconciler.java

+4
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ private String getExcerpt(Post post) {
388388
}
389389
var content = contentWrapper.get();
390390

391+
if (StringUtils.isAnyBlank(content.getContent(), content.getRaw())) {
392+
return StringUtils.EMPTY;
393+
}
394+
391395
var contentChecksum = Hashing.sha256().hashString(content.getContent(), UTF_8).toString();
392396
var annotations = MetadataUtil.nullSafeAnnotations(post);
393397
var oldChecksum = annotations.get(Constant.CONTENT_CHECKSUM_ANNO);

Diff for: application/src/test/java/run/halo/app/core/reconciler/PostReconcilerTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,42 @@ void reconcile() {
106106
assertThat(value.getStatus().getContributors()).isEqualTo(List.of("guqing", "zhangsan"));
107107
}
108108

109+
@Test
110+
void shouldGenerateBlankExcerptWhenContentIsNull() {
111+
var name = "post-A";
112+
Post post = TestPost.postV1();
113+
post.getSpec().setPublish(true);
114+
post.getSpec().setHeadSnapshot("post-A-head-snapshot");
115+
post.getSpec().setReleaseSnapshot("post-fake-released-snapshot");
116+
when(client.fetch(eq(Post.class), eq(name)))
117+
.thenReturn(Optional.of(post));
118+
when(postService.getContent(eq(post.getSpec().getReleaseSnapshot()),
119+
eq(post.getSpec().getBaseSnapshot())))
120+
.thenReturn(Mono.just(ContentWrapper.builder()
121+
.snapshotName(post.getSpec().getHeadSnapshot())
122+
.raw(null)
123+
.content(null)
124+
.rawType("markdown")
125+
.build()));
126+
127+
Snapshot snapshotV2 = TestPost.snapshotV2();
128+
snapshotV2.getMetadata().setLabels(new HashMap<>());
129+
snapshotV2.getSpec().setContributors(Set.of("guqing", "zhangsan"));
130+
131+
Snapshot snapshotV1 = TestPost.snapshotV1();
132+
snapshotV1.getSpec().setContributors(Set.of("guqing"));
133+
134+
when(client.listAll(eq(Snapshot.class), any(), any()))
135+
.thenReturn(List.of(snapshotV1, snapshotV2));
136+
137+
ArgumentCaptor<Post> captor = ArgumentCaptor.forClass(Post.class);
138+
postReconciler.reconcile(new Reconciler.Request(name));
139+
140+
verify(client, times(1)).update(captor.capture());
141+
Post value = captor.getValue();
142+
assertThat(value.getStatus().getExcerpt()).isEqualTo("");
143+
}
144+
109145
@Test
110146
void reconcileExcerpt() {
111147
// https://github.com/halo-dev/halo/issues/2452

0 commit comments

Comments
 (0)