Skip to content

Commit

Permalink
fix: resolve unequal schemes built from same Extension blocking unreg…
Browse files Browse the repository at this point in the history
…ister (#5245)

#### What type of PR is this?
/kind bug
/area core
/milestone 2.12.x

#### What this PR does / why we need it:
修复同一个自定义模型构建出的 Scheme 不相等导致无法正确从 SchemeManager 中移除的问题

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

#### Does this PR introduce a user-facing change?
```release-note
修复同一个自定义模型构建出的 Scheme 不相等导致无法正确从 SchemeManager 中移除的问题
```
  • Loading branch information
guqing authored Jan 25, 2024
1 parent 0faa8a8 commit 29bd059
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/src/main/java/run/halo/app/extension/Scheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,21 @@ public static GVK getGvkFromType(@NonNull Class<? extends Extension> type) {
"Missing annotation " + GVK.class.getName() + " on type " + type.getName());
return gvk;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Scheme scheme = (Scheme) o;
return groupVersionKind.equals(scheme.groupVersionKind);
}

@Override
public int hashCode() {
return groupVersionKind.hashCode();
}
}
45 changes: 45 additions & 0 deletions application/src/test/java/run/halo/app/extension/SchemeTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package run.halo.app.extension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -66,4 +68,47 @@ void shouldCreateSchemeSuccessfully() {
assertEquals(FakeExtension.class, scheme.type());
}

@Test
void equalsAndHashCodeTest() {
var scheme1 = Scheme.buildFromType(FakeExtension.class);
var scheme2 = Scheme.buildFromType(FakeExtension.class);
assertEquals(scheme1, scheme2);
assertEquals(scheme1.hashCode(), scheme2.hashCode());

// openApiSchema is not included in equals and hashCode.
var scheme3 = new Scheme(FakeExtension.class, scheme1.groupVersionKind(),
scheme1.plural(), scheme1.singular(), JsonNodeFactory.instance.objectNode());
assertEquals(scheme1, scheme3);

// singular and plural are not included in equals and hashCode.
var scheme4 = new Scheme(FakeExtension.class, scheme1.groupVersionKind(),
scheme1.plural(), "other", scheme1.openApiSchema());
assertEquals(scheme1, scheme4);

// plural is not included in equals and hashCode.
var scheme5 = new Scheme(FakeExtension.class, scheme1.groupVersionKind(),
"other", scheme1.singular(), scheme1.openApiSchema());
assertEquals(scheme1, scheme5);

// type is not included in equals and hashCode.
var scheme6 = new Scheme(FakeExtension.class, scheme1.groupVersionKind(),
scheme1.plural(), scheme1.singular(), scheme1.openApiSchema());
assertEquals(scheme1, scheme6);

// groupVersionKind is included in equals and hashCode.
var scheme7 = new Scheme(FakeExtension.class,
new GroupVersionKind("other.halo.run", "v1alpha1", "Fake"),
scheme1.plural(), scheme1.singular(), scheme1.openApiSchema());
assertNotEquals(scheme1, scheme7);

scheme7 = new Scheme(FakeExtension.class,
new GroupVersionKind("fake.halo.run", "v1alpha2", "Fake"),
scheme1.plural(), scheme1.singular(), scheme1.openApiSchema());
assertNotEquals(scheme1, scheme7);

scheme7 = new Scheme(FakeExtension.class,
new GroupVersionKind("fake.halo.run", "v1alpha1", "Other"),
scheme1.plural(), scheme1.singular(), scheme1.openApiSchema());
assertNotEquals(scheme1, scheme7);
}
}

0 comments on commit 29bd059

Please sign in to comment.