You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/12/05 10:22:09 UTC

[camel] 01/43: CAMEL-12943: Rest DSL generates invalid swagger operation Id

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 81173ecc7dec6ba8473d5fb30b57db844c2acaf7
Author: Dmitry Volodin <dm...@gmail.com>
AuthorDate: Mon Nov 19 19:22:27 2018 +0300

    CAMEL-12943: Rest DSL generates invalid swagger operation Id
---
 .../apache/camel/model/RouteDefinitionHelper.java  | 29 +++++++++++-
 .../apache/camel/model/rest/RestDefinition.java    | 10 ++++-
 .../camel/impl/RouteIdRestDefinitionTest.java      | 52 ++++++++++++++++++++++
 3 files changed, 89 insertions(+), 2 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
index 95f6c0b..805514e 100644
--- a/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
@@ -144,6 +144,19 @@ public final class RouteDefinitionHelper {
                     });
                 }
                 customIds.add(id);
+            } else {
+                RestDefinition rest = route.getRestDefinition();
+                if (rest != null && route.isRest()) {
+                    VerbDefinition verb = findVerbDefinition(rest, route.getInputs().get(0).getUri());
+                    if (verb != null) {
+                        String id = verb.getId();
+                        if (verb.hasCustomIdAssigned() && ObjectHelper.isNotEmpty(id) && !customIds.contains(id)) {
+                            route.setId(id);
+                            customIds.add(id);
+                            break;
+                        }
+                    }
+                }
             }
         }
 
@@ -171,7 +184,8 @@ public final class RouteDefinitionHelper {
             }
             RestDefinition rest = route.getRestDefinition();
             if (rest != null && route.isRest()) {
-                for (VerbDefinition verb : rest.getVerbs()) {
+                VerbDefinition verb = findVerbDefinition(rest, route.getInputs().get(0).getUri());
+                if (verb != null) {
                     String id = verb.idOrCreate(context.getNodeIdFactory());
                     if (!verb.getUsedForGeneratingNodeId()) {
                         id = route.getId();
@@ -197,6 +211,19 @@ public final class RouteDefinitionHelper {
             }
         }
     }
+    
+    /**
+     * Find verb associated with the route by mapping uri
+     */
+    private static VerbDefinition findVerbDefinition(RestDefinition rest, String endpointUri) {
+        for (VerbDefinition verb : rest.getVerbs()) {
+            String verbUri = rest.buildFromUri(verb);
+            if (endpointUri.startsWith(verbUri)) {
+                return verb;
+            }
+        }
+        return null;
+    }
 
     /**
      * Validates that the target route has no duplicate id's from any of the existing routes.
diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
index d672225..6d9755f 100644
--- a/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestDefinition.java
@@ -611,6 +611,13 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
         verb.setRoute(route);
         return route;
     }
+    
+    /**
+     * Build the from endpoint uri for the verb
+     */
+    public String buildFromUri(VerbDefinition verb) {
+        return "rest:" + verb.asVerb() + ":" + buildUri(verb);
+    }
 
     // Implementation
     //-------------------------------------------------------------------------
@@ -750,6 +757,7 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
         return answer;
     }
 
+    @SuppressWarnings("rawtypes")
     private void addRouteDefinition(CamelContext camelContext, List<RouteDefinition> answer, String component) {
         for (VerbDefinition verb : getVerbs()) {
             // either the verb has a singular to or a embedded route
@@ -828,7 +836,7 @@ public class RestDefinition extends OptionalIdentifiedDefinition<RestDefinition>
             route.setRestBindingDefinition(binding);
 
             // create the from endpoint uri which is using the rest component
-            String from = "rest:" + verb.asVerb() + ":" + buildUri(verb);
+            String from = buildFromUri(verb);
 
             // append options
             Map<String, Object> options = new HashMap<>();
diff --git a/camel-core/src/test/java/org/apache/camel/impl/RouteIdRestDefinitionTest.java b/camel-core/src/test/java/org/apache/camel/impl/RouteIdRestDefinitionTest.java
new file mode 100644
index 0000000..30b1327
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/RouteIdRestDefinitionTest.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.rest.DummyRestConsumerFactory;
+import org.apache.camel.component.rest.DummyRestProcessorFactory;
+import org.junit.Test;
+
+public class RouteIdRestDefinitionTest extends ContextTestSupport {
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("dummy-rest", new DummyRestConsumerFactory());
+        jndi.bind("dummy-rest-api", new DummyRestProcessorFactory());
+        return jndi;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start1?timeout=30000").to("mock:result");
+                from("direct:start2").to("mock:result");
+                rest("/say/hello").get("/bar").id("getSayHelloBar").to("mock:result");
+            }
+        };
+    }
+
+    @Test
+    public void testSayHelloBar() {
+        assertEquals("getSayHelloBar", context.getRouteDefinitions().get(2).getId());
+    }
+    
+}
\ No newline at end of file