Skip to content

Commit

Permalink
Arguments marked with @ModelAttribute were ignored (should have param…
Browse files Browse the repository at this point in the history
…Type="form"). That caused crash in swagger-ui.

baseModelPackage and additionaModelPackages were removed since there were unnecessary.
All models are now adding on the go, that increased speed and amount of data sent to client (swagger ui).
  • Loading branch information
Andrey Antonov committed Feb 21, 2014
1 parent 27dd93d commit fa713bd
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 293 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,24 +1,26 @@
package com.knappsack.swagger4springweb.parser;

import com.knappsack.swagger4springweb.util.ApiUtils;
import com.knappsack.swagger4springweb.util.ModelUtils;
import com.wordnik.swagger.model.Model;
import org.springframework.web.bind.annotation.ResponseBody;

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

public class ApiModelParser {

public Map<String, Model> getResponseBodyModels(Method method) {
Map<String, Model> models = new HashMap<String, Model>();
private final Map<String, Model> models;

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

public void parseResponseBodyModels(Method method) {
if (method.getAnnotation(ResponseBody.class) != null) {
Type type = method.getGenericReturnType();

ApiUtils.addModels(type, models);
ModelUtils.addModels(type, models);
}

return models;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static java.lang.String.format;

public class ApiOperationParser {

private final Map<String, Model> models;
private String resourcePath;
private List<String> ignorableAnnotations;
private boolean ignoreUnusedPathVariables;

public ApiOperationParser(String resourcePath, List<String> ignorableAnnotations,
boolean ignoreUnusedPathVariables) {
boolean ignoreUnusedPathVariables, Map<String, Model> models) {
this.ignorableAnnotations = ignorableAnnotations;
this.ignoreUnusedPathVariables = ignoreUnusedPathVariables;
this.resourcePath = resourcePath;
this.models = models;
}

public Operation getDocumentationOperation(Method method) {
public Operation parseDocumentationOperation(Method method) {

DocumentationOperation documentationOperation = new DocumentationOperation();
documentationOperation.setNickname(method.getName());// method name
Expand Down Expand Up @@ -106,8 +109,8 @@ public Operation getDocumentationOperation(Method method) {
}
}

ApiParameterParser apiParameterParser = new ApiParameterParser(ignorableAnnotations);
List<Parameter> documentationParameters = apiParameterParser.getApiParameters(method);
ApiParameterParser apiParameterParser = new ApiParameterParser(ignorableAnnotations, models);
List<Parameter> documentationParameters = apiParameterParser.parseApiParametersAndArgumentModels(method);
documentationOperation.setParameters(documentationParameters);
addUnusedPathVariables(documentationOperation, methodRequestMapping.value());

Expand Down
Loading

0 comments on commit fa713bd

Please sign in to comment.