gnodet-bot commented on code in PR #30803:
URL: https://github.com/apache/camel/pull/30803#discussion_r4700919362
##########
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:
💡 `REST_VERBS` is defined here as a `Set<String>` and also exists in
`RouteGraph` as a `private static final List<String>`. Two parallel constants
for the same set of six HTTP verbs is a drift risk. Consider moving the
canonical definition to `RouteGraph` (already there as a `List`) and importing
it here, or exposing it as a package-private constant.
##########
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:
⚠️ **`Set.of().contains(null)` will throw `NullPointerException`** when
`r.fromUri()` is `null`.
`scheme(null)` returns `null` per its own null-guard. `Set.of("direct",
"seda", "direct-vm").contains(null)` throws `NullPointerException` per the JDK
spec for unmodifiable sets — unlike `HashSet`, which returns `false`. A route
with a `from` node that has no `uri` (e.g. a Kaoto-style object where
`endpointOf` returns `null`) hits this path.
`certainlyWithoutABody` already has the guard:
```java
String scheme = scheme(route.fromUri());
if (scheme == null) {
return false;
}
```
`origin()` is missing it. Fix:
```suggestion
String scheme = scheme(r.fromUri());
if (!ROUTE_CALLS.contains(scheme)) {
```
Then use `scheme` in the `certainlyWithoutABody` call below too (it already
passes `r.fromUri()` explicitly there, so `key` is enough — just drop the
redundant `scheme(r.fromUri())` call).
--
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]