Skip to content

Commit

Permalink
fix #3923: removing kind only from registered classes
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and manusa committed Nov 21, 2022
1 parent 7b2426b commit 97f53c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -113,7 +114,7 @@ private static KubernetesResource fromObjectNode(JsonParser jp, JsonNode node) t
TypeKey key = getKey(node);
Class<? extends KubernetesResource> resourceType = mapping.getForKey(key);
if (resourceType == null) {
if (key == null || (key.version == null && key.apiGroup == null)) {
if (key == null) {
// just a wrapper around a map
// if this raw mapping typed as HasMetadata, a failure will result
return jp.getCodec().treeToValue(node, RawExtension.class);
Expand Down Expand Up @@ -171,24 +172,21 @@ public Class<? extends KubernetesResource> getForKey(TypeKey key) {
}

public void registerKind(String apiVersion, String kind, Class<? extends KubernetesResource> clazz) {
mappings.put(createKey(apiVersion, kind), clazz);
Optional.ofNullable(createKey(apiVersion, kind)).ifPresent(k -> mappings.put(k, clazz));
}

/**
* Returns a composite key for apiVersion and kind.
*/
TypeKey createKey(String apiVersion, String kind) {
if (kind == null) {
if (kind == null || apiVersion == null) {
return null;
} else if (apiVersion == null) {
return new TypeKey(kind, null, null);
} else {
String[] versionParts = new String[] { null, apiVersion };
if (apiVersion.contains("/")) {
versionParts = apiVersion.split("/", 2);
}
return new TypeKey(kind, versionParts[0], versionParts[1]);
}
String[] versionParts = new String[] { null, apiVersion };
if (apiVersion.contains("/")) {
versionParts = apiVersion.split("/", 2);
}
return new TypeKey(kind, versionParts[0], versionParts[1]);
}

private void registerClasses(ClassLoader classLoader) {
Expand All @@ -209,11 +207,14 @@ TypeKey getKeyFromClass(Class<? extends KubernetesResource> clazz) {
} else if (apiVersion != null && !apiVersion.isEmpty()) {
return createKey(apiVersion, kind);
}
return new TypeKey(kind, null, null);
return null;
}

private void addMapping(Class<? extends KubernetesResource> clazz) {
TypeKey keyFromClass = getKeyFromClass(clazz);
if (keyFromClass == null) {
return;
}
mappings.put(keyFromClass, clazz);

// oc behavior - allow resolving against just the version
Expand Down
Expand Up @@ -52,17 +52,7 @@ void shouldRegisterKind() {
}

@Test
void shouldNPEIfRegisterNullKind() {
// given
// when
assertThrows(NullPointerException.class, () -> {
mapping.registerKind("version1", null, SmurfResource.class);
});
// then throws
}

@Test
void shouldRegisterKindWithoutVersionIfNullVersion() {
void shouldNotRegisterKindWithoutVersionIfNullVersion() {
// given
String version = null;
String kind = "kind1";
Expand All @@ -71,7 +61,7 @@ void shouldRegisterKindWithoutVersionIfNullVersion() {
mapping.registerKind(version, kind, SmurfResource.class);
// then
Class<? extends KubernetesResource> clazz = mapping.getForKey(key);
assertThat(clazz).isEqualTo(SmurfResource.class);
assertThat(clazz).isNull();
}

@Test
Expand Down

0 comments on commit 97f53c7

Please sign in to comment.