Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check enum interfaces for @JsonValue #1992

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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