Skip to content

Commit

Permalink
Register @JsonNaming value for reflection
Browse files Browse the repository at this point in the history
geoand committed Jul 3, 2024
1 parent 7b817b5 commit 05e244a
Showing 3 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -83,6 +84,7 @@ public class JacksonProcessor {

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 JACKSON_NAMING = DotName.createSimple(JsonNaming.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");
@@ -298,6 +300,18 @@ void register(
.methods().fields().build());
}

// register @JsonNaming for reflection
Set<String> namingTypesNames = new HashSet<>();
for (AnnotationInstance namingInstance : index.getAnnotations(JSON_NAMING)) {
AnnotationValue namingValue = namingInstance.value();
if (namingValue != null) {
namingTypesNames.add(namingValue.asClass().name().toString());
}
}
if (!namingTypesNames.isEmpty()) {
reflectiveClass.produce(ReflectiveClassBuildItem.builder(namingTypesNames.toArray(EMPTY_STRING)).build());
}

// this needs to be registered manually since the runtime module is not indexed by Jandex
additionalBeans.produce(new AdditionalBeanBuildItem(ObjectMapperProducer.class));
}
Original file line number Diff line number Diff line change
@@ -5,11 +5,14 @@

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonTypeInfo(use = NAME, include = PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(value = Elephant.class, name = "Elephant"),
@JsonSubTypes.Type(value = Whale.class, name = "Whale"),
})
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public interface Mammal {
}
Original file line number Diff line number Diff line change
@@ -23,5 +23,6 @@ void test() {
.extract().response();
assertThat(response.jsonPath().getString("mammals[0].color")).isEqualTo("white");
assertThat(response.jsonPath().getString("mammals[1].continent")).isEqualTo("africa");
assertThat(response.jsonPath().getString("mammals[1].horn_length")).isEqualTo("10");
}
}

0 comments on commit 05e244a

Please sign in to comment.