Skip to content

Commit

Permalink
Make Kubernetes model for native registration more complete
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Aug 29, 2022
1 parent 5e67644 commit e823bd0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.jackson.deployment;

import java.util.List;

import org.jboss.jandex.DotName;

import io.quarkus.builder.item.MultiBuildItem;
Expand All @@ -10,13 +12,22 @@
*/
public final class IgnoreJsonDeserializeClassBuildItem extends MultiBuildItem {

private final DotName dotName;
private final List<DotName> dotNames;

public IgnoreJsonDeserializeClassBuildItem(DotName dotName) {
this.dotName = dotName;
this.dotNames = List.of(dotName);
}

public IgnoreJsonDeserializeClassBuildItem(List<DotName> dotNames) {
this.dotNames = dotNames;
}

@Deprecated(forRemoval = true)
public DotName getDotName() {
return dotName;
return dotNames.get(0);
}

public List<DotName> getDotNames() {
return dotNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void register(

Set<DotName> ignoredDotNames = new HashSet<>();
for (IgnoreJsonDeserializeClassBuildItem ignoreJsonDeserializeClassBuildItem : ignoreJsonDeserializeClassBuildItems) {
ignoredDotNames.add(ignoreJsonDeserializeClassBuildItem.getDotName());
ignoredDotNames.addAll(ignoreJsonDeserializeClassBuildItem.getDotNames());
}

// handle the various @JsonDeserialize cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public class KubernetesClientProcessor {
.createSimple("io.fabric8.kubernetes.client.informers.ResourceEventHandler");
private static final DotName KUBERNETES_RESOURCE = DotName
.createSimple("io.fabric8.kubernetes.api.model.KubernetesResource");
private static final DotName KUBERNETES_RESOURCE_LIST = DotName
.createSimple("io.fabric8.kubernetes.api.model.KubernetesResourceList");

private static final DotName VISITABLE_BUILDER = DotName
.createSimple("io.fabric8.kubernetes.api.builder.VisitableBuilder");
private static final DotName CUSTOM_RESOURCE = DotName.createSimple("io.fabric8.kubernetes.client.CustomResource");

private static final Logger log = Logger.getLogger(KubernetesClientProcessor.class.getName());
Expand Down Expand Up @@ -117,36 +122,28 @@ public void process(ApplicationIndexBuildItem applicationIndex, CombinedIndexBui

Collection<ClassInfo> kubernetesResourceImpls = combinedIndexBuildItem.getIndex()
.getAllKnownImplementors(KUBERNETES_RESOURCE);
Collection<ClassInfo> kubernetesResourceListImpls = combinedIndexBuildItem.getIndex()
.getAllKnownImplementors(KUBERNETES_RESOURCE_LIST);
Collection<ClassInfo> visitableBuilderImpls = combinedIndexBuildItem.getIndex()
.getAllKnownImplementors(VISITABLE_BUILDER);

// default sizes determined experimentally - these are only set in order to prevent continuous expansion of the array list
List<String> withoutFieldsRegistration = new ArrayList<>(kubernetesResourceImpls.size());
List<String> withoutFieldsRegistration = new ArrayList<>(
kubernetesResourceImpls.size() + kubernetesResourceListImpls.size());
List<String> withFieldsRegistration = new ArrayList<>(2);
kubernetesResourceImpls
.stream()
.peek(c -> {
// we need to make sure that the Jackson extension does not try to fully register the model classes
// since we are going to register them weakly
ignoredJsonDeserializationClasses.produce(new IgnoreJsonDeserializeClassBuildItem(c.name()));
})
.filter(c -> !watchedClasses.contains(c.name()))
.map(c -> {
boolean registerFields = false;
List<AnnotationInstance> jsonFormatInstances = c.annotationsMap().get(JSON_FORMAT);
if (jsonFormatInstances != null) {
for (AnnotationInstance jsonFormatInstance : jsonFormatInstances) {
if (jsonFormatInstance.target().kind() == AnnotationTarget.Kind.FIELD) {
registerFields = true;
break;
}
}
}
return new AbstractMap.SimpleEntry<>(c.name(), registerFields);
}).forEach(e -> {
if (e.getValue()) {
withFieldsRegistration.add(e.getKey().toString());
} else {
withoutFieldsRegistration.add(e.getKey().toString());
}
});
List<DotName> ignoreJsonDeserialization = new ArrayList<>(
kubernetesResourceImpls.size() + kubernetesResourceListImpls.size());

populateReflectionRegistrationLists(kubernetesResourceImpls, watchedClasses, ignoreJsonDeserialization,
withoutFieldsRegistration,
withFieldsRegistration);
populateReflectionRegistrationLists(kubernetesResourceListImpls, watchedClasses, ignoreJsonDeserialization,
withoutFieldsRegistration,
withFieldsRegistration);
populateReflectionRegistrationLists(visitableBuilderImpls, watchedClasses, ignoreJsonDeserialization,
withoutFieldsRegistration,
withFieldsRegistration);

if (!withFieldsRegistration.isEmpty()) {
reflectiveClasses.produce(ReflectiveClassBuildItem
.builder(withFieldsRegistration.toArray(EMPTY_STRINGS_ARRAY)).weak(true).methods(true).fields(true)
Expand All @@ -158,6 +155,8 @@ public void process(ApplicationIndexBuildItem applicationIndex, CombinedIndexBui
.build());
}

ignoredJsonDeserializationClasses.produce(new IgnoreJsonDeserializeClassBuildItem(ignoreJsonDeserialization));

// we also ignore some classes that are annotated with @JsonDeserialize that would force the registration of the entire model
ignoredJsonDeserializationClasses.produce(
new IgnoreJsonDeserializeClassBuildItem(DotName.createSimple("io.fabric8.kubernetes.api.model.KubeSchema")));
Expand Down Expand Up @@ -206,6 +205,40 @@ public void process(ApplicationIndexBuildItem applicationIndex, CombinedIndexBui
sslNativeSupport.produce(new ExtensionSslNativeSupportBuildItem(Feature.KUBERNETES_CLIENT));
}

private static void populateReflectionRegistrationLists(Collection<ClassInfo> kubernetesResourceImpls,
Set<DotName> watchedClasses,
List<DotName> ignoredJsonDeserializationClasses,
List<String> withoutFieldsRegistration,
List<String> withFieldsRegistration) {
kubernetesResourceImpls
.stream()
.peek(c -> {
// we need to make sure that the Jackson extension does not try to fully register the model classes
// since we are going to register them weakly
ignoredJsonDeserializationClasses.add(c.name());
})
.filter(c -> !watchedClasses.contains(c.name()))
.map(c -> {
boolean registerFields = false;
List<AnnotationInstance> jsonFormatInstances = c.annotationsMap().get(JSON_FORMAT);
if (jsonFormatInstances != null) {
for (AnnotationInstance jsonFormatInstance : jsonFormatInstances) {
if (jsonFormatInstance.target().kind() == AnnotationTarget.Kind.FIELD) {
registerFields = true;
break;
}
}
}
return new AbstractMap.SimpleEntry<>(c.name(), registerFields);
}).forEach(e -> {
if (e.getValue()) {
withFieldsRegistration.add(e.getKey().toString());
} else {
withoutFieldsRegistration.add(e.getKey().toString());
}
});
}

private void findWatchedClasses(final DotName implementedOrExtendedClass, final ApplicationIndexBuildItem applicationIndex,
final CombinedIndexBuildItem combinedIndexBuildItem, final Set<DotName> watchedClasses,
final int expectedGenericTypeCardinality, boolean isTargetClassAnInterface) {
Expand Down

0 comments on commit e823bd0

Please sign in to comment.