[
https://issues.apache.org/jira/browse/CAMEL-12943?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16695625#comment-16695625
]
ASF GitHub Bot commented on CAMEL-12943:
----------------------------------------
dmvolod closed pull request #2623: CAMEL-12943: Rest DSL generates invalid
swagger operation Id
URL: https://github.com/apache/camel/pull/2623
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
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 b0059553bae..83e79f34748 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
@@ -142,6 +142,19 @@ public void run() {
});
}
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;
+ }
+ }
+ }
}
}
@@ -169,7 +182,8 @@ public void run() {
}
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();
@@ -195,6 +209,19 @@ public void run() {
}
}
}
+
+ /**
+ * 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 5fcce056d7b..3e09b9a1406 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
@@ -658,6 +658,13 @@ public RouteDefinition route() {
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
//-------------------------------------------------------------------------
@@ -799,6 +806,7 @@ public static RouteDefinition
asRouteApiDefinition(CamelContext camelContext, Re
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
@@ -877,7 +885,7 @@ private void addRouteDefinition(CamelContext camelContext,
List<RouteDefinition>
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 00000000000..30b132765d9
--- /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
diff --git
a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java
index ac838059e34..a7b2d45e6e3 100644
---
a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java
+++
b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java
@@ -58,7 +58,7 @@ public void configure() throws Exception {
.param().name("body").type(RestParamType.body).description("The user to update
or create").endParam()
.to("bean:userService?method=updateUser")
- .get("/findAll").description("Find all
users").outTypeList(User.class)
+ .get("/findAll").description("Find all
users").outType(User[].class)
.responseMessage().message("All the found
users").endResponseMessage()
.to("bean:userService?method=listUsers");
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Rest DSL generates invalid swagger operation Id
> -----------------------------------------------
>
> Key: CAMEL-12943
> URL: https://issues.apache.org/jira/browse/CAMEL-12943
> Project: Camel
> Issue Type: Bug
> Components: camel-swagger
> Reporter: Thomas Diesler
> Assignee: Dmitry Volodin
> Priority: Major
> Fix For: 2.23.0
>
>
> With this code
>
> {code:java}
> rest("/customers").description("Customers REST service")
> .get("/\{id}")
> .bindingMode(RestBindingMode.auto)
> .id("getCustomerById")
> .description("Retrieves a customer for the specified id")
> .outType(Customer.class)
> .route()
> .process(exchange -> {
> Customer customer = new Customer();
> customer.setId(exchange.getIn().getHeader("id",
> Integer.class));
> customer.setFirstName("Kermit");
> customer.setLastName("The Frog");
> exchange.getOut().setBody(customer);
> })
> .endRest();
> {code}
>
> we see a swagger definition generated like this
>
> {code:java}
> "paths" : {
> "/customers/\{id}" : {
> "get" : {
> "tags" : [ "customers" ],
> "summary" : "Retrieves a customer for the specified id",
> "operationId" : "route2",
> "parameters" : [ {
> "name" : "id",
> "in" : "path",
> "required" : true,
> "type" : "string"
> } ],
> "responses" : {
> "200" : {
> "description" : "Output type",
> "schema" : {
> "$ref" : "#/definitions/Customer",
> "originalRef" : "Customer"
> },
> "responseSchema" : {
> "$ref" : "#/definitions/Customer",
> "originalRef" : "Customer"
> }
> }
> }
> }
> }
> },
> {code}
>
> The operationId seems to be incorrect.
>
> In our testsuite, which checks the latest Camel HEAD every Monday, we see ...
>
> {code:java}
> Caused by: java.lang.IllegalArgumentException: The specified operation with
> ID: `getCustomerById` cannot be found in the Swagger specification loaded
> from `http://localhost:8080/api/swagger`. Operations defined in the
> specification are: route2
> at
> org.apache.camel.component.rest.swagger.RestSwaggerEndpoint.createProducer(RestSwaggerEndpoint.java:198)
> at
> org.apache.camel.impl.ProducerCache.doGetProducer(ProducerCache.java:573)
> ... 190 more
> {code}
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)