Skip to content

Commit

Permalink
fix: Include extended classes and implemented interfaces for JsonValu…
Browse files Browse the repository at this point in the history
…e method search
  • Loading branch information
T3rm1 committed Oct 25, 2024
1 parent dc8785e commit c84ef80
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +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<Method> jsonValueMethod = Arrays.stream(propClass.getDeclaredMethods())
Optional<Method> jsonValueMethod = ReflectionUtils.getAnnotatedMethods(propClass, JsonValue.class).stream()
.filter(m -> m.isAnnotationPresent(JsonValue.class))
.filter(m -> m.getAnnotation(JsonValue.class).value())
.findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,29 @@ public static Optional<Object> safeGet(Field field, Object obj) {
} catch (IllegalAccessException e) {
return Optional.empty();
}
}

public static List<Method> getAnnotatedMethods(Class<?> type, Class<? extends Annotation> annotation) {
List<Method> 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<? extends Annotation> annotation, List<Method> methods) {
for (Class<?> iface : type.getInterfaces()) {
for (Method method : iface.getDeclaredMethods()) {
if (method.isAnnotationPresent(annotation)) {
methods.add(method);
}
}
getAnnotatedMethodsFromInterfaces(iface, annotation, methods);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.swagger.v3.core.oas.models;

import com.fasterxml.jackson.annotation.JsonValue;

public interface InterfaceWithJacksonValue {

@JsonValue
int getJsonValue();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class ModelWithJacksonEnumField {
public JacksonValueFieldEnum fourthEnumValue;
public JacksonNumberValueFieldEnum fifthEnumValue;
public JacksonValuePrivateEnum sixthEnumValue;
public JacksonValueInInterfaceEnum seventhEnumValue;
}

0 comments on commit c84ef80

Please sign in to comment.