You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/03/31 08:32:21 UTC
(camel) 01/01: CAMEL-20637: camel-rest-dsl - Matching rest operation may not select correct
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch rest-pet
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2356c0248b13f259e31f88ad4dc5a9be256ac6cd
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Mar 31 10:32:02 2024 +0200
CAMEL-20637: camel-rest-dsl - Matching rest operation may not select correct
---
.../support/RestConsumerContextPathMatcher.java | 10 ++++
.../RestConsumerContextPathMatcherTest.java | 58 ++++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java b/core/camel-support/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java
index a5d32037997..ba534ae8a44 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java
@@ -171,6 +171,16 @@ public final class RestConsumerContextPathMatcher {
.sorted(Comparator.comparingInt(o -> -1 * o.getConsumerPath().length())).findFirst().orElse(null);
}
+ // is there a direct match by with a different VERB, as then this call is not allowed
+ if (answer == null) {
+ for (ConsumerPath<T> entry : consumerPaths) {
+ if (matchRestPath(requestPath, entry.getConsumerPath(), false)) {
+ // okay we have direct match but for another VERB so this call is not allowed
+ return null;
+ }
+ }
+ }
+
if (answer != null) {
return answer;
}
diff --git a/core/camel-support/src/test/java/org/apache/camel/support/RestConsumerContextPathMatcherTest.java b/core/camel-support/src/test/java/org/apache/camel/support/RestConsumerContextPathMatcherTest.java
index d079e03b701..067905951ab 100644
--- a/core/camel-support/src/test/java/org/apache/camel/support/RestConsumerContextPathMatcherTest.java
+++ b/core/camel-support/src/test/java/org/apache/camel/support/RestConsumerContextPathMatcherTest.java
@@ -22,6 +22,7 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class RestConsumerContextPathMatcherTest {
@@ -125,4 +126,61 @@ public class RestConsumerContextPathMatcherTest {
assertEquals(path2.getConsumerPath(), "/camel/foo/{id}");
assertEquals(path3.getConsumerPath(), "/camel/*");
}
+
+ @Test
+ public void testRestConsumerContextPathMatcherPetStore() {
+ List<RestConsumerContextPathMatcher.ConsumerPath<MockConsumerPath>> consumerPaths = new ArrayList<>();
+ consumerPaths.add(new MockConsumerPath("POST", "/pet"));
+ consumerPaths.add(new MockConsumerPath("PUT", "/pet"));
+ consumerPaths.add(new MockConsumerPath("GET", "/pet/findByStatus"));
+ consumerPaths.add(new MockConsumerPath("GET", "/pet/findByTags"));
+ consumerPaths.add(new MockConsumerPath("DELETE", "/pet/{petId}"));
+ consumerPaths.add(new MockConsumerPath("GET", "/pet/{petId}"));
+ consumerPaths.add(new MockConsumerPath("POST", "/pet/{petId}"));
+ consumerPaths.add(new MockConsumerPath("POST", "/pet/{petId}/uploadImage"));
+
+ RestConsumerContextPathMatcher.register("/api/v3/*");
+
+ RestConsumerContextPathMatcher.ConsumerPath<?> path1 = RestConsumerContextPathMatcher.matchBestPath("GET",
+ "/pet", consumerPaths);
+ assertNull(path1);
+ RestConsumerContextPathMatcher.ConsumerPath<?> path2 = RestConsumerContextPathMatcher.matchBestPath("POST",
+ "/pet", consumerPaths);
+ assertEquals(path2.getConsumerPath(), "/pet");
+
+ RestConsumerContextPathMatcher.ConsumerPath<?> path3 = RestConsumerContextPathMatcher.matchBestPath("GET",
+ "/pet/findByStatus", consumerPaths);
+ assertEquals(path3.getConsumerPath(), "/pet/findByStatus");
+ RestConsumerContextPathMatcher.ConsumerPath<?> path4 = RestConsumerContextPathMatcher.matchBestPath("DELETE",
+ "/pet/findByStatus", consumerPaths);
+ assertNull(path4);
+
+ RestConsumerContextPathMatcher.ConsumerPath<?> path5 = RestConsumerContextPathMatcher.matchBestPath("GET",
+ "/pet/findByTags", consumerPaths);
+ assertEquals(path5.getConsumerPath(), "/pet/findByTags");
+ RestConsumerContextPathMatcher.ConsumerPath<?> path6 = RestConsumerContextPathMatcher.matchBestPath("POST",
+ "/pet/findByStatus", consumerPaths);
+ assertNull(path6);
+
+ RestConsumerContextPathMatcher.ConsumerPath<?> path7 = RestConsumerContextPathMatcher.matchBestPath("GET",
+ "/pet/123", consumerPaths);
+ assertEquals(path7.getConsumerPath(), "/pet/{petId}");
+ RestConsumerContextPathMatcher.ConsumerPath<?> path8 = RestConsumerContextPathMatcher.matchBestPath("POST",
+ "/pet/222", consumerPaths);
+ assertEquals(path8.getConsumerPath(), "/pet/{petId}");
+ RestConsumerContextPathMatcher.ConsumerPath<?> path9 = RestConsumerContextPathMatcher.matchBestPath("DELETE",
+ "/pet/333", consumerPaths);
+ assertEquals(path9.getConsumerPath(), "/pet/{petId}");
+ RestConsumerContextPathMatcher.ConsumerPath<?> path10 = RestConsumerContextPathMatcher.matchBestPath("PUT",
+ "/pet/444", consumerPaths);
+ assertNull(path10);
+
+ RestConsumerContextPathMatcher.ConsumerPath<?> path11 = RestConsumerContextPathMatcher.matchBestPath("POST",
+ "/pet/123/uploadImage", consumerPaths);
+ assertEquals(path11.getConsumerPath(), "/pet/{petId}/uploadImage");
+ RestConsumerContextPathMatcher.ConsumerPath<?> path12 = RestConsumerContextPathMatcher.matchBestPath("DELETE",
+ "/pet/222/uploadImage", consumerPaths);
+ assertNull(path12);
+ }
+
}