Skip to content

Commit

Permalink
Fix springfox#2855, fix incorrect api parameter type
Browse files Browse the repository at this point in the history
  • Loading branch information
dulong committed Jan 10, 2019
1 parent 379f0ce commit 1c8c13c
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Mono<User> createUser(
@ResponseBody
@ApiOperation(value = "Creates list of users with given input array")
public Mono<ResponseEntity<User>> createUsersWithArrayInput(@ApiParam(value = "List of user object", required = true)
User[] users) {
@RequestBody User[] users) {
for (User user : users) {
userRepository.add(user);
}
Expand All @@ -76,7 +76,7 @@ public Mono<ResponseEntity<User>> createUsersWithArrayInput(@ApiParam(value = "L
@ResponseBody
@ApiOperation(value = "Creates list of users with given input array")
public Mono<ResponseEntity<String>> createUsersWithListInput(
@ApiParam(value = "List of user object", required = true) List<User> users) {
@ApiParam(value = "List of user object", required = true) @RequestBody List<User> users) {
for (User user : users) {
userRepository.add(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ResponseEntity<User> createUser(
@ResponseBody
@ApiOperation(value = "Creates list of users with given input array")
public ResponseEntity<User> createUsersWithArrayInput(@ApiParam(value = "List of user object", required = true)
User[] users) {
@RequestBody User[] users) {
for (User user : users) {
userRepository.add(user);
}
Expand All @@ -73,7 +73,7 @@ public ResponseEntity<User> createUsersWithArrayInput(@ApiParam(value = "List of
@ResponseBody
@ApiOperation(value = "Creates list of users with given input array")
public ResponseEntity<String> createUsersWithListInput(
@ApiParam(value = "List of user object", required = true) List<User> users) {
@ApiParam(value = "List of user object", required = true) @RequestBody List<User> users) {
for (User user : users) {
userRepository.add(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ public static String findParameterType(ParameterContext parameterContext) {
} else if (resolvedMethodParameter.hasParameterAnnotation(ModelAttribute.class)) {
LOGGER.warn("@ModelAttribute annotated parameters should have already been expanded via "
+ "the ExpandedParameterBuilderPlugin");
return "body";
}
if (!resolvedMethodParameter.hasParameterAnnotations()) {
return determineScalarParameterType(
parameterContext.getOperationContext().consumes(),
parameterContext.getOperationContext().httpMethod());
}
return "body";
return determineScalarParameterType(
parameterContext.getOperationContext().consumes(),
parameterContext.getOperationContext().httpMethod());
}

private static boolean isListOfFiles(ResolvedType parameterType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@ public void bug2822(@ApiParam(example = "exampleMessage") @RequestBody String me
// Empty body is sufficient for testing
}

@ApiOperation(value = "bug2855")
@RequestMapping(value = "/2855", method = POST, consumes = APPLICATION_FORM_URLENCODED_VALUE)
public void bug2855(
@NotNull String a,
String b) {
}

public class Bug2423 {
public String from;
public String to;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public void apply(ParameterContext context) {
Optional<ApiParam> apiParam = context.resolvedMethodParameter().findAnnotation(ApiParam.class);
String paramType = findParameterType(context);
String name = null;
Optional<String> defaultName = context.resolvedMethodParameter().defaultName();
if(defaultName.isPresent()) {
name = defaultName.get();
}
if (apiParam.isPresent()) {
name = ofNullable(apiParam.get().name()).filter(((Predicate<String>)String::isEmpty).negate()).orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package springfox.documentation.swagger1.readers.parameter

import com.fasterxml.classmate.TypeResolver
import io.swagger.annotations.ApiParam
import org.springframework.web.bind.annotation.RequestBody
import springfox.documentation.builders.ParameterBuilder
import springfox.documentation.schema.DefaultGenericTypeNamingStrategy
import springfox.documentation.service.ResolvedMethodParameter
Expand All @@ -47,7 +48,7 @@ class ParameterNameReaderSpec extends DocumentationContextSpec {
given:
def operationContext = Mock(OperationContext)
def resolvedMethodParameter =
new ResolvedMethodParameter(0, "", [apiParam], new TypeResolver().resolve(Object.class))
new ResolvedMethodParameter(0, "methodParameterName", [apiParam, requestBody], new TypeResolver().resolve(Object.class))
def genericNamingStrategy = new DefaultGenericTypeNamingStrategy()
and: "mocks are setup"
operationContext.consumes() >> []
Expand All @@ -60,9 +61,11 @@ class ParameterNameReaderSpec extends DocumentationContextSpec {
then:
parameterContext.parameterBuilder().build().name == expectedName
where:
apiParam | paramType | expectedName
[name: { -> "bodyParam" }, value: { -> "body Param"}] as ApiParam | "body" | "body"
null | "body" | "body"
apiParam | requestBody | paramType | expectedName
[name: { -> "bodyParam" }, value: { -> "body Param"}] as ApiParam | null | "query" | "bodyParam"
[name: { -> "bodyParam" }, value: { -> "body Param"}] as ApiParam | [] as RequestBody | "body" | "body"
null | [] as RequestBody | "body" | "body"
null | null | "query" | "methodParameterName"
}

def nameReader(annotation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,40 @@
"deprecated":false
}
},
"/bugs/2855":{
"post":{
"tags":[
"Bugs"
],
"summary":"bug2855",
"operationId":"bug2855UsingPOST_2",
"consumes":[
"application/x-www-form-urlencoded"
],
"parameters":[
{
"in":"formData",
"name":"a",
"description":"a",
"required":true,
"type":"string"
},
{
"in":"formData",
"name":"b",
"description":"b",
"required":false,
"type":"string"
}
],
"responses":{
"200":{
"description":"OK"
}
},
"deprecated":false
}
},
"/bugs/bug1827{?authorIds,authors[0].books[0].id,authors[0].books[0].name,authors[0].id,authors[0].name,id,name}": {
"get": {
"tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,40 @@
"deprecated":false
}
},
"/bugs/2855":{
"post":{
"tags":[
"Bugs"
],
"summary":"bug2855",
"operationId":"bug2855UsingPOST_1",
"consumes":[
"application/x-www-form-urlencoded"
],
"parameters":[
{
"in":"formData",
"name":"a",
"description":"a",
"required":true,
"type":"string"
},
{
"in":"formData",
"name":"b",
"description":"b",
"required":false,
"type":"string"
}
],
"responses":{
"200":{
"description":"OK"
}
},
"deprecated":false
}
},
"/bugs/bug1827{?authorIds,authors[0].books[0].id,authors[0].books[0].name,authors[0].id,authors[0].name,id,name}": {
"get": {
"tags": [
Expand Down

0 comments on commit 1c8c13c

Please sign in to comment.