Skip to content

Commit

Permalink
fix: Check enum interfaces for @JsonValue (smallrye#1992)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Sep 30, 2024
1 parent f3c2e39 commit a52d507
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
Expand All @@ -32,12 +32,18 @@ private EnumProcessor() {
}

public static List<Object> enumConstants(AnnotationScannerContext context, Type enumType) {
ClassInfo enumKlazz = context.getIndex().getClassByName(TypeUtil.getName(enumType));
AugmentedIndexView index = context.getAugmentedIndex();
ClassInfo enumKlazz = index.getClassByName(TypeUtil.getName(enumType));
Function<FieldInfo, String> nameTranslator = nameTranslator(context, enumKlazz);

return enumKlazz.annotationsMap()
.getOrDefault(JSON_VALUE, Collections.emptyList())
return index.inheritanceChain(enumKlazz, enumType)
.keySet()
.stream()
.flatMap(clazz -> Stream.concat(
Stream.of(clazz),
clazz.interfaceTypes().stream().map(index::getClass).filter(Objects::nonNull)))
.filter(clazz -> clazz.hasAnnotation(JSON_VALUE))
.flatMap(clazz -> clazz.annotationsMap().get(JSON_VALUE).stream())
// @JsonValue#value (default = true) allows for the functionality to be disabled
.filter(atJsonValue -> context.annotations().value(atJsonValue, PROP_VALUE, true))
.map(AnnotationInstance::target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@ void testEnumToSchemaTypeWithEmptyEnum() {
assertEquals(Arrays.asList("VAL1", "VAL2"), result.getEnumeration());
}

interface EnumValue4 {
@com.fasterxml.jackson.annotation.JsonValue
// type derived as int with format int64 due to JsonValue return type
boolean getValue();
}

@org.eclipse.microprofile.openapi.annotations.media.Schema
public static enum ExampleEnum4 implements EnumValue4 {
TRUE(true),
FALSE(false);

final boolean value;

ExampleEnum4(boolean value) {
this.value = value;
}

public boolean getValue() {
return value;
}
}

@Test
void testEnumToSchemaTypeWithInheritance() {
Index index = indexOf(ExampleEnum4.class, EnumValue4.class);
AnnotationScannerContext context = new AnnotationScannerContext(index, ClassLoaderUtil.getDefaultClassLoader(),
emptyConfig());
Schema result = SchemaFactory.enumToSchema(context, Type.create(ExampleEnum4.class));
assertEquals(Arrays.asList(Schema.SchemaType.BOOLEAN), result.getType());
assertEquals(Arrays.asList(true, false), result.getEnumeration());
}

@Test
void testParseSchemaType() {
for (SchemaType type : SchemaType.values()) {
Expand Down

0 comments on commit a52d507

Please sign in to comment.