Skip to content

Commit

Permalink
Fix the problem where extensions were not changed but still updated (#…
Browse files Browse the repository at this point in the history
…5887)

#### What type of PR is this?

/kind improvement
/area core
/milestone 2.16.x

#### What this PR does / why we need it:

This PR fixes the problem where extensions were not changed but still updated. What we want is to not update the extension if it has not changed.

Before that, we update the version of extension manually while getting the latest extension, this will lead to change the type of metadata.version from int to long.See the code snippet below:

https://github.com/halo-dev/halo/blob/a629961e8de4c086490e821248b6fa9964caecdd/application/src/main/java/run/halo/app/extension/JSONExtensionConverter.java#L83

Now, we force update the versions using type Long.

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

```release-note
None
```
  • Loading branch information
JohnNiang committed May 15, 2024
1 parent 587dafa commit 2341905
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ public <E extends Extension> Mono<E> update(E extension) {
newMetadata.setCreationTimestamp(oldMetadata.getCreationTimestamp());
newMetadata.setGenerateName(oldMetadata.getGenerateName());

// If the extension is an unstructured, the version type may be integer instead of long.
// reset metadata.version for long type.
oldMetadata.setVersion(oldMetadata.getVersion());
newMetadata.setVersion(newMetadata.getVersion());

if (Objects.equals(oldJsonExt, newJsonExt)) {
// skip updating if not data changed.
return Mono.just(extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,32 @@ void shouldNotUpdateIfExtensionNotChange() {
verify(storeClient, never()).update(any(), any(), any());
}

@Test
void shouldNotUpdateIfUnstructuredNotChange() throws JsonProcessingException {
var storeName = "/registry/fake.halo.run/fakes/fake";
var extensionStore = createExtensionStore(storeName, 2L);
when(storeClient.fetchByName(storeName)).thenReturn(
Mono.just(extensionStore));

var fakeJson = objectMapper.writeValueAsString(createFakeExtension("fake", 2L));
var oldFakeJson = objectMapper.writeValueAsString(createFakeExtension("fake", 2L));

var fake = objectMapper.readValue(fakeJson, Unstructured.class);
var oldFake = objectMapper.readValue(oldFakeJson, Unstructured.class);
oldFake.getMetadata().setVersion(2L);

when(converter.convertFrom(Unstructured.class, extensionStore)).thenReturn(oldFake);

StepVerifier.create(client.update(fake))
.expectNext(fake)
.verifyComplete();

verify(storeClient).fetchByName(storeName);
verify(converter).convertFrom(Unstructured.class, extensionStore);
verify(converter, never()).convertTo(any());
verify(storeClient, never()).update(any(), any(), any());
}

@Test
void shouldUpdateIfExtensionStatusChangedOnly() {
var fake = createFakeExtension("fake", 2L);
Expand Down

0 comments on commit 2341905

Please sign in to comment.