gnodet-bot commented on code in PR #30803:
URL: https://github.com/apache/camel/pull/30803#discussion_r4700937031


##########
dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/BodyTypeFlow.java:
##########
@@ -309,6 +400,46 @@ private static String readerIn(JsonNode node) {
         return null;
     }
 
+    /** The REST operations of the file by the endpoint they send to: GET 
/stock/{id} for direct:getStock. */
+    private static Map<String, String> restOperations(JsonNode target) {
+        Map<String, String> answer = new LinkedHashMap<>();
+        if (target == null || !target.isArray()) {
+            return answer;
+        }
+        for (JsonNode entry : target) {
+            JsonNode rest = entry.isObject() ? entry.get("rest") : null;
+            if (rest == null || !rest.isObject()) {
+                continue;
+            }
+            String base = rest.has("path") ? rest.get("path").asString("") : 
"";
+            for (var it = rest.propertyNames().iterator(); it.hasNext();) {
+                String verb = it.next();
+                if (!REST_VERBS.contains(verb)) {
+                    continue;
+                }
+                JsonNode value = rest.get(verb);
+                for (JsonNode operation : value.isArray() ? value : 
List.of(value)) {
+                    String uri = operation.isObject() ? 
endpointOf(operation.get("to")) : null;
+                    if (uri != null) {
+                        String path = operation.has("path") ? 
operation.get("path").asString("") : "";
+                        answer.putIfAbsent(normalize(uri), 
verb.toUpperCase(Locale.ROOT) + " " + joinPath(base, path));
+                    }
+                }
+            }
+        }
+        return answer;
+    }
+
+    private static String joinPath(String base, String path) {
+        if (base.isEmpty()) {
+            return path.isEmpty() ? "/" : path;
+        }
+        if (path.isEmpty()) {
+            return base;
+        }
+        return base.endsWith("/") || path.startsWith("/") ? base + path : base 
+ "/" + path;
+    }
+
     /** The endpoints a REST verb without a body sends to, such as a get: that 
routes to direct:getStock. */
     private static Set<String> restEndpointsWithoutABody(JsonNode target) {

Review Comment:
   ⚠️ **Double slash when `base` ends with `/` and `path` starts with `/`.**
   
   The condition `base.endsWith("/") || path.startsWith("/")` is true whenever 
*either* side has the separator. When both have one (e.g. `base="/stock/"` and 
`path="/{sku}"`), it concatenates them directly → `"/stock//{sku}"`. An OpenAPI 
spec with a trailing slash on `rest.path` triggers this.
   
   ```suggestion
           if (base.endsWith("/") && path.startsWith("/")) {
               return base + path.substring(1);
           }
           return base.endsWith("/") || path.startsWith("/") ? base + path : 
base + "/" + path;
   ```



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/ProjectBodies.java:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.dsl.jbang.core.commands.ai;
+
+import java.nio.file.Path;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.dsl.jbang.core.commands.ai.ProjectRoutes.Route;
+import org.apache.camel.dsl.yaml.validator.BodyOrigin;
+import org.apache.camel.dsl.yaml.validator.ProjectFacts;
+import org.apache.camel.dsl.yaml.validator.YamlValidator;
+
+/**
+ * Where the message body of each YAML route of a project comes from, for 
{@code camel overview} (CAMEL-25200). It is
+ * the analysis of the body check of {@code camel validate}, made the way 
{@code camel validate} makes it: per file,
+ * with the same schema validator, only for a file that passes the schema, and 
with the GET operations of an OpenAPI
+ * contract beside the route known to carry no body. So the {@code 
body-never-set} findings of the two are the same.
+ */
+final class ProjectBodies {
+
+    private ProjectBodies() {
+    }
+
+    /**
+     * The body origin of each YAML route, by the key of the route in the 
overview.
+     *
+     * @param dir     the project directory, for the OpenAPI contracts the 
routes bind to; may be null
+     * @param sources the content of each route file by its path relative to 
the directory
+     * @param routes  the routes of the overview
+     */
+    static Map<String, BodyOrigin> analyze(
+            Path dir, Map<String, String> sources, List<Route> routes, 
CamelCatalog catalog) {
+        Map<String, BodyOrigin> answer = new LinkedHashMap<>();
+        YamlValidator validator = null;
+        for (Map.Entry<String, String> e : sources.entrySet()) {
+            String file = e.getKey();
+            String name = file.toLowerCase(Locale.ROOT);
+            if (!name.endsWith(".yaml") && !name.endsWith(".yml")) {
+                continue;
+            }
+            List<BodyOrigin> origins;
+            try {
+                if (validator == null) {
+                    validator = catalog != null
+                            ? SourceValidator.yamlValidator(catalog) : 
SourceValidator.yamlValidator();
+                }
+                Set<String> bodyless = dir != null ? 
OpenApiVerbs.bodylessEndpoints(e.getValue(), dir) : Set.of();
+                origins = validator.bodyOrigins(e.getValue(), new 
ProjectFacts(bodyless, Map.of(), null));
+            } catch (Exception ex) {
+                // the overview is still an overview without the bodies
+                continue;
+            }
+            if (!origins.isEmpty()) {
+                match(file, origins, routes, answer);
+            }

Review Comment:
   💡 **Consider logging at trace/debug level before swallowing the exception.**
   
   `catch (Exception ex) { continue; }` is correct for graceful degradation, 
but it makes it very hard to diagnose why body origins are missing for a file 
(malformed YAML? validator regression? missing catalog?). A `LOG.debug("body 
analysis failed for {}: {}", file, ex.getMessage())` would cost nothing at INFO 
level and save diagnostic time.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to