Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply JAX-RS path regex to params #4415

Merged
merged 1 commit into from
May 22, 2023
Merged
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 @@ -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;
}
}