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 https://github.com/halo-dev/halo/issues/7320 for more.

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/7320

#### Does this PR introduce a user-facing change?

```release-note
修复通过接口创建文章可能导致无法发布和删除的问题
```
pull/7328/head
John Niang 2025-04-02 16:07:54 +08:00 committed by GitHub
parent d8bfecb86b
commit 2a6bedc73d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -388,6 +388,10 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
} }
var content = contentWrapper.get(); var content = contentWrapper.get();
if (StringUtils.isAnyBlank(content.getContent(), content.getRaw())) {
return StringUtils.EMPTY;
}
var contentChecksum = Hashing.sha256().hashString(content.getContent(), UTF_8).toString(); var contentChecksum = Hashing.sha256().hashString(content.getContent(), UTF_8).toString();
var annotations = MetadataUtil.nullSafeAnnotations(post); var annotations = MetadataUtil.nullSafeAnnotations(post);
var oldChecksum = annotations.get(Constant.CONTENT_CHECKSUM_ANNO); var oldChecksum = annotations.get(Constant.CONTENT_CHECKSUM_ANNO);

View File

@ -106,6 +106,42 @@ class PostReconcilerTest {
assertThat(value.getStatus().getContributors()).isEqualTo(List.of("guqing", "zhangsan")); assertThat(value.getStatus().getContributors()).isEqualTo(List.of("guqing", "zhangsan"));
} }
@Test
void shouldGenerateBlankExcerptWhenContentIsNull() {
var name = "post-A";
Post post = TestPost.postV1();
post.getSpec().setPublish(true);
post.getSpec().setHeadSnapshot("post-A-head-snapshot");
post.getSpec().setReleaseSnapshot("post-fake-released-snapshot");
when(client.fetch(eq(Post.class), eq(name)))
.thenReturn(Optional.of(post));
when(postService.getContent(eq(post.getSpec().getReleaseSnapshot()),
eq(post.getSpec().getBaseSnapshot())))
.thenReturn(Mono.just(ContentWrapper.builder()
.snapshotName(post.getSpec().getHeadSnapshot())
.raw(null)
.content(null)
.rawType("markdown")
.build()));
Snapshot snapshotV2 = TestPost.snapshotV2();
snapshotV2.getMetadata().setLabels(new HashMap<>());
snapshotV2.getSpec().setContributors(Set.of("guqing", "zhangsan"));
Snapshot snapshotV1 = TestPost.snapshotV1();
snapshotV1.getSpec().setContributors(Set.of("guqing"));
when(client.listAll(eq(Snapshot.class), any(), any()))
.thenReturn(List.of(snapshotV1, snapshotV2));
ArgumentCaptor<Post> captor = ArgumentCaptor.forClass(Post.class);
postReconciler.reconcile(new Reconciler.Request(name));
verify(client, times(1)).update(captor.capture());
Post value = captor.getValue();
assertThat(value.getStatus().getExcerpt()).isEqualTo("");
}
@Test @Test
void reconcileExcerpt() { void reconcileExcerpt() {
// https://github.com/halo-dev/halo/issues/2452 // https://github.com/halo-dev/halo/issues/2452