-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…1589) Signed-off-by: Michael Edgar <[email protected]>
- Loading branch information
Showing
9 changed files
with
258 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/EnumProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.smallrye.openapi.runtime.scanner.dataobject; | ||
|
||
import static io.smallrye.openapi.api.constants.JacksonConstants.ENUM_NAMING; | ||
import static io.smallrye.openapi.api.constants.JacksonConstants.JSON_PROPERTY; | ||
import static io.smallrye.openapi.api.constants.JacksonConstants.JSON_VALUE; | ||
import static io.smallrye.openapi.api.constants.JacksonConstants.PROP_VALUE; | ||
|
||
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 org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.AnnotationTarget; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.FieldInfo; | ||
import org.jboss.jandex.Type; | ||
|
||
import io.smallrye.openapi.runtime.scanner.spi.AnnotationScannerContext; | ||
import io.smallrye.openapi.runtime.util.Annotations; | ||
import io.smallrye.openapi.runtime.util.JandexUtil; | ||
import io.smallrye.openapi.runtime.util.TypeUtil; | ||
|
||
public class EnumProcessor { | ||
|
||
private static final int ENUM = 0x00004000; // see java.lang.reflect.Modifier#ENUM | ||
|
||
private EnumProcessor() { | ||
} | ||
|
||
public static List<Object> enumConstants(AnnotationScannerContext context, Type enumType) { | ||
ClassInfo enumKlazz = context.getIndex().getClassByName(TypeUtil.getName(enumType)); | ||
Function<FieldInfo, String> nameTranslator = nameTranslator(context, enumKlazz); | ||
|
||
return enumKlazz.annotationsMap() | ||
.getOrDefault(JSON_VALUE, Collections.emptyList()) | ||
.stream() | ||
// @JsonValue#value (default = true) allows for the functionality to be disabled | ||
.filter(atJsonValue -> Annotations.value(atJsonValue, PROP_VALUE, true)) | ||
.map(AnnotationInstance::target) | ||
.filter(JandexUtil::isSupplier) | ||
.map(valueTarget -> jacksonJsonValues(context, enumKlazz, valueTarget)) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElseGet(() -> JandexUtil.fields(context, enumKlazz) | ||
.stream() | ||
.filter(field -> (field.flags() & ENUM) != 0) | ||
.map(nameTranslator::apply) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
private static List<Object> jacksonJsonValues(AnnotationScannerContext context, ClassInfo enumKlazz, | ||
AnnotationTarget valueTarget) { | ||
String className = enumKlazz.name().toString(); | ||
String methodName = valueTarget.asMethod().name(); | ||
|
||
try { | ||
Class<?> loadedEnum = Class.forName(className, false, context.getClassLoader()); | ||
Method valueMethod = loadedEnum.getDeclaredMethod(methodName); | ||
Object[] constants = loadedEnum.getEnumConstants(); | ||
|
||
List<Object> reflectedEnumeration = new ArrayList<>(constants.length); | ||
|
||
for (Object constant : constants) { | ||
reflectedEnumeration.add(valueMethod.invoke(constant)); | ||
} | ||
|
||
return reflectedEnumeration; | ||
} catch (Exception e) { | ||
DataObjectLogging.logger.exceptionReadingEnumJsonValue(className, methodName, e); | ||
} | ||
|
||
return null; // NOSONAR | ||
} | ||
|
||
private static Function<FieldInfo, String> nameTranslator(AnnotationScannerContext context, ClassInfo enumKlazz) { | ||
return Optional.<Type> ofNullable(Annotations.getAnnotationValue(enumKlazz, ENUM_NAMING, PROP_VALUE)) | ||
.map(namingClass -> namingClass.name().toString()) | ||
.map(namingClass -> PropertyNamingStrategyFactory.getStrategy(namingClass, context.getClassLoader())) | ||
.<Function<FieldInfo, String>> map(nameStrategy -> fieldInfo -> nameStrategy.apply(fieldInfo.name())) | ||
.orElse(fieldInfo -> Optional | ||
.<String> ofNullable(Annotations.getAnnotationValue(fieldInfo, JSON_PROPERTY, PROP_VALUE)) | ||
.orElseGet(fieldInfo::name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
core/src/test/java/io/smallrye/openapi/runtime/scanner/dataobject/EnumNamingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package io.smallrye.openapi.runtime.scanner.dataobject; | ||
|
||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
import org.eclipse.microprofile.openapi.models.OpenAPI; | ||
import org.jboss.jandex.Index; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.smallrye.openapi.runtime.scanner.IndexScannerTestBase; | ||
import io.smallrye.openapi.runtime.scanner.OpenApiAnnotationScanner; | ||
|
||
class EnumNamingTest extends IndexScannerTestBase { | ||
|
||
static void test(Class<?>... classes) throws Exception { | ||
Index index = indexOf(classes); | ||
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); | ||
OpenAPI result = scanner.scan(); | ||
|
||
printToConsole(result); | ||
assertJsonEquals("components.schemas.enum-naming.json", result); | ||
} | ||
|
||
@Test | ||
void testEnumNamingDefault() throws Exception { | ||
@Schema(name = "Bean") | ||
class Bean { | ||
@SuppressWarnings("unused") | ||
DaysOfWeekDefault days; | ||
} | ||
|
||
test(Bean.class, DaysOfWeekDefault.class); | ||
} | ||
|
||
@Test | ||
void testEnumNamingValueMethod() throws Exception { | ||
@Schema(name = "Bean") | ||
class Bean { | ||
@SuppressWarnings("unused") | ||
DaysOfWeekValue days; | ||
} | ||
|
||
test(Bean.class, DaysOfWeekValue.class); | ||
} | ||
|
||
@Test | ||
void testEnumNamingStrategy() throws Exception { | ||
@Schema(name = "Bean") | ||
class Bean { | ||
@SuppressWarnings("unused") | ||
DaysOfWeekStrategy days; | ||
} | ||
|
||
test(Bean.class, DaysOfWeekStrategy.class); | ||
} | ||
|
||
@Test | ||
void testEnumNamingProperty() throws Exception { | ||
@Schema(name = "Bean") | ||
class Bean { | ||
@SuppressWarnings("unused") | ||
DaysOfWeekProperty days; | ||
} | ||
|
||
test(Bean.class, DaysOfWeekProperty.class); | ||
} | ||
|
||
@Schema(name = "DaysOfWeek") | ||
enum DaysOfWeekDefault { | ||
Monday, | ||
Tuesday, | ||
Wednesday, | ||
Thursday, | ||
Friday, | ||
Saturday, | ||
Sunday | ||
} | ||
|
||
@Schema(name = "DaysOfWeek") | ||
enum DaysOfWeekValue { | ||
MONDAY, | ||
TUESDAY, | ||
WEDNESDAY, | ||
THURSDAY, | ||
FRIDAY, | ||
SATURDAY, | ||
SUNDAY; | ||
|
||
@com.fasterxml.jackson.annotation.JsonValue | ||
public String toValue() { | ||
String name = name(); | ||
return name.charAt(0) + name.substring(1).toLowerCase(); | ||
} | ||
} | ||
|
||
@Schema(name = "DaysOfWeek") | ||
@com.fasterxml.jackson.databind.annotation.EnumNaming(DaysOfWeekShortNaming.class) | ||
enum DaysOfWeekStrategy { | ||
MON("Monday"), | ||
TUE("Tuesday"), | ||
WED("Wednesday"), | ||
THU("Thursday"), | ||
FRI("Friday"), | ||
SAT("Saturday"), | ||
SUN("Sunday"); | ||
|
||
final String displayName; | ||
|
||
private DaysOfWeekStrategy(String displayName) { | ||
this.displayName = displayName; | ||
} | ||
} | ||
|
||
public static class DaysOfWeekShortNaming implements com.fasterxml.jackson.databind.EnumNamingStrategy { | ||
@Override | ||
public String convertEnumToExternalName(String enumName) { | ||
return DaysOfWeekStrategy.valueOf(enumName).displayName; | ||
} | ||
} | ||
|
||
@Schema(name = "DaysOfWeek") | ||
enum DaysOfWeekProperty { | ||
@com.fasterxml.jackson.annotation.JsonProperty("Monday") | ||
MONDAY, | ||
@com.fasterxml.jackson.annotation.JsonProperty("Tuesday") | ||
TUESDAY, | ||
@com.fasterxml.jackson.annotation.JsonProperty("Wednesday") | ||
WEDNESDAY, | ||
@com.fasterxml.jackson.annotation.JsonProperty("Thursday") | ||
THURSDAY, | ||
@com.fasterxml.jackson.annotation.JsonProperty("Friday") | ||
FRIDAY, | ||
@com.fasterxml.jackson.annotation.JsonProperty("Saturday") | ||
SATURDAY, | ||
@com.fasterxml.jackson.annotation.JsonProperty("Sunday") | ||
SUNDAY | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...rc/test/resources/io/smallrye/openapi/runtime/scanner/components.schemas.enum-naming.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"openapi" : "3.0.3", | ||
"components" : { | ||
"schemas" : { | ||
"Bean" : { | ||
"type" : "object", | ||
"properties" : { | ||
"days" : { | ||
"$ref" : "#/components/schemas/DaysOfWeek" | ||
} | ||
} | ||
}, | ||
"DaysOfWeek" : { | ||
"enum" : [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ], | ||
"type" : "string" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters