Skip to content

Commit

Permalink
fix: don't provide empty paths when using a prefix
Browse files Browse the repository at this point in the history
closes: #38663

Signed-off-by: Steve Hawkins <[email protected]>
(cherry picked from commit 236f3fc)
  • Loading branch information
shawkins authored and michbeck100 committed Feb 23, 2024
1 parent 04040f9 commit 5dd69e4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static void close() {

@RegisterExtension
static QuarkusUnitTest testExtension = new QuarkusUnitTest()
.overrideConfigKey("quarkus.http.root-path", "/app")
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
Expand Down Expand Up @@ -155,4 +156,9 @@ public void testBeanParamsInSubresource() {
.then()
.body(is("first and second"));
}

@Test
public void testRoot() throws Exception {
given().get().then().body(is("[app, ]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.lang.reflect.Proxy;
import java.util.List;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.UriInfo;

Expand All @@ -18,13 +20,21 @@ public class ResourceLocatorBaseResource {

private static final Logger LOG = Logger.getLogger(ResourceLocatorBaseResource.class);

@GET
@Produces("*/*")
public String getDefault(@Context UriInfo uri) {
LOG.debug("Here in BaseResource");
List<String> matchedURIs = uri.getMatchedURIs();
return matchedURIs.toString();
}

@Path("base/{param}/resources")
public Object getSubresource(@PathParam("param") String param, @Context UriInfo uri) {
LOG.debug("Here in BaseResource");
Assertions.assertEquals("1", param);
List<String> matchedURIs = uri.getMatchedURIs();
Assertions.assertEquals(2, matchedURIs.size());
Assertions.assertEquals("base/1/resources", matchedURIs.get(0));
Assertions.assertEquals("app/base/1/resources", matchedURIs.get(0));
Assertions.assertEquals("", matchedURIs.get(1));
for (String ancestor : matchedURIs)
LOG.debug(" " + ancestor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public String doGet(@Context UriInfo uri) {
LOG.info("Uri Ancestors for Subresource.doGet():");
List<String> matchedURIs = uri.getMatchedURIs();
Assertions.assertEquals(2, matchedURIs.size());
Assertions.assertEquals("base/1/resources", matchedURIs.get(0));
Assertions.assertEquals("app/base/1/resources", matchedURIs.get(0));
Assertions.assertEquals("", matchedURIs.get(1));
for (String ancestor : matchedURIs)
LOG.debug(" " + ancestor);
Expand All @@ -41,8 +41,8 @@ public Object getSubresource2(@Context UriInfo uri) {
LOG.info("Uri Ancestors for Subresource.getSubresource2():");
List<String> matchedURIs = uri.getMatchedURIs();
Assertions.assertEquals(3, matchedURIs.size());
Assertions.assertEquals("base/1/resources/subresource2", matchedURIs.get(0));
Assertions.assertEquals("base/1/resources", matchedURIs.get(1));
Assertions.assertEquals("app/base/1/resources/subresource2", matchedURIs.get(0));
Assertions.assertEquals("app/base/1/resources", matchedURIs.get(1));
Assertions.assertEquals("", matchedURIs.get(2));
for (String ancestor : matchedURIs)
LOG.debug(" " + ancestor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class ResourceLocatorSubresource2 {
public String doGet(@PathParam("param") String param, @Context UriInfo uri) {
LOG.debug("Uri Ancestors for Subresource2.doGet():");
Assertions.assertEquals(4, uri.getMatchedURIs().size());
Assertions.assertEquals("base/1/resources/subresource2/stuff/2/bar", uri.getMatchedURIs().get(0));
Assertions.assertEquals("base/1/resources/subresource2", uri.getMatchedURIs().get(1));
Assertions.assertEquals("base/1/resources", uri.getMatchedURIs().get(2));
Assertions.assertEquals("app/base/1/resources/subresource2/stuff/2/bar", uri.getMatchedURIs().get(0));
Assertions.assertEquals("app/base/1/resources/subresource2", uri.getMatchedURIs().get(1));
Assertions.assertEquals("app/base/1/resources", uri.getMatchedURIs().get(2));
Assertions.assertEquals("", uri.getMatchedURIs().get(3));
for (String ancestor : uri.getMatchedURIs())
LOG.debugv(" {0}", ancestor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ public String getPathWithoutPrefix() {
if (!prefix.isEmpty()) {
// FIXME: can we really have paths that don't start with the prefix if there's a prefix?
if (path.startsWith(prefix)) {
if (path.length() == prefix.length()) {
return "/";
}
return path.substring(prefix.length());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public String getPath(boolean decode) {
if (prefix.isEmpty())
return path;
// else skip the prefix
if (path.length() == prefix.length()) {
return "/";
}
return path.substring(prefix.length());
}

Expand Down

0 comments on commit 5dd69e4

Please sign in to comment.