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

Search for JsonValue in all classes and interfaces #4770

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -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<Method> jsonValueMethod = Arrays.stream(propClass.getDeclaredMethods())
.filter(m -> m.isAnnotationPresent(JsonValue.class))
.filter(m -> m.getAnnotation(JsonValue.class).value())
Optional<Method> jsonValueMethod = ReflectionUtils.getAnnotatedMethods(propClass, JsonValue.class).stream()
.findFirst();

Optional<Field> jsonValueField = Arrays.stream(propClass.getDeclaredFields())
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;
}