Fix the problem where extensions were not changed but still updated (#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:

a629961e8d/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
```
pull/5928/head
John Niang 2024-05-15 15:14:34 +08:00 committed by GitHub
parent 587dafa66c
commit 2341905323
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 0 deletions

View File

@ -215,6 +215,11 @@ public class ReactiveExtensionClientImpl implements ReactiveExtensionClient {
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);

View File

@ -488,6 +488,32 @@ class ReactiveExtensionClientTest {
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);