Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
Generate description for parameters now correctly when used in ModelAttribute
  • Loading branch information
Postremus committed Sep 9, 2018
1 parent 4a80190 commit 4ea4bca
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.github.kongchen.swagger.docgen.spring;

import com.fasterxml.jackson.databind.JavaType;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import io.swagger.converter.ModelConverters;
import io.swagger.jaxrs.ext.AbstractSwaggerExtension;
import io.swagger.jaxrs.ext.SwaggerExtension;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.*;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.FileProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.StringProperty;
import io.swagger.util.ParameterProcessor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.apache.commons.lang3.reflect.TypeUtils;
Expand Down Expand Up @@ -74,6 +77,9 @@ public List<Parameter> extractParameters(List<Annotation> annotations, Type type
private Map<Class<?>, Annotation> toMap(Collection<? extends Annotation> annotations) {
Map<Class<?>, Annotation> annotationMap = new HashMap<>();
for (Annotation annotation : annotations) {
if (annotation == null) {
continue;
}
annotationMap.put(annotation.annotationType(), annotation);
}

Expand Down Expand Up @@ -221,32 +227,22 @@ private List<Parameter> extractParametersFromModelAttributeAnnotation(Type type,
// Here we have a bean setter method that is annotted with @ApiParam, but we still
// need to know what type of parameter to create. In order to do this, we look for
// any annotation attached to the first method parameter of the setter fucntion.
Annotation[][] methodAnnotations = propertyDescriptorSetter.getParameterAnnotations();
if (methodAnnotations == null || methodAnnotations.length == 0) {
Annotation[][] parameterAnnotations = propertyDescriptorSetter.getParameterAnnotations();
if (parameterAnnotations == null || parameterAnnotations.length == 0) {
continue;
}

Class parameterClass = propertyDescriptor.getPropertyType();
List<Parameter> propertySetterExtractedParameters = this.extractParameterFromAnnotation(
parameterClass, toMap(Arrays.asList(methodAnnotations[0])));

if (propertySetterExtractedParameters.isEmpty()) {
QueryParameter queryParameter = new QueryParameter().name(propertyDescriptor.getDisplayName())
.description(propertySetterApiParam.value())
.required(propertySetterApiParam.required());
queryParameter.setAccess(propertySetterApiParam.access());
Property schema = ModelConverters.getInstance()
.readAsProperty(propertyDescriptor.getPropertyType());
if (schema != null) {
queryParameter.setProperty(schema);
}
if (!propertySetterApiParam.name().isEmpty()) {
queryParameter.setName(propertySetterApiParam.name());
parameterClass, toMap(Arrays.asList(parameterAnnotations[0])));

for (Parameter parameter : propertySetterExtractedParameters) {
if (Strings.isNullOrEmpty(parameter.getName())) {
parameter.setName(propertyDescriptor.getDisplayName());
}
parameters.add(queryParameter);
} else {
parameters.add(propertySetterExtractedParameters.get(0));
ParameterProcessor.applyAnnotations(new Swagger(), parameter, type, Lists.newArrayList(propertySetterApiParam));
}
parameters.addAll(propertySetterExtractedParameters);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testExtractParametersReturnsEmptyList() {
// This allows to test if first the chain is called, and only then empty, modifiable lists are returned as last resort
List<Parameter> parameters = swaggerExtension.extractParameters(
annotations,
String.class,
Void.class,
typesToSkip,
iterator);
// Has to return a collection we can later modify.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,14 +646,14 @@
"name" : "limit",
"in" : "query",
"description" : "The pagination limit",
"required" : false,
"required" : true,
"type" : "integer",
"format" : "int32"
}, {
"name" : "offset",
"in" : "query",
"description" : "The pagination offset",
"required" : false,
"required" : true,
"type" : "integer",
"format" : "int32"
} ],
Expand Down Expand Up @@ -956,18 +956,19 @@
"name" : "name",
"in" : "query",
"description" : "Updated name of the pet",
"required" : false,
"required" : true,
"type" : "string"
}, {
"name" : "petId",
"in" : "path",
"description" : "ID of pet that needs to be updated",
"required" : true,
"type" : "string"
}, {
"name" : "status",
"in" : "query",
"description" : "Updated status of the pet",
"required" : false,
"required" : true,
"type" : "string"
} ],
"responses" : {
Expand Down Expand Up @@ -1633,4 +1634,4 @@
}
}
}
}
}
9 changes: 5 additions & 4 deletions src/test/resources/expectedOutput/swagger-spring.json
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,14 @@
"name" : "limit",
"in" : "query",
"description" : "The pagination limit",
"required" : false,
"required" : true,
"type" : "integer",
"format" : "int32"
}, {
"name" : "offset",
"in" : "query",
"description" : "The pagination offset",
"required" : false,
"required" : true,
"type" : "integer",
"format" : "int32"
} ],
Expand Down Expand Up @@ -971,18 +971,19 @@
"name" : "name",
"in" : "query",
"description" : "Updated name of the pet",
"required" : false,
"required" : true,
"type" : "string"
}, {
"name" : "petId",
"in" : "path",
"description" : "ID of pet that needs to be updated",
"required" : true,
"type" : "string"
}, {
"name" : "status",
"in" : "query",
"description" : "Updated status of the pet",
"required" : false,
"required" : true,
"type" : "string"
} ],
"responses" : {
Expand Down
9 changes: 5 additions & 4 deletions src/test/resources/expectedOutput/swagger-spring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,13 @@ paths:
- name: "limit"
in: "query"
description: "The pagination limit"
required: false
required: true
type: "integer"
format: "int32"
- name: "offset"
in: "query"
description: "The pagination offset"
required: false
required: true
type: "integer"
format: "int32"
responses:
Expand Down Expand Up @@ -858,16 +858,17 @@ paths:
- name: "name"
in: "query"
description: "Updated name of the pet"
required: false
required: true
type: "string"
- name: "petId"
in: "path"
description: "ID of pet that needs to be updated"
required: true
type: "string"
- name: "status"
in: "query"
description: "Updated status of the pet"
required: false
required: true
type: "string"
responses:
200:
Expand Down
10 changes: 5 additions & 5 deletions src/test/resources/sample-springmvc.html
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,7 @@
<tr>
<th>limit</th>
<td>query</td>
<td>no</td>
<td>yes</td>
<td>The pagination limit</td>
<td> - </td>

Expand All @@ -1967,7 +1967,7 @@
<tr>
<th>offset</th>
<td>query</td>
<td>no</td>
<td>yes</td>
<td>The pagination offset</td>
<td> - </td>

Expand Down Expand Up @@ -2743,7 +2743,7 @@
<tr>
<th>name</th>
<td>query</td>
<td>no</td>
<td>yes</td>
<td>Updated name of the pet</td>
<td> - </td>

Expand All @@ -2757,7 +2757,7 @@
<th>petId</th>
<td>path</td>
<td>yes</td>
<td></td>
<td>ID of pet that needs to be updated</td>
<td> - </td>


Expand All @@ -2769,7 +2769,7 @@
<tr>
<th>status</th>
<td>query</td>
<td>no</td>
<td>yes</td>
<td>Updated status of the pet</td>
<td> - </td>

Expand Down

0 comments on commit 4ea4bca

Please sign in to comment.