Skip to content
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 @@ -28,6 +28,7 @@

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Stream;
Expand All @@ -40,6 +41,8 @@
import capital.scalable.restdocs.snippet.StandardTableSnippet;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.MethodParameter;
import org.springframework.restdocs.operation.Operation;
import org.springframework.web.method.HandlerMethod;
Expand Down Expand Up @@ -88,6 +91,20 @@ protected FieldDescriptors createFieldDescriptors(Operation operation,
protected Type firstGenericType(MethodParameter param) {
Type type = param.getGenericParameterType();
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type actualArgument = parameterizedType.getActualTypeArguments()[0];
if(actualArgument instanceof Class) {
return actualArgument;
} else if(actualArgument instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable)actualArgument;
String variableName = typeVariable.getName();
Map<TypeVariable, Type> typeMap = GenericTypeResolver.getTypeVariableMap(param.getContainingClass());
for(TypeVariable tv : typeMap.keySet()) {
if(StringUtils.equals(tv.getName(), variableName)) {
return typeMap.get(tv);
}
}
}
return ((ParameterizedType) type).getActualTypeArguments()[0];
} else {
return Object.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,27 @@ public void comment() throws Exception {
.row("field4", "String", "true", "Method 4."));
}

@Test
public void genericSuperMethod() throws Exception{
HandlerMethod handlerMethod = createHandlerMethod("getItemsGeneric");
mockFieldComment(Item.class, "field1", "A string");
mockFieldComment(Item.class, "field2", "A decimal");

new JacksonResponseFieldSnippet().document(operationBuilder
.attribute(HandlerMethod.class.getName(), handlerMethod)
.attribute(ObjectMapper.class.getName(), mapper)
.attribute(JavadocReader.class.getName(), javadocReader)
.attribute(ConstraintReader.class.getName(), constraintReader)
.build());

assertThat(this.generatedSnippets.snippet(AUTO_RESPONSE_FIELDS)).is(
tableWithHeader("Path", "Type", "Optional", "Description")
.row("[].field1", "String", "true", "A string.")
.row("[].field2", "Decimal", "true", "A decimal."));

}


private void mockConstraintMessage(Class<?> type, String fieldName, String comment) {
when(constraintReader.getConstraintMessages(type, fieldName))
.thenReturn(singletonList(comment));
Expand Down Expand Up @@ -477,8 +498,28 @@ private HandlerMethod createHandlerMethod(String responseEntityItem)
return new HandlerMethod(new TestResource(), responseEntityItem);
}

public interface IGenericTestResource<T> {

List<T> getItemsGeneric();
}

public static abstract class GenericTestResource<E> implements IGenericTestResource<E>{

abstract E createGeneric();

@Override
public List<E> getItemsGeneric() {
return Collections.singletonList(createGeneric());
}
}

// actual method responses do not matter, they are here just for the illustration
private static class TestResource {
private static class TestResource extends GenericTestResource<Item>{

@Override
Item createGeneric() {
return new Item("test");
}

public Item getItem() {
return new Item("test");
Expand Down