Skip to content

Commit 6721e95

Browse files
committed
added test, pattern support for #1054
1 parent 004979a commit 6721e95

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

modules/swagger-jaxrs/src/main/java/com/wordnik/swagger/jaxrs/Reader.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,32 @@ protected Swagger read(Class<?> cls, String parentPath, boolean readHidden, Stri
124124

125125
String operationPath = getPath(apiPath, methodPath, parentPath);
126126
if(operationPath != null && apiOperation != null) {
127+
String [] pps = operationPath.split("/");
128+
String [] pathParts = new String[pps.length];
129+
Map<String, String> regexMap = new HashMap<String, String>();
130+
131+
for(int i = 0; i < pps.length; i++) {
132+
String p = pps[i];
133+
if(p.startsWith("{")) {
134+
int pos = p.indexOf(":");
135+
if(pos > 0) {
136+
String left = p.substring(1, pos);
137+
String right = p.substring(pos + 1, p.length()-1);
138+
pathParts[i] = "{" + left + "}";
139+
regexMap.put(left, right);
140+
}
141+
else
142+
pathParts[i] = p;
143+
}
144+
else pathParts[i] = p;
145+
}
146+
StringBuilder pathBuilder = new StringBuilder();
147+
for(String p : pathParts) {
148+
if(!p.isEmpty())
149+
pathBuilder.append("/").append(p);
150+
}
151+
operationPath = pathBuilder.toString();
152+
127153
String httpMethod = extractOperationMethod(apiOperation, method, SwaggerExtensions.chain());
128154

129155
Operation operation = parseMethod(method);
@@ -132,6 +158,12 @@ protected Swagger read(Class<?> cls, String parentPath, boolean readHidden, Stri
132158
operation.parameter(param);
133159
}
134160
}
161+
for(Parameter param : operation.getParameters()) {
162+
if(regexMap.get(param.getName()) != null) {
163+
String pattern = regexMap.get(param.getName());
164+
param.setPattern(pattern);
165+
}
166+
}
135167

136168
String protocols = apiOperation.protocols();
137169
if(!"".equals(protocols)) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import resources._
2+
3+
import com.wordnik.swagger.jaxrs.config._
4+
import com.wordnik.swagger.models.parameters._
5+
import com.wordnik.swagger.models.properties.MapProperty
6+
7+
import com.wordnik.swagger.models.Swagger
8+
import com.wordnik.swagger.jaxrs.Reader
9+
import com.wordnik.swagger.util.Json
10+
11+
import scala.collection.JavaConverters._
12+
13+
import org.junit.runner.RunWith
14+
import org.scalatest.junit.JUnitRunner
15+
import org.scalatest.FlatSpec
16+
import org.scalatest.Matchers
17+
18+
@RunWith(classOf[JUnitRunner])
19+
class RegexPathParamTest extends FlatSpec with Matchers {
20+
it should "scan a simple resource" in {
21+
val swagger = new Reader(new Swagger()).read(classOf[RegexPathParamResource])
22+
val get = swagger.getPaths().get("/{report_type}").getGet()
23+
val param = get.getParameters().get(0)
24+
param.getName() should be ("report_type")
25+
param.getPattern() should be ("[aA-zZ]+")
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package resources;
2+
3+
import com.wordnik.swagger.annotations.*;
4+
5+
import javax.ws.rs.*;
6+
import javax.ws.rs.core.Response;
7+
8+
import java.util.*;
9+
10+
@Api(value = "/external/info/")
11+
public class RegexPathParamResource {
12+
@GET
13+
@ApiOperation(value="this", tags="tag1")
14+
@Path("/{report_type:[aA-zZ]+}")
15+
public Response getThis(@ApiParam(value = "test") @PathParam("report_type") String param) {
16+
return Response.ok().build();
17+
}
18+
}

0 commit comments

Comments
 (0)