Skip to content

Commit

Permalink
Merge pull request kongchen#661 from illes/master
Browse files Browse the repository at this point in the history
Fix @ApiOperation.response being ignored at subresource discovery
  • Loading branch information
who authored Sep 5, 2018
2 parents 3c403ec + bf4a91d commit 9a629e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public Class<? extends Annotation> annotationType() {
apiConsumes = updateOperationConsumes(parentConsumes, apiConsumes, operation);
apiProduces = updateOperationProduces(parentProduces, apiProduces, operation);

handleSubResource(apiConsumes, httpMethod, apiProduces, tags, method, operationPath, operation);
handleSubResource(apiConsumes, httpMethod, apiProduces, tags, method, apiOperation, operationPath, operation);

// can't continue without a valid http method
httpMethod = (httpMethod == null) ? parentMethod : httpMethod;
Expand Down Expand Up @@ -198,9 +198,13 @@ private Map<String, Tag> scanClasspathForTags() {
return tags;
}

private void handleSubResource(String[] apiConsumes, String httpMethod, String[] apiProduces, Map<String, Tag> tags, Method method, String operationPath, Operation operation) {
private void handleSubResource(String[] apiConsumes, String httpMethod, String[] apiProduces, Map<String, Tag> tags, Method method, ApiOperation apiOperation, String operationPath, Operation operation) {
if (isSubResource(httpMethod, method)) {
Class<?> responseClass = method.getReturnType();
if (apiOperation != null && !apiOperation.response().equals(Void.class) && !apiOperation.response().equals(void.class)) {
responseClass = apiOperation.response();
}
LOGGER.debug("handling sub-resource method " + method.toString() + " -> " + responseClass);
read(responseClass, operationPath, httpMethod, true, apiConsumes, apiProduces, tags, operation.getParameters());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.kongchen.swagger.docgen.reader;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
Expand All @@ -9,6 +10,7 @@
import io.swagger.annotations.ApiOperation;
import io.swagger.jaxrs.ext.SwaggerExtension;
import io.swagger.jaxrs.ext.SwaggerExtensions;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.Tag;
import io.swagger.models.parameters.Parameter;
Expand Down Expand Up @@ -102,6 +104,21 @@ private void assertSwaggerResponseContents(Tag expectedTag, Swagger result) {
assertFalse(path.getOperations().isEmpty(), "Should be a get operation");
}

@Test
public void discoverSubResource() {
Swagger result = reader.read(SomeResource.class);
assertSwaggerPath(result.getPath("/resource/explicit/name").getGet(), result, "/resource/implicit/name");
}

private void assertSwaggerPath(Operation expectedOperation, Swagger result, String expectedPath) {
assertNotNull(result, "No Swagger object created");
assertFalse(result.getPaths().isEmpty(), "Should contain operation paths");
assertTrue(result.getPaths().containsKey(expectedPath), "Expected path missing");
io.swagger.models.Path path = result.getPaths().get(expectedPath);
assertFalse(path.getOperations().isEmpty(), "Should be a get operation");
assertEquals(expectedOperation, path.getGet(), "Should contain operation");
}

@Api(tags = "atag")
@Path("/apath")
static class AnApi {
Expand All @@ -125,4 +142,30 @@ public Response getOperation() {
@Path("/apath")
static class NotAnnotatedApi {
}

@Path("/resource")
@Api(tags = "Resource")
static class SomeResource {
@Path("explicit")
public SomeSubResource getSomething() {
// no implementation needed. Method is only for the test cases, so that the return type is captured
return new SomeSubResource();
}
@Path("implicit")
@ApiOperation(value="", response = SomeSubResource.class)
public Object getSomeSub() {
// no implementation needed. Method is only for the test cases, so that the return type is overridden by @ApiOperation.response
return new SomeSubResource();
}
}

static class SomeSubResource {
@Path("name")
@GET
public String getName() {
// no implementation needed. Method is only for the test cases
return toString();
}
}

}

0 comments on commit 9a629e3

Please sign in to comment.