From c6194670f18ec33cd9fa7c01e9d20871fc266c41 Mon Sep 17 00:00:00 2001 From: T3rm1 Date: Fri, 25 Oct 2024 18:33:08 +0200 Subject: [PATCH] fix: Include extended classes and implemented interfaces for JsonValue method search --- .../v3/core/jackson/ModelResolver.java | 4 +--- .../swagger/v3/core/util/ReflectionUtils.java | 23 +++++++++++++++++++ .../v3/core/converting/EnumPropertyTest.java | 8 ++++++- .../oas/models/InterfaceWithJacksonValue.java | 9 ++++++++ .../models/JacksonValueInInterfaceEnum.java | 18 +++++++++++++++ .../oas/models/ModelWithJacksonEnumField.java | 1 + 6 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/InterfaceWithJacksonValue.java create mode 100644 modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonValueInInterfaceEnum.java diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java index 58181bcdd7..97376e0dd3 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java @@ -1171,9 +1171,7 @@ protected void _addEnumProps(Class propClass, Schema property) { final boolean useIndex = _mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX); final boolean useToString = _mapper.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); - Optional jsonValueMethod = Arrays.stream(propClass.getDeclaredMethods()) - .filter(m -> m.isAnnotationPresent(JsonValue.class)) - .filter(m -> m.getAnnotation(JsonValue.class).value()) + Optional jsonValueMethod = ReflectionUtils.getAnnotatedMethods(propClass, JsonValue.class).stream() .findFirst(); Optional jsonValueField = Arrays.stream(propClass.getDeclaredFields()) diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java index f8b0c08a7d..66f0bf09d1 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java @@ -486,6 +486,29 @@ public static Optional safeGet(Field field, Object obj) { } catch (IllegalAccessException e) { return Optional.empty(); } + } + public static List getAnnotatedMethods(Class type, Class annotation) { + List methods = new ArrayList<>(); + for (Class clazz = type; clazz != Object.class; clazz = clazz.getSuperclass()) { + for (Method method : clazz.getDeclaredMethods()) { + if (method.isAnnotationPresent(annotation)) { + methods.add(method); + } + } + } + getAnnotatedMethodsFromInterfaces(type, annotation, methods); + return methods; + } + + private static void getAnnotatedMethodsFromInterfaces(Class type, Class annotation, List methods) { + for (Class iface : type.getInterfaces()) { + for (Method method : iface.getDeclaredMethods()) { + if (method.isAnnotationPresent(annotation)) { + methods.add(method); + } + } + getAnnotatedMethodsFromInterfaces(iface, annotation, methods); + } } } diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/EnumPropertyTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/EnumPropertyTest.java index 0893929fbf..ff705cd692 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/EnumPropertyTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/converting/EnumPropertyTest.java @@ -19,6 +19,7 @@ import org.testng.annotations.Test; import java.util.Arrays; +import java.util.Collections; import java.util.Map; import static org.testng.Assert.assertEquals; @@ -233,6 +234,11 @@ public void testExtractJacksonEnumFields() { final Schema sixthEnumProperty = (Schema) model.getProperties().get("sixthEnumValue"); assertTrue(sixthEnumProperty instanceof StringSchema); final StringSchema sixthStringProperty = (StringSchema) sixthEnumProperty; - assertEquals(sixthEnumProperty.getEnum(), Arrays.asList("one", "two", "three")); + assertEquals(sixthStringProperty.getEnum(), Arrays.asList("one", "two", "three")); + + final Schema seventhEnumProperty = (Schema) model.getProperties().get("seventhEnumValue"); + assertTrue(seventhEnumProperty instanceof StringSchema); + final StringSchema seventhEnumStringProperty = (StringSchema) seventhEnumProperty; + assertEquals(seventhEnumStringProperty.getEnum(), Collections.singletonList("10")); } } diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/InterfaceWithJacksonValue.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/InterfaceWithJacksonValue.java new file mode 100644 index 0000000000..9e4416675f --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/InterfaceWithJacksonValue.java @@ -0,0 +1,9 @@ +package io.swagger.v3.core.oas.models; + +import com.fasterxml.jackson.annotation.JsonValue; + +public interface InterfaceWithJacksonValue { + + @JsonValue + int getJsonValue(); +} diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonValueInInterfaceEnum.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonValueInInterfaceEnum.java new file mode 100644 index 0000000000..3b4e1a3885 --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonValueInInterfaceEnum.java @@ -0,0 +1,18 @@ + +package io.swagger.v3.core.oas.models; + +public enum JacksonValueInInterfaceEnum implements InterfaceWithJacksonValue { + + TEN(10); + + private final int jsonValue; + + JacksonValueInInterfaceEnum(int jsonValue) { + this.jsonValue = jsonValue; + } + + @Override + public int getJsonValue() { + return jsonValue; + } +} diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/ModelWithJacksonEnumField.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/ModelWithJacksonEnumField.java index d9e154085b..217c005be3 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/ModelWithJacksonEnumField.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/ModelWithJacksonEnumField.java @@ -10,4 +10,5 @@ public class ModelWithJacksonEnumField { public JacksonValueFieldEnum fourthEnumValue; public JacksonNumberValueFieldEnum fifthEnumValue; public JacksonValuePrivateEnum sixthEnumValue; + public JacksonValueInInterfaceEnum seventhEnumValue; }