Skip to content

Commit

Permalink
Merge pull request kongchen#511 from packleader/bugfix/spring-params
Browse files Browse the repository at this point in the history
Bugfix/spring params
  • Loading branch information
who authored Aug 3, 2017
2 parents ad1d041 + a001cc6 commit 8d3193d
Show file tree
Hide file tree
Showing 7 changed files with 1,915 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@
import org.apache.commons.lang3.reflect.TypeUtils;
import org.apache.maven.plugin.logging.Log;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.BeanParam;
import javax.ws.rs.FormParam;
Expand Down Expand Up @@ -367,6 +362,7 @@ private boolean hasValidAnnotations(List<Annotation> parameterAnnotations) {
validParameterAnnotations.add(PathVariable.class);
validParameterAnnotations.add(RequestHeader.class);
validParameterAnnotations.add(RequestPart.class);
validParameterAnnotations.add(CookieValue.class);


boolean hasValidAnnotation = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.swagger.models.properties.RefProperty;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.plexus.util.StringUtils;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -286,6 +287,8 @@ private Operation parseMethod(Method method) {
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++) {
Expand All @@ -294,6 +297,9 @@ private Operation parseMethod(Method method) {
List<Parameter> parameters = getParameters(type, annotations);

for (Parameter parameter : parameters) {
if(parameter.getName().isEmpty()) {
parameter.setName(parameterNames[i]);
}
operation.parameter(parameter);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.FileProperty;
import io.swagger.models.properties.Property;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.TypeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.AnnotationUtils;
Expand Down Expand Up @@ -68,7 +69,8 @@ private Parameter extractParameterFromAnnotation(Annotation annotation, String d

if (annotation instanceof RequestParam) {
RequestParam requestParam = (RequestParam) annotation;
QueryParameter queryParameter = new QueryParameter().name(requestParam.value())
String paramName = StringUtils.defaultIfEmpty(requestParam.value(), requestParam.name());
QueryParameter queryParameter = new QueryParameter().name(paramName)
.required(requestParam.required());

if (!defaultValue.isEmpty()) {
Expand All @@ -82,7 +84,8 @@ private Parameter extractParameterFromAnnotation(Annotation annotation, String d
parameter = queryParameter;
} else if (annotation instanceof PathVariable) {
PathVariable pathVariable = (PathVariable) annotation;
PathParameter pathParameter = new PathParameter().name(pathVariable.value());
String paramName = StringUtils.defaultIfEmpty(pathVariable.value(), pathVariable.name());
PathParameter pathParameter = new PathParameter().name(paramName);
if (!defaultValue.isEmpty()) {
pathParameter.setDefaultValue(defaultValue);
}
Expand All @@ -93,7 +96,8 @@ private Parameter extractParameterFromAnnotation(Annotation annotation, String d
parameter = pathParameter;
} else if (annotation instanceof RequestHeader) {
RequestHeader requestHeader = (RequestHeader) annotation;
HeaderParameter headerParameter = new HeaderParameter().name(requestHeader.value())
String paramName = StringUtils.defaultIfEmpty(requestHeader.value(), requestHeader.name());
HeaderParameter headerParameter = new HeaderParameter().name(paramName)
.required(requestHeader.required());
headerParameter.setDefaultValue(defaultValue);
Property schema = ModelConverters.getInstance().readAsProperty(type);
Expand All @@ -104,7 +108,8 @@ private Parameter extractParameterFromAnnotation(Annotation annotation, String d
parameter = headerParameter;
} else if (annotation instanceof CookieValue) {
CookieValue cookieValue = (CookieValue) annotation;
CookieParameter cookieParameter = new CookieParameter().name(cookieValue.value())
String paramName = StringUtils.defaultIfEmpty(cookieValue.value(), cookieValue.name());
CookieParameter cookieParameter = new CookieParameter().name(paramName)
.required(cookieValue.required());
if (!defaultValue.isEmpty()) {
cookieParameter.setDefaultValue(defaultValue);
Expand All @@ -117,7 +122,8 @@ private Parameter extractParameterFromAnnotation(Annotation annotation, String d
parameter = cookieParameter;
} else if (annotation instanceof RequestPart) {
RequestPart requestPart = (RequestPart) annotation;
FormParameter formParameter = new FormParameter().name(requestPart.value())
String paramName = StringUtils.defaultIfEmpty(requestPart.value(), requestPart.name());
FormParameter formParameter = new FormParameter().name(paramName)
.required(requestPart.required());

if (!defaultValue.isEmpty()) {
Expand Down
105 changes: 105 additions & 0 deletions src/test/java/com/wordnik/springmvc/EchoResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.wordnik.springmvc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(value = "/echo", description = "Set of simple endpoints that return whatever value you pass in")
@RequestMapping(value = "/echo", produces = {"application/json", "application/xml"})
public class EchoResource {

// Tests for @PathVariable
@RequestMapping(value = "/pathVariableExpectParameterName/{parameterName}", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String pathVariableExpectParameterName(@PathVariable String parameterName) {
return parameterName;
}

@RequestMapping(value = "/pathVariableExpectVariableName/{parameterName}", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String pathVariableExpectVariableName(@PathVariable(name = "pathVariableName") String parameterName) {
return parameterName;
}

@RequestMapping(value = "/pathVariableExpectVariableValue/{parameterName}", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String pathVariableExpectVariableValue(@PathVariable(value = "pathVariableValue") String parameterName) {
return parameterName;
}

// Tests for @RequestParam
@RequestMapping(value = "/requestParamExpectParameterName", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestParamExpectParameterName(@RequestParam String parameterName) {
return parameterName;
}

@RequestMapping(value = "/requestParamExpectParamName", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestParamExpectParamName(@RequestParam(name = "requestParamName") String parameterName) {
return parameterName;
}

@RequestMapping(value = "/requestParamExpectParamValue", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestParamExpectParamValue(@RequestParam(value = "requestParamValue") String parameterName) {
return parameterName;
}

// Tests for @RequestHeader
@RequestMapping(value = "/requestHeaderExpectParameterName", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestHeaderExpectParameterName(@RequestHeader String parameterName) {
return parameterName;
}

@RequestMapping(value = "/requestHeaderExpectHeaderName", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestHeaderExpectHeaderName(@RequestHeader(name = "requestHeaderName") String parameterName) {
return parameterName;
}

@RequestMapping(value = "/requestHeaderExpectHeaderValue", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestHeaderExpectHeaderValue(@RequestHeader(value = "requestHeaderValue") String parameterName) {
return parameterName;
}

// Tests for @CookieValue
@RequestMapping(value = "/cookieValueExpectParameterName", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String cookieValueExpectParameterName(@CookieValue String parameterName) {
return parameterName;
}

@RequestMapping(value = "/cookieValueExpectCookieName", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String cookieValueExpectCookieName(@CookieValue(name = "cookieValueName") String parameterName) {
return parameterName;
}

@RequestMapping(value = "/cookieValueExpectCookieValue", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String cookieValueExpectCookieValue(@CookieValue(value = "cookieValueValue") String parameterName) {
return parameterName;
}

// Tests for @RequestPart
@RequestMapping(value = "/requestPartExpectParameterName", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestPartExpectParameterName(@RequestPart String parameterName) {
return parameterName;
}

@RequestMapping(value = "/requestPartExpectPartName", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestPartExpectPartName(@RequestPart(name = "requestPartName") String parameterName) {
return parameterName;
}

@RequestMapping(value = "/requestPartExpectPartValue", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "")
public String requestPartExpectPartValue(@RequestPart(value = "requestPartValue") String parameterName) {
return parameterName;
}
}
Loading

0 comments on commit 8d3193d

Please sign in to comment.