Skip to content

Commit

Permalink
apply JAX-RS path regex to params
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed May 22, 2023
1 parent 3231aaa commit 2dfa1cc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ public OpenAPI read(Class<?> cls,
continue;
}
setPathItemOperation(pathItemObject, httpMethod, operation);

applyPathParamsPatterns(operation, regexMap);
paths.addPathItem(operationPath, pathItemObject);
if (openAPI.getPaths() != null) {
this.paths.putAll(openAPI.getPaths());
Expand Down Expand Up @@ -680,6 +680,19 @@ public OpenAPI read(Class<?> cls,
return openAPI;
}

protected void applyPathParamsPatterns(Operation operation, Map<String, String> patternsMap) {
if (operation.getParameters() == null) {
return;
}
operation.getParameters().stream()
.filter(p -> patternsMap.containsKey(p.getName()))
.filter(p -> "path".equals(p.getIn()))
.filter(p -> p.getSchema() != null)
.filter(p -> StringUtils.isBlank(p.getSchema().getPattern()))
.filter(p -> !Parameter.StyleEnum.MATRIX.equals(p.getStyle()))
.filter(p -> "string" == p.getSchema().getType() || (p.getSchema().getTypes() != null && p.getSchema().getTypes().contains("string")))
.forEach(p -> p.getSchema().setPattern(patternsMap.get(p.getName())));
}
protected Content processContent(Content content, Schema schema, Consumes methodConsumes, Consumes classConsumes) {
if (content == null) {
content = new Content();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import io.swagger.v3.jaxrs2.resources.Ticket3587Resource;
import io.swagger.v3.jaxrs2.resources.Ticket3731BisResource;
import io.swagger.v3.jaxrs2.resources.Ticket3731Resource;
import io.swagger.v3.jaxrs2.resources.Ticket4412Resource;
import io.swagger.v3.jaxrs2.resources.UploadResource;
import io.swagger.v3.jaxrs2.resources.UrlEncodedResourceWithEncodings;
import io.swagger.v3.jaxrs2.resources.UserAnnotationResource;
Expand Down Expand Up @@ -3063,4 +3064,33 @@ public void testResponseReturnType() {
" type: string";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}

@Test
public void test4412PathWildcards() {
Reader reader = new Reader(new OpenAPI());

OpenAPI openAPI = reader.read(Ticket4412Resource.class);
String yaml = "openapi: 3.0.1\n" +
"paths:\n" +
" /test/sws/{var}:\n" +
" get:\n" +
" operationId: getCart\n" +
" parameters:\n" +
" - name: var\n" +
" in: path\n" +
" required: true\n" +
" schema:\n" +
" pattern: .*\n" +
" type: string\n" +
" responses:\n" +
" default:\n" +
" description: default response\n" +
" content:\n" +
" text/xml:\n" +
" schema:\n" +
" type: array\n" +
" items:\n" +
" type: string";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.swagger.v3.jaxrs2.resources;

import io.swagger.v3.oas.annotations.Parameter;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/test")
public class Ticket4412Resource {
@Path("/sws/{var:.*}")
@GET
@Produces(MediaType.TEXT_XML)
public List<String> getCart(@PathParam("var") String var) {
return null;
}
}

0 comments on commit 2dfa1cc

Please sign in to comment.