Skip to content

Commit

Permalink
Register JsonSubTypes.Type values for native mode
Browse files Browse the repository at this point in the history
Fixes: quarkusio#37942
(cherry picked from commit a110cea)
  • Loading branch information
geoand authored and gsmet committed Jan 9, 2024
1 parent 7430ec6 commit 5779695
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jboss.jandex.Type;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.SimpleObjectIdResolver;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
Expand Down Expand Up @@ -79,7 +80,7 @@ public class JacksonProcessor {
private static final DotName JSON_AUTO_DETECT = DotName.createSimple(JsonAutoDetect.class.getName());

private static final DotName JSON_TYPE_ID_RESOLVER = DotName.createSimple(JsonTypeIdResolver.class.getName());

private static final DotName JSON_SUBTYPES = DotName.createSimple(JsonSubTypes.class.getName());
private static final DotName JSON_CREATOR = DotName.createSimple("com.fasterxml.jackson.annotation.JsonCreator");

private static final DotName JSON_NAMING = DotName.createSimple("com.fasterxml.jackson.databind.annotation.JsonNaming");
Expand All @@ -98,6 +99,7 @@ public class JacksonProcessor {
// this list can probably be enriched with more modules
private static final List<String> MODULES_NAMES_TO_AUTO_REGISTER = Arrays.asList(TIME_MODULE, JDK8_MODULE,
PARAMETER_NAMES_MODULE);
private static final String[] EMPTY_STRING = new String[0];

@Inject
CombinedIndexBuildItem combinedIndexBuildItem;
Expand Down Expand Up @@ -266,6 +268,25 @@ void register(
}
}

// register @JsonSubTypes.Type values for reflection
Set<String> subTypeTypesNames = new HashSet<>();
for (AnnotationInstance subTypeInstance : index.getAnnotations(JSON_SUBTYPES)) {
AnnotationValue subTypeValue = subTypeInstance.value();
if (subTypeValue != null) {
for (AnnotationInstance subTypeTypeInstance : subTypeValue.asNestedArray()) {
AnnotationValue subTypeTypeValue = subTypeTypeInstance.value();
if (subTypeTypeValue != null) {
subTypeTypesNames.add(subTypeTypeValue.asClass().name().toString());
}
}

}
}
if (!subTypeTypesNames.isEmpty()) {
reflectiveClass.produce(ReflectiveClassBuildItem.builder(subTypeTypesNames.toArray(EMPTY_STRING))
.methods().fields().build());
}

// this needs to be registered manually since the runtime module is not indexed by Jandex
additionalBeans.produce(new AdditionalBeanBuildItem(ObjectMapperProducer.class));
}
Expand Down

0 comments on commit 5779695

Please sign in to comment.