Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Fix StackOverflow for methods returning a response object & for recursive models #20

Merged
merged 3 commits into from
Apr 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ To use the Swagger Doclet in your Maven project, add the following to your POM f
<doclet>ServiceDoclet</doclet>
<docletArtifact>
<groupId>com.unclehulka</groupId>
<artifactId>swagger-jaxrs-doclet</artifactId>
<version>0.0.1.6-SNAPSHOT</version>
<artifactId>jaxrs-doclet</artifactId>
<version>0.0.2-SNAPSHOT</version>
</docletArtifact>
<reportOutputDirectory>${project.build.outputDirectory}</reportOutputDirectory>
<useStandardDocletOptions>false</useStandardDocletOptions>
<additionalparam>-apiVersion 1 -docBasePath ${server.url} -apiBasePath ${server.url}</additionalparam>
<additionalparam>-apiVersion 1 -docBasePath ${server.url}/apidocs -apiBasePath ${server.url}</additionalparam>
</configuration>
<goals>
<goal>javadoc</goal>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yammer.dropwizard.apidocs.sample.api;

public class Recursive {

private Recursive recursive;

public Recursive getRecursive() {
return recursive;
}

public void setRecursive(Recursive recursive) {
this.recursive = recursive;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.yammer.dropwizard.apidocs.sample.resources;

import com.yammer.dropwizard.apidocs.sample.api.Recursive;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Path("/Recursive")
public class RecursiveResource {
@POST
public Recursive recurse(Recursive recursive) {
return recursive;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ public static boolean startInternal(RootDoc doc, Recorder recorder) {
//build model for body parameter
for (Parameter parameter : method.parameters()) {
if (shouldIncludeParameter(parameter)) {
if (paramTypeOf(parameter).equalsIgnoreCase("body") && !PRIMITIVES.contains(typeOf(parameter.type())))
classModelMap = parseModels(parameter.type(), classModelMap);
classModelMap = parseModels(parameter.type(), classModelMap);
}
}

Expand All @@ -139,11 +138,8 @@ public static boolean startInternal(RootDoc doc, Recorder recorder) {
if (me.getReturnType() == null || !me.getReturnType().equals(name)) {
me.setReturnType(name);
}
if (!PRIMITIVES.contains(name)) {
classModelMap = parseModels(type, classModelMap);
}
classModelMap = parseModels(type, classModelMap);
}

}
}
apiMap.put(apiPath, methodMap);
Expand Down Expand Up @@ -261,63 +257,75 @@ private static Method parseMethod(MethodDoc method) {
*/
private static Map<String, Model> parseModels(Type type, Map<String, Model> modelMap) {
String typeName = typeOf(type);
ClassDoc cd = type.asClassDoc();
if (cd != null) {
Model model = modelMap.get(typeName);
if (model == null) {
Map<String, Type> eleMap = new HashMap<>();

//Get fields
FieldDoc[] fdArr = cd.fields();
if (fdArr != null && fdArr.length > 0) {
for (FieldDoc fd : fdArr) {
if (eleMap.get(fd.name()) == null) {
eleMap.put(fd.name(), fd.type());
if(!PRIMITIVES.contains(typeName) && !type.qualifiedTypeName().startsWith("javax.")) {
ClassDoc cd = type.asClassDoc();
if (cd != null) {
Model model = modelMap.get(typeName);
if (model == null) {
Map<String, Type> eleMap = new HashMap<>();

//Get fields
FieldDoc[] fdArr = cd.fields();
if (fdArr != null && fdArr.length > 0) {
for (FieldDoc fd : fdArr) {
if (eleMap.get(fd.name()) == null) {
eleMap.put(fd.name(), fd.type());
}
}
}
}

//Get methods
MethodDoc[] mdArr = cd.methods();
if (mdArr != null && mdArr.length > 0) {
for (MethodDoc md : mdArr) {
if (md.name().startsWith("get") && md.name().length() > 3) {
String name = md.name().substring(3);
name = name.substring(0, 1).toLowerCase() + (name.length() > 1 ? name.substring(1) : "");
if (eleMap.get(name) == null) {
eleMap.put(name, md.returnType());
//Get methods
MethodDoc[] mdArr = cd.methods();
if (mdArr != null && mdArr.length > 0) {
for (MethodDoc md : mdArr) {
if (md.name().startsWith("get") && md.name().length() > 3) {
String name = md.name().substring(3);
name = name.substring(0, 1).toLowerCase() + (name.length() > 1 ? name.substring(1) : "");
if (eleMap.get(name) == null) {
eleMap.put(name, md.returnType());
}
}
}
}
}

//Process all fields & methods
if (eleMap.keySet().size() > 0) {
Map<String, Property> fieldMap = new HashMap<>();
for (Map.Entry<String, Type> entry : eleMap.entrySet()) {
//Check if it is a collection and get collection type
String containerOf = null;
ParameterizedType pt = entry.getValue().asParameterizedType();
if (pt != null) {
Type[] typeArgs = pt.typeArguments();
if (typeArgs != null && typeArgs.length > 0) {
containerOf = typeOf(typeArgs[0]);
if (!PRIMITIVES.contains(containerOf)) {
parseModels(typeArgs[0], modelMap);
//Process all fields & methods
if (eleMap.keySet().size() > 0) {

//Add to the model
Map<String, Property> fieldMap = new HashMap<>();
for (Map.Entry<String, Type> entry : eleMap.entrySet()) {
//Check if it is a collection and get collection type
String containerOf = null;
ParameterizedType pt = entry.getValue().asParameterizedType();
if (pt != null) {
Type[] typeArgs = pt.typeArguments();
if (typeArgs != null && typeArgs.length > 0) {
containerOf = typeOf(typeArgs[0]);
}
}

//Add to map
String eleTypeName = typeOf(entry.getValue());
fieldMap.put(entry.getKey(), new Property(eleTypeName, null, containerOf));

}

//Add to map
String eleTypeName = typeOf(entry.getValue());
fieldMap.put(entry.getKey(), new Property(eleTypeName, null, containerOf));
modelMap.put(typeName, new Model(typeName, fieldMap));

//Build contained models
for (Map.Entry<String, Type> entry : eleMap.entrySet()) {
//Check if it is a collection and get collection type
ParameterizedType pt = entry.getValue().asParameterizedType();
if (pt != null) {
Type[] typeArgs = pt.typeArguments();
if (typeArgs != null && typeArgs.length > 0) {
parseModels(typeArgs[0], modelMap);
}
}

//If not primitive, build the model for it too
if (!PRIMITIVES.contains(eleTypeName)) {
parseModels(entry.getValue(), modelMap);
}
}
modelMap.put(typeName, new Model(typeName, fieldMap));
}
}
}
Expand All @@ -331,8 +339,13 @@ private static boolean shouldIncludeParameter(Parameter parameter) {
AnnotationDesc[] annotations = parameter.annotations();

//Include if it has no annotations
if (annotations == null || annotations.length == 0)
return true;
if (annotations == null || annotations.length == 0){
if (paramTypeOf(parameter).equalsIgnoreCase("body")){
return true;
} else {
return false;
}
}

//Include if it has a jax-rs annotation
for (AnnotationDesc annotation : annotations) {
Expand Down