Skip to content

Commit

Permalink
Merge pull request #37 from dayman/master
Browse files Browse the repository at this point in the history
Bugs fixing and minor improvements
  • Loading branch information
Will Kennedy committed Feb 25, 2014
2 parents 252e3fa + fa713bd commit 1fac516
Show file tree
Hide file tree
Showing 17 changed files with 521 additions and 559 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ public class ApiDocumentationController {

private String baseControllerPackage = "";
private List<String> additionalControllerPackages = new ArrayList<String>();

/**
* @deprecated no need in model packages
*/
private String baseModelPackage = "";

/**
* @deprecated no need in model packages
*/
private List<String> additionalModelPackages = new ArrayList<String>();
private String basePath = "";
private String apiVersion = "v1";
Expand All @@ -49,7 +57,8 @@ ApiListing getDocumentation(HttpServletRequest request) {
String handlerMappingPath = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
//trim the operation request mapping from the desired value
handlerMappingPath = handlerMappingPath.substring(handlerMappingPath.lastIndexOf("/doc") + 4, handlerMappingPath.length());
handlerMappingPath = handlerMappingPath
.substring(handlerMappingPath.lastIndexOf("/doc") + 4, handlerMappingPath.length());

Map<String, ApiListing> docs = getDocs(request);
if (docs == null) {
Expand Down Expand Up @@ -150,10 +159,10 @@ public void setApiVersion(String apiVersion) {
private Map<String, ApiListing> getDocs(HttpServletRequest request) {
if (this.documentation == null) {
String servletPath = null;
if(request != null) {
if (request != null) {
servletPath = request.getServletPath();
}
ApiParser apiParser = new ApiParserImpl(apiInfo, getControllerPackages(), getModelPackages(), getBasePath(),
ApiParser apiParser = new ApiParserImpl(apiInfo, getControllerPackages(), getBasePath(),
servletPath, apiVersion, ignorableAnnotations, ignoreUnusedPathVariables);
documentation = apiParser.createApiListings();
}
Expand All @@ -163,11 +172,11 @@ private Map<String, ApiListing> getDocs(HttpServletRequest request) {
private ResourceListing getResourceList(HttpServletRequest request) {
if (this.resourceList == null) {
String servletPath = null;
if(request != null) {
if (request != null) {
servletPath = request.getServletPath();
servletPath = servletPath.replace("/resourceList", "");
}
ApiParser apiParser = new ApiParserImpl(apiInfo, getControllerPackages(), getModelPackages(), getBasePath(),
ApiParser apiParser = new ApiParserImpl(apiInfo, getControllerPackages(), getBasePath(),
servletPath, apiVersion, ignorableAnnotations, ignoreUnusedPathVariables);
resourceList = apiParser.getResourceListing(getDocs(request));
}
Expand All @@ -181,30 +190,17 @@ public void setResourceList(ResourceListing resourceList) {

private List<String> getControllerPackages() {
List<String> controllerPackages = new ArrayList<String>();
if(baseControllerPackage != null && !baseControllerPackage.isEmpty()) {
if (baseControllerPackage != null && !baseControllerPackage.isEmpty()) {
controllerPackages.add(baseControllerPackage);
}

if(additionalControllerPackages != null && !additionalControllerPackages.isEmpty()) {
if (additionalControllerPackages != null && !additionalControllerPackages.isEmpty()) {
controllerPackages.addAll(additionalControllerPackages);
}

return controllerPackages;
}

private List<String> getModelPackages() {
List<String> modelPackages = new ArrayList<String>();
if(baseModelPackage != null && !baseModelPackage.isEmpty()) {
modelPackages.add(baseModelPackage);
}

if(additionalModelPackages != null && !additionalModelPackages.isEmpty()) {
modelPackages.addAll(additionalModelPackages);
}

return modelPackages;
}

@SuppressWarnings("unused")
public List<String> getIgnorableAnnotations() {
return ignorableAnnotations;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.knappsack.swagger4springweb.model;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class AnnotatedParameter {

private String parameterName;
private Class<?> parameterType;
private Class<?> parameterClass;
private Type parameterType;
private List<Annotation> annotations = new ArrayList<Annotation>();

public String getParameterName() {
Expand All @@ -18,12 +20,12 @@ public void setParameterName(String parameterName) {
this.parameterName = parameterName;
}

public Class<?> getParameterType() {
return parameterType;
public Class<?> getParameterClass() {
return parameterClass;
}

public void setParameterType(Class<?> parameterType) {
this.parameterType = parameterType;
public void setParameterClass(Class<?> parameterClass) {
this.parameterClass = parameterClass;
}

public List<Annotation> getAnnotations() {
Expand All @@ -34,7 +36,15 @@ public void addAnnotation(Annotation annotation) {
this.annotations.add(annotation);
}

public void addAnnotations(List<Annotation> annotations) {
public void addAnnotations(List<Annotation> annotations) {
this.annotations.addAll(annotations);
}
}

public Type getParameterType() {
return parameterType;
}

public void setParameterType(final Type parameterType) {
this.parameterType = parameterType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ApiDescriptionParser {
* @param resourcePath String - the path of this API. For Spring MVC this would be the value of the RequestMapping
* @return ApiDescription
*/
public ApiDescription getApiDescription(Method method, String description, String resourcePath) {
public ApiDescription parseApiDescription(Method method, String description, String resourcePath) {
String requestMappingValue = AnnotationUtils.getMethodRequestMappingValue(method);
String path;
if (resourcePath != null && !resourcePath.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,26 @@
package com.knappsack.swagger4springweb.parser;

import com.knappsack.swagger4springweb.model.AnnotatedParameter;
import com.knappsack.swagger4springweb.util.AnnotationUtils;
import com.wordnik.swagger.converter.SwaggerSchemaConverter;
import com.knappsack.swagger4springweb.util.ModelUtils;
import com.wordnik.swagger.model.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import scala.Option;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.lang.reflect.Type;
import java.util.Map;

public class ApiModelParser {

public Map<String, Model> getResponseBodyModels(Method method) {
Map<String, Model> documentationSchemaMap = new HashMap<String, Model>();
if(method.getAnnotation(ResponseBody.class) != null) {
Class<?> returnType = method.getReturnType();
SwaggerSchemaConverter parser = new SwaggerSchemaConverter();
String schemaName;
if(returnType.isArray()) {
//TODO - possibly reinvestigate what we should do in the case of an array
//parser = new ApiModelParser(returnType.getComponentType());
schemaName = returnType.getComponentType().getSimpleName();
} else {
schemaName = returnType.getSimpleName();
}
Option<Model> model = parser.read(returnType);
if(model.nonEmpty()) {
documentationSchemaMap.put(schemaName, model.get());
}
}
private final Map<String, Model> models;

return documentationSchemaMap;
public ApiModelParser(final Map<String, Model> models) {
this.models = models;
}

public Map<String, Model> getParameterModels(Method method) {

Map<String, Model> documentationSchemaMap = new HashMap<String, Model>();
public void parseResponseBodyModels(Method method) {
if (method.getAnnotation(ResponseBody.class) != null) {
Type type = method.getGenericReturnType();

List<AnnotatedParameter> annotatedParameters = AnnotationUtils.getAnnotatedParameters(method);
for (AnnotatedParameter annotatedParameter : annotatedParameters) {
Class<?> parameterType = annotatedParameter.getParameterType();
SwaggerSchemaConverter parser = new SwaggerSchemaConverter();
Option<Model> model = parser.read(parameterType);
if(model.nonEmpty()) {
documentationSchemaMap.put(parameterType.getSimpleName(), model.get());
}
ModelUtils.addModels(type, models);
}

return documentationSchemaMap;
}
}
Loading

0 comments on commit 1fac516

Please sign in to comment.