davsclaus commented on code in PR #30803:
URL: https://github.com/apache/camel/pull/30803#discussion_r4701070374
##########
dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/BodyTypeFlow.java:
##########
@@ -56,106 +59,194 @@ final class BodyTypeFlow {
/** The steps that work on the body itself, and have nothing to work on
when there is none. */
private static final Set<String> STEPS_THAT_NEED_THE_BODY =
Set.of("unmarshal", "marshal", "convertBodyTo");
+ /** The verbs of the REST DSL. */
+ private static final Set<String> REST_VERBS = Set.of("get", "post", "put",
"delete", "patch", "head");
+
/** The REST verbs that carry no body, so a route they send to starts with
none. */
private static final Set<String> VERBS_WITHOUT_BODY = Set.of("get",
"delete", "head");
private BodyTypeFlow() {
}
+ /** The components of the routes a route calls with its own message. */
+ private static final Set<String> ROUTE_CALLS = Set.of("direct", "seda",
"direct-vm");
+
/**
+ * Where the body of each route of the file comes from, with the message
of the check for a route that reads a body
+ * it cannot have: what {@link #report} reports, and what {@code camel
overview} shows (CAMEL-25200).
+ *
* @param known the endpoints the caller knows deliver no body, such
as {@code direct:getStock} for the GET
* operation of an OpenAPI specification the route binds
to (CAMEL-24844 phase B)
* @param bodyTypes the body types of the catalog, which say the consumers
that give no body (timer, cron)
*/
- static void check(JsonNode target, NodePath path, List<Error> errors,
Set<String> known, CatalogBodyTypes bodyTypes) {
+ static List<BodyOrigin> analyze(JsonNode target, Set<String> known,
CatalogBodyTypes bodyTypes) {
List<Route> routes = routes(target);
+ List<BodyOrigin> answer = new ArrayList<>();
if (routes.isEmpty()) {
- return;
+ return answer;
}
- // the graph: which routes send to the endpoint a route starts from
- Map<String, List<Route>> byFrom = new HashMap<>();
- for (Route r : routes) {
- if (r.fromUri() != null) {
- byFrom.computeIfAbsent(normalize(r.fromUri()), k -> new
ArrayList<>()).add(r);
+ Graph graph = new Graph(target, routes, known, bodyTypes);
+ for (int i = 0; i < routes.size(); i++) {
+ answer.add(graph.origin(i, routes.get(i)));
+ }
+ return answer;
+ }
+
+ /** Adds the message of each route that reads a body it cannot have, as
{@link #analyze} found them. */
+ static void report(List<BodyOrigin> origins, NodePath path, List<Error>
errors) {
+ for (BodyOrigin origin : origins) {
+ if (origin.problem() != null) {
+ errors.add(Error.builder()
+ .keyword("type")
+ .instanceLocation(path)
+ .messageKey("type")
+ .format(new MessageFormat("{0}"))
+ .arguments(origin.problem())
+ .build());
}
}
- Map<Route, List<Route>> callers = new HashMap<>();
- for (Route caller : routes) {
- for (String uri : sendsTo(caller.steps())) {
- for (Route target2 : byFrom.getOrDefault(normalize(uri),
List.of())) {
- callers.computeIfAbsent(target2, k -> new
ArrayList<>()).add(caller);
+ }
+
+ /** The routes of a file and the ways a message reaches each of them. */
+ private static final class Graph {
+
+ private final Map<Route, List<Route>> callers = new HashMap<>();
+ private final Map<String, String> rest;
+ private final Set<String> restless;
+ private final Set<String> known;
+ private final Map<String, List<RouteGraph.ErrorPath>> errorPaths = new
HashMap<>();
+ private final CatalogBodyTypes bodyTypes;
+
+ Graph(JsonNode target, List<Route> routes, Set<String> known,
CatalogBodyTypes bodyTypes) {
+ this.bodyTypes = bodyTypes;
+ // the graph: which routes send to the endpoint a route starts from
+ Map<String, List<Route>> byFrom = new HashMap<>();
+ for (Route r : routes) {
+ if (r.fromUri() != null) {
+ byFrom.computeIfAbsent(normalize(r.fromUri()), k -> new
ArrayList<>()).add(r);
}
}
+ for (Route caller : routes) {
+ for (String uri : sendsTo(caller.steps())) {
+ for (Route target2 : byFrom.getOrDefault(normalize(uri),
List.of())) {
+ callers.computeIfAbsent(target2, k -> new
ArrayList<>()).add(caller);
+ }
+ }
+ }
+ this.rest = restOperations(target);
+ this.restless = new HashSet<>(restEndpointsWithoutABody(target));
+ this.known = new HashSet<>();
+ for (String uri : known) {
+ this.restless.add(normalize(uri));
+ this.known.add(normalize(uri));
+ }
+ for (RouteGraph.ErrorPath p : RouteGraph.errorPaths(target)) {
+ errorPaths.computeIfAbsent(p.endpoint(), k -> new
ArrayList<>()).add(p);
+ }
}
- Set<String> restless = new
HashSet<>(restEndpointsWithoutABody(target));
- for (String uri : known) {
- restless.add(normalize(uri));
- }
- for (Route r : routes) {
+
+ BodyOrigin origin(int index, Route r) {
+ boolean noBody = certainlyWithoutABody(r, new HashSet<>());
String reader = firstBodyReaderBeforeAnyProducer(r.steps(),
bodyTypes);
- if (reader == null) {
- continue;
+ String problem = reader != null && noBody ? problem(r, reader) :
null;
+ String setBy = null;
+ int setAt = 0;
+ if (noBody && r.steps() != null && r.steps().isArray()) {
+ for (int i = 0; i < r.steps().size(); i++) {
+ if (producesTheBody(r.steps().get(i), bodyTypes)) {
+ setBy = firstName(r.steps().get(i));
+ setAt = i + 1;
+ break;
+ }
+ }
}
- if (!certainlyWithoutABody(r, callers, restless, new HashSet<>(),
bodyTypes)) {
- continue;
+ String key = normalize(r.fromUri());
+ String origin;
+ String via = null;
+ List<String> who = List.of();
+ if (!ROUTE_CALLS.contains(scheme(r.fromUri()))) {
+ origin = BodyOrigin.CONSUMER;
+ } else if (callers.containsKey(r)) {
+ origin = BodyOrigin.CALLER;
+ via = key;
Review Comment:
Fixed in 80abf4a10425: `origin()` now reads the scheme once and a `from`
without an endpoint gets the `UNKNOWN` origin (rather than `CONSUMER`, as
nothing is known about where its body comes from). The schema rejects such a
route before `camel validate` / `camel overview` get there, so the new
`BodyOriginTest.testFromWithoutAnEndpoint` calls `BodyTypeFlow.analyze`
directly; it threw the NPE before the fix.
_Claude Code on behalf of davsclaus_
##########
dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/BodyTypeFlow.java:
##########
@@ -56,106 +59,194 @@ final class BodyTypeFlow {
/** The steps that work on the body itself, and have nothing to work on
when there is none. */
private static final Set<String> STEPS_THAT_NEED_THE_BODY =
Set.of("unmarshal", "marshal", "convertBodyTo");
+ /** The verbs of the REST DSL. */
+ private static final Set<String> REST_VERBS = Set.of("get", "post", "put",
"delete", "patch", "head");
+
/** The REST verbs that carry no body, so a route they send to starts with
none. */
private static final Set<String> VERBS_WITHOUT_BODY = Set.of("get",
"delete", "head");
Review Comment:
Done in 80abf4a10425: `RouteGraph.REST_VERBS` is now package-private and
`BodyTypeFlow` imports it. It stays a `List` there, as `RouteGraph` walks the
verbs in a fixed order.
_Claude Code on behalf of davsclaus_
##########
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:
Fixed in 80abf4a10425 as suggested;
`BodyOriginTest.testRestPathWithATrailingSlash` checks `path: /stock/` +
`/{sku}` gives `GET /stock/{sku}` (it gave `/stock//{sku}` before).
_Claude Code on behalf of davsclaus_
--
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]