Skip to content

Commit

Permalink
Merge pull request kongchen#512 from danieleorler/fix-for-504
Browse files Browse the repository at this point in the history
ensure params inheritance is honoured in JAX-RS
  • Loading branch information
who authored Aug 3, 2017
2 parents 8d3193d + f1ef419 commit 8103bd5
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,26 @@
import io.swagger.converter.ModelConverters;
import io.swagger.jaxrs.ext.SwaggerExtension;
import io.swagger.jaxrs.ext.SwaggerExtensions;
import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.Response;
import io.swagger.models.SecurityRequirement;
import io.swagger.models.Swagger;
import io.swagger.models.*;
import io.swagger.models.Tag;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.util.ReflectionUtils;

import org.apache.maven.plugin.logging.Log;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.*;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

public class JaxrsReader extends AbstractReader implements ClassSwaggerReader {
private static final Logger LOGGER = LoggerFactory.getLogger(JaxrsReader.class);
Expand Down Expand Up @@ -402,28 +382,28 @@ private static String getClassName(Type type) {
private Annotation[][] findParamAnnotations(Method method) {
Annotation[][] paramAnnotation = method.getParameterAnnotations();

method = ReflectionUtils.getOverriddenMethod(method);
while(method != null) {
paramAnnotation = merge(paramAnnotation, method.getParameterAnnotations());
method = ReflectionUtils.getOverriddenMethod(method);
Method overriddenMethod = ReflectionUtils.getOverriddenMethod(method);
while(overriddenMethod != null) {
paramAnnotation = merge(overriddenMethod.getParameterAnnotations(), paramAnnotation);
overriddenMethod = ReflectionUtils.getOverriddenMethod(overriddenMethod);
}
return paramAnnotation;
}


private Annotation[][] merge(Annotation[][] paramAnnotation,
Annotation[][] superMethodParamAnnotations) {
Annotation[][] mergedAnnotations = new Annotation[paramAnnotation.length][];
private Annotation[][] merge(Annotation[][] overriddenMethodParamAnnotation,
Annotation[][] currentParamAnnotations) {
Annotation[][] mergedAnnotations = new Annotation[overriddenMethodParamAnnotation.length][];

for(int i=0; i<paramAnnotation.length; i++) {
mergedAnnotations[i] = merge(paramAnnotation[i], superMethodParamAnnotations[i]);
for(int i=0; i<overriddenMethodParamAnnotation.length; i++) {
mergedAnnotations[i] = merge(overriddenMethodParamAnnotation[i], currentParamAnnotations[i]);
}
return mergedAnnotations;
}

private Annotation[] merge(Annotation[] annotations,
Annotation[] annotations2) {
Set<Annotation> mergedAnnotations = new HashSet<Annotation>();
List<Annotation> mergedAnnotations = new ArrayList<Annotation>();
mergedAnnotations.addAll(Arrays.asList(annotations));
mergedAnnotations.addAll(Arrays.asList(annotations2));
return mergedAnnotations.toArray(new Annotation[0]);
Expand Down
11 changes: 10 additions & 1 deletion src/test/java/com/wordnik/jaxrs/MyResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.swagger.annotations.*;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -36,4 +36,13 @@ public abstract Response getPetsById(
notes = "This is a contrived example"
)
public abstract List<ListItem> getListOfItems();

//contrived example test case for swagger-maven-plugin issue #504
@GET
@ApiOperation(value = "Get a response", notes = "This is a contrived example")
Response testParamInheritance(
@PathParam("firstParamInterface") String firstParam,
@PathParam("secondParamInterface") String secondParam,
@QueryParam("thirdParamInterface") String thirdParam);

}
27 changes: 27 additions & 0 deletions src/test/java/com/wordnik/jaxrs/MyResourceAbstract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.wordnik.jaxrs;

import com.wordnik.sample.exception.NotFoundException;
import com.wordnik.sample.model.ListItem;
import io.swagger.annotations.ApiParam;

import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.List;

/**
* @author daniele.orler
*/
public abstract class MyResourceAbstract implements MyResource {
@Override
public abstract Response getPetsById(Long startId, Long endId) throws NotFoundException;

@Override
public abstract List<ListItem> getListOfItems();

@Override
public abstract Response testParamInheritance(
@PathParam("firstParamAbstract") String firstParam,
@ApiParam(required = true) @QueryParam("secondParamAbstract") String secondParam,
String thirdParam);
}
15 changes: 14 additions & 1 deletion src/test/java/com/wordnik/jaxrs/MyResourceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import com.wordnik.sample.model.Pet;

import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Path("/myResourceImpl")
public class MyResourceImpl implements MyResource {
public class MyResourceImpl extends MyResourceAbstract {
static PetData petData = new PetData();
static JavaRestResourceUtil ru = new JavaRestResourceUtil();

Expand Down Expand Up @@ -56,4 +57,16 @@ public List<ListItem> getListOfItems() {
return new ArrayList();
}

//contrived example test case for swagger-maven-plugin issue #504
/* (non-Javadoc)
* @see com.wordnik.jaxrs.MyResource#testParamInheritance(java.lang.String, java.lang.String, java.lang.String)
*/
@Path("{firstParamConcrete}/properties")
@Override
public Response testParamInheritance(
@PathParam("firstParamConcrete") String firstParam,
String secondParam,
String thirdParam) {
return Response.ok().build();
}
}
29 changes: 29 additions & 0 deletions src/test/resources/expectedOutput/swagger-with-converter.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@
}
}
},
"/myResourceImpl/{firstParamConcrete}/properties" : {
"get" : {
"summary" : "Get a response",
"description" : "This is a contrived example",
"operationId" : "testParamInheritance",
"produces" : [ "application/json", "application/xml" ],
"parameters" : [ {
"name" : "firstParamConcrete",
"in" : "path",
"required" : true,
"type" : "string"
}, {
"name" : "secondParamAbstract",
"in" : "query",
"required" : true,
"type" : "string"
}, {
"name" : "thirdParamInterface",
"in" : "query",
"required" : false,
"type" : "string"
} ],
"responses" : {
"default" : {
"description" : "successful operation"
}
}
}
},
"/pet": {
"get": {
"tags": [
Expand Down
24 changes: 24 additions & 0 deletions src/test/resources/expectedOutput/swagger-with-converter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ paths:
type: "array"
items:
$ref: "#/definitions/ListItem"
/myResourceImpl/{firstParamConcrete}/properties:
get:
summary: "Get a response"
description: "This is a contrived example"
operationId: "testParamInheritance"
produces:
- "application/json"
- "application/xml"
parameters:
- name: "firstParamConcrete"
in: "path"
required: true
type: "string"
- name: "secondParamAbstract"
in: "query"
required: true
type: "string"
- name: "thirdParamInterface"
in: "query"
required: false
type: "string"
responses:
default:
description: "successful operation"
/pet:
get:
tags:
Expand Down
29 changes: 29 additions & 0 deletions src/test/resources/expectedOutput/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@
}
}
},
"/myResourceImpl/{firstParamConcrete}/properties" : {
"get" : {
"summary" : "Get a response",
"description" : "This is a contrived example",
"operationId" : "testParamInheritance",
"produces" : [ "application/json", "application/xml" ],
"parameters" : [ {
"name" : "firstParamConcrete",
"in" : "path",
"required" : true,
"type" : "string"
}, {
"name" : "secondParamAbstract",
"in" : "query",
"required" : true,
"type" : "string"
}, {
"name" : "thirdParamInterface",
"in" : "query",
"required" : false,
"type" : "string"
} ],
"responses" : {
"default" : {
"description" : "successful operation"
}
}
}
},
"/pet": {
"get": {
"tags": [
Expand Down
24 changes: 24 additions & 0 deletions src/test/resources/expectedOutput/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ paths:
type: "array"
items:
$ref: "#/definitions/ListItem"
/myResourceImpl/{firstParamConcrete}/properties:
get:
summary: "Get a response"
description: "This is a contrived example"
operationId: "testParamInheritance"
produces:
- "application/json"
- "application/xml"
parameters:
- name: "firstParamConcrete"
in: "path"
required: true
type: "string"
- name: "secondParamAbstract"
in: "query"
required: true
type: "string"
- name: "thirdParamInterface"
in: "query"
required: false
type: "string"
responses:
default:
description: "successful operation"
/pet:
get:
tags:
Expand Down
Loading

0 comments on commit 8103bd5

Please sign in to comment.