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

Resource inheritance support #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static com.google.common.collect.Collections2.transform;
import static com.hypnoticocelot.jaxrs.doclet.parser.AnnotationHelper.parsePath;


public class ApiClassParser {

private final DocletOptions options;
Expand All @@ -36,6 +37,7 @@ public ApiClassParser(DocletOptions options, ClassDoc classDoc, Collection<Class

/**
* Creates sub-resource class parser.
*
* @param parentMethod method that creates the sub-resource.
*/
public ApiClassParser(DocletOptions options, ClassDoc classDoc, Collection<ClassDoc> classes, Method parentMethod) {
Expand All @@ -55,36 +57,40 @@ public Collection<Api> parse() {
List<Api> apis = new ArrayList<Api>();
Map<String, Collection<Method>> apiMethods = new HashMap<String, Collection<Method>>();

for (MethodDoc method : classDoc.methods()) {
ApiMethodParser methodParser = parentMethod == null ?
new ApiMethodParser(options, rootPath, method) :
new ApiMethodParser(options, parentMethod, method);
Method parsedMethod = methodParser.parse();
if (parsedMethod == null) {
continue;
}
if (parsedMethod.isSubResource()) {
ClassDoc subResourceClassDoc = lookUpClassDoc(method.returnType());
if (subResourceClassDoc != null) {
// delete class from the dictionary to handle recursive sub-resources
Collection<ClassDoc> shrunkClasses = new ArrayList<ClassDoc>(classes);
shrunkClasses.remove(classDoc);
// recursively parse the sub-resource class
ApiClassParser subResourceParser = new ApiClassParser(options, subResourceClassDoc, shrunkClasses, parsedMethod);
apis.addAll(subResourceParser.parse());
models.addAll(subResourceParser.models());
ClassDoc currentClassDoc = classDoc;
while (currentClassDoc != null) {
for (MethodDoc method : currentClassDoc.methods()) {
ApiMethodParser methodParser = parentMethod == null ?
new ApiMethodParser(options, rootPath, method) :
new ApiMethodParser(options, parentMethod, method);
Method parsedMethod = methodParser.parse();
if (parsedMethod == null) {
continue;
}
continue;
}
models.addAll(methodParser.models());
if (parsedMethod.isSubResource()) {
ClassDoc subResourceClassDoc = lookUpClassDoc(method.returnType());
if (subResourceClassDoc != null) {
// delete class from the dictionary to handle recursive sub-resources
Collection<ClassDoc> shrunkClasses = new ArrayList<ClassDoc>(classes);
shrunkClasses.remove(currentClassDoc);
// recursively parse the sub-resource class
ApiClassParser subResourceParser = new ApiClassParser(options, subResourceClassDoc, shrunkClasses, parsedMethod);
apis.addAll(subResourceParser.parse());
models.addAll(subResourceParser.models());
}
continue;
}
models.addAll(methodParser.models());

String realPath = parsedMethod.getPath();
Collection<Method> matchingMethods = apiMethods.get(realPath);
if (matchingMethods == null) {
matchingMethods = new ArrayList<Method>();
apiMethods.put(realPath, matchingMethods);
String realPath = parsedMethod.getPath();
Collection<Method> matchingMethods = apiMethods.get(realPath);
if (matchingMethods == null) {
matchingMethods = new ArrayList<Method>();
apiMethods.put(realPath, matchingMethods);
}
matchingMethods.add(parsedMethod);
}
matchingMethods.add(parsedMethod);
currentClassDoc = currentClassDoc.superclass();
}

for (Map.Entry<String, Collection<Method>> apiEntries : apiMethods.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.hypnoticocelot.jaxrs.doclet.apidocs;

import com.hypnoticocelot.jaxrs.doclet.DocletOptions;
import com.hypnoticocelot.jaxrs.doclet.Recorder;
import com.hypnoticocelot.jaxrs.doclet.model.ApiDeclaration;
import com.hypnoticocelot.jaxrs.doclet.parser.JaxRsAnnotationParser;
import com.sun.javadoc.RootDoc;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static com.hypnoticocelot.jaxrs.doclet.apidocs.FixtureLoader.loadFixture;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;


public class InheritanceTest {
private Recorder recorderMock;
private DocletOptions options;

@Before
public void setup() {
recorderMock = mock(Recorder.class);
options = new DocletOptions().setRecorder(recorderMock);
}

@Test
public void testStart() throws IOException {
final RootDoc rootDoc = RootDocLoader.fromPath("src/test/resources", "fixtures.inheritance");
new JaxRsAnnotationParser(options, rootDoc).run();

final ApiDeclaration api = loadFixture("/fixtures/inheritance/concrete.json", ApiDeclaration.class);
verify(recorderMock).record(any(File.class), eq(api));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fixtures.inheritance;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;


public abstract class AbstractResource {

@GET
@Path("{id}")
public String getById(@PathParam("id") String id) {
return getResourceById(id);
}

protected abstract String getResourceById(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fixtures.inheritance;

import javax.ws.rs.GET;
import javax.ws.rs.Path;


@Path("/foo")
public class ConcreteResource extends AbstractResource {

@GET
@Path("bar")
public String bar() {
return "bar";
}

@Override
protected String getResourceById(String id) {
return "Concrete Resource with id " + id;
}
}
37 changes: 37 additions & 0 deletions jaxrs-doclet/src/test/resources/fixtures/inheritance/concrete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"apiVersion": "0",
"swaggerVersion": "1.1",
"basePath": "http://localhost:8080",
"resourcePath": "/foo",
"apis": [
{
"path": "/foo/bar",
"description": "",
"operations": [
{
"httpMethod": "GET",
"nickname": "bar",
"responseClass": "string"
}
]
},
{
"path": "/foo/{id}",
"description": "",
"operations": [
{
"httpMethod": "GET",
"nickname": "getById",
"responseClass": "string",
"parameters": [
{
"paramType": "path",
"name": "id",
"dataType": "string"
}
]
}
]
}
]
}