From 64818a70c1d8151d8ef421b21239e53737f9b14f Mon Sep 17 00:00:00 2001 From: Mario Martins Date: Fri, 5 Jan 2024 16:09:57 -0800 Subject: [PATCH] Adding Spring Data Rest Controllers Support and Generics --- pom.xml | 51 +++---------------- .../SpringMavenDocumentSource.java | 2 + .../docgen/reader/SpringMvcApiReader.java | 39 +++++++++----- 3 files changed, 33 insertions(+), 59 deletions(-) diff --git a/pom.xml b/pom.xml index 5672e7b40..b85733ae7 100644 --- a/pom.xml +++ b/pom.xml @@ -291,6 +291,12 @@ ${kotlin.version} test + + org.springframework.data + spring-data-rest-webmvc + 3.7.14 + compile + @@ -312,51 +318,6 @@ - - org.apache.maven.plugins - maven-plugin-plugin - ${version.maven-plugin-plugin} - - - true - - - - mojo-descriptor - - descriptor - - - - - help-goal - - helpmojo - - - - - - org.codehaus.mojo - animal-sniffer-maven-plugin - ${version.animal-sniffer-plugin} - - - org.codehaus.mojo.signature - java16-sun - 1.0 - - - - - check-java16-sun - test - - check - - - - org.jetbrains.kotlin kotlin-maven-plugin diff --git a/src/main/java/com/github/kongchen/swagger/docgen/mavenplugin/SpringMavenDocumentSource.java b/src/main/java/com/github/kongchen/swagger/docgen/mavenplugin/SpringMavenDocumentSource.java index 57606df54..b9417ea8b 100644 --- a/src/main/java/com/github/kongchen/swagger/docgen/mavenplugin/SpringMavenDocumentSource.java +++ b/src/main/java/com/github/kongchen/swagger/docgen/mavenplugin/SpringMavenDocumentSource.java @@ -4,6 +4,7 @@ import com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; +import org.springframework.data.rest.webmvc.RepositoryRestController; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.RestController; @@ -25,6 +26,7 @@ public SpringMavenDocumentSource(ApiSource apiSource, Log log, String encoding) protected Set> getValidClasses() { Set> result = super.getValidClasses(); result.addAll(apiSource.getValidClasses(RestController.class)); + result.addAll(apiSource.getValidClasses(RepositoryRestController.class)); result.addAll(apiSource.getValidClasses(ControllerAdvice.class)); return result; } diff --git a/src/main/java/com/github/kongchen/swagger/docgen/reader/SpringMvcApiReader.java b/src/main/java/com/github/kongchen/swagger/docgen/reader/SpringMvcApiReader.java index fa3b81669..aedee14fe 100644 --- a/src/main/java/com/github/kongchen/swagger/docgen/reader/SpringMvcApiReader.java +++ b/src/main/java/com/github/kongchen/swagger/docgen/reader/SpringMvcApiReader.java @@ -1,19 +1,41 @@ package com.github.kongchen.swagger.docgen.reader; +import static org.apache.commons.lang3.StringUtils.defaultIfEmpty; +import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation; +import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; + import com.github.kongchen.swagger.docgen.GenerateException; import com.github.kongchen.swagger.docgen.spring.SpringResource; import com.github.kongchen.swagger.docgen.spring.SpringSwaggerExtension; import com.github.kongchen.swagger.docgen.util.SpringUtils; -import io.swagger.annotations.*; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Authorization; +import io.swagger.annotations.AuthorizationScope; import io.swagger.converter.ModelConverters; import io.swagger.jaxrs.ext.SwaggerExtension; import io.swagger.jaxrs.ext.SwaggerExtensions; -import io.swagger.models.*; +import io.swagger.models.Model; +import io.swagger.models.Operation; +import io.swagger.models.Response; +import io.swagger.models.SecurityRequirement; +import io.swagger.models.Swagger; import io.swagger.models.Tag; import io.swagger.models.parameters.Parameter; import io.swagger.models.properties.Property; import io.swagger.models.properties.RefProperty; import io.swagger.util.BaseReaderUtils; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; import org.apache.maven.plugin.logging.Log; import org.codehaus.plexus.util.StringUtils; import org.springframework.core.DefaultParameterNameDiscoverer; @@ -22,16 +44,6 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseStatus; -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.*; - -import static org.apache.commons.lang3.StringUtils.defaultIfEmpty; -import static org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation; -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; - public class SpringMvcApiReader extends AbstractReader implements ClassSwaggerReader { private static final ResponseContainerConverter RESPONSE_CONTAINER_CONVERTER = new ResponseContainerConverter(); @@ -285,14 +297,13 @@ private Operation parseMethod(Method method, RequestMethod requestMethod) { // process parameters Class[] parameterTypes = method.getParameterTypes(); - Type[] genericParameterTypes = method.getGenericParameterTypes(); Annotation[][] paramAnnotations = method.getParameterAnnotations(); DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer(); String[] parameterNames = parameterNameDiscoverer.getParameterNames(method); // paramTypes = method.getParameterTypes // genericParamTypes = method.getGenericParameterTypes for (int i = 0; i < parameterTypes.length; i++) { - Type type = genericParameterTypes[i]; + Type type = parameterTypes[i]; List annotations = Arrays.asList(paramAnnotations[i]); List parameters = getParameters(type, annotations);