Skip to content

Commit

Permalink
Bugs fixing and minor improvements
Browse files Browse the repository at this point in the history
ApiOperationParser
1. A bug when we have @ApiOperation without "method" specified.
  Then value is taken from httpMethod (without trimming). As a result we get "POST ", which is not recognized by Swagger UI.
2. A bug when we have @ApiOperation without "produces" or "consumes" specifed.
  Then code populates this arrays with "" value and then in Swagger UI we see "Produces: ['']"
3. ApiError renamed to ApiResponse in code
4. Validation logic partially moved to DocumentationOperation (as well as toScalaOperation())

ApiModelParser, ApiParserImpl
5. A bug with missing models.
  Some of the models were ignored by parsing and Swagger UI had no ability to show them to user.
  Models parsing logic was moved to ApiUtils class so that both of classes mentioned could use it.
  In future, probably, it is better to merge this "model parsing logic" and remove baseModelPackage property,
  because it seems that this property is not necessary.

6. Work with Generics was improved. Code now correctly parses generic return types:
  List<SomeClass> -> List[SomeClass]
  ContainerClass<InnerClass> -> ContainerClass[InnerClass]

  My Fork of Swagger UI renders such response types correctly
  swagger-api/swagger-ui#401

  Elements with only one generic would be processed.
  • Loading branch information
Andrey Antonov committed Feb 19, 2014
1 parent 252e3fa commit 27dd93d
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 243 deletions.
Original file line number Diff line number Diff line change
@@ -1,55 +1,24 @@
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.ApiUtils;
import com.wordnik.swagger.model.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import scala.Option;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
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());
}
}

return documentationSchemaMap;
}

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

Map<String, Model> documentationSchemaMap = new HashMap<String, Model>();
Map<String, Model> models = new HashMap<String, Model>();
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());
}
ApiUtils.addModels(type, models);
}

return documentationSchemaMap;
return models;
}
}
Loading

0 comments on commit 27dd93d

Please sign in to comment.