This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27307-aws2-cw-empty-body in repository https://gitbox.apache.org/repos/asf/camel.git
commit 51e14e7aa1c1b9ef67ea392dd3d8f9e8db69b8dd Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 20 19:27:55 2026 +0100 CAMEL-27307: camel-aws2-cw - putMetricData with an empty collection or array body fails with a clear message An empty collection or array body gave an empty metricData list, and the producer called putMetricData with it. Against the Floci emulator the call succeeded and nothing was put, so the message passed as if a data point was put. The producer now fails the exchange with an IllegalArgumentException ("The body is an empty collection: there is no MetricDatum to put", or "array"), before the call, so it does not depend on what the service does with no metric data. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../org/apache/camel/catalog/components/aws2-cw.json | 2 +- .../apache/camel/catalog/docs/aws2-cw-component.adoc | 9 +++++---- .../org/apache/camel/component/aws2/cw/aws2-cw.json | 2 +- .../src/main/docs/aws2-cw-component.adoc | 9 +++++---- .../apache/camel/component/aws2/cw/Cw2Endpoint.java | 7 ++++--- .../apache/camel/component/aws2/cw/Cw2Producer.java | 11 ++++++++--- .../camel/component/aws2/cw/CwPutMetricDataTest.java | 19 +++++++++++++++++++ 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws2-cw.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws2-cw.json index fb941f78dfaa..9e2c39511465 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws2-cw.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws2-cw.json @@ -67,7 +67,7 @@ "CamelAwsCwIsTruncated": { "index": 13, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "Boolean", "deprecated": false, "autowired": false, "secret": false, "description": "Whether the response has more results (is truncated).", "constantName": "org.apache.camel.component.aws2.cw.Cw2Constants#IS_TRUNCATED" } }, "bodyTypes": { - "producerIn": { "carriers": [ "any" ], "description": "putMetricData sends a body that is a MetricDatum of the AWS SDK or a collection or an array of them, and one with another element fails; with another body the metric comes from the CamelAwsCwMetric* headers or the name, value and unit options. The other operations do not use the body" }, + "producerIn": { "carriers": [ "any" ], "description": "putMetricData sends a body that is a MetricDatum of the AWS SDK or a collection or an array of them, and an empty one or one with another element fails; with another body the metric comes from the CamelAwsCwMetric* headers or the name, value and unit options. The other operations do not use the body" }, "producerOut": { "carriers": [ "unchanged" ], "byOption": [ { "option": "operation", "value": "listMetrics", "carriers": [ "pojo" ], "javaType": "software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse" }, { "option": "operation", "value": "describeAlarms", "carriers": [ "pojo" ], "javaType": "software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse" }, { "option": "operation", "value": "describeAlarmsForMetric", "carriers": [ "pojo" ], "javaType": "softwa [...] }, "properties": { diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws2-cw-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws2-cw-component.adoc index 750e27f1805f..7250186284fe 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws2-cw-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws2-cw-component.adoc @@ -180,10 +180,11 @@ exchange.getIn().setHeader(Cw2Constants.METRIC_UNIT, "Count"); === The metric data of putMetricData The `putMetricData` operation sends the body when it is a `MetricDatum`, or a collection or an array of `MetricDatum` -(each element must be a `MetricDatum`, otherwise the exchange fails). For any other body, the metric is built from the -headers and the options: `CamelAwsCwMetricName` or the `name` option, `CamelAwsCwMetricValue` or the `value` option -(1 by default), `CamelAwsCwMetricUnit` or the `unit` option (`Count` by default), and `CamelAwsCwMetricTimestamp` or -the `timestamp` option. +(each element must be a `MetricDatum`, otherwise the exchange fails; an empty collection or array fails the exchange +too, as there is no metric to put). For any other body, the metric is built from the headers and the options: +`CamelAwsCwMetricName` or the `name` option, `CamelAwsCwMetricValue` or the `value` option (1 by default), +`CamelAwsCwMetricUnit` or the `unit` option (`Count` by default), and `CamelAwsCwMetricTimestamp` or the `timestamp` +option. The unit is the name of a `StandardUnit` of the AWS SDK, such as `Count`, `Bytes` or `Kilobytes/Second`, in any case; an unknown unit fails the exchange. The timestamp header is an `Instant` or a `java.util.Date`. diff --git a/components/camel-aws/camel-aws2-cw/src/generated/resources/META-INF/org/apache/camel/component/aws2/cw/aws2-cw.json b/components/camel-aws/camel-aws2-cw/src/generated/resources/META-INF/org/apache/camel/component/aws2/cw/aws2-cw.json index fb941f78dfaa..9e2c39511465 100644 --- a/components/camel-aws/camel-aws2-cw/src/generated/resources/META-INF/org/apache/camel/component/aws2/cw/aws2-cw.json +++ b/components/camel-aws/camel-aws2-cw/src/generated/resources/META-INF/org/apache/camel/component/aws2/cw/aws2-cw.json @@ -67,7 +67,7 @@ "CamelAwsCwIsTruncated": { "index": 13, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "Boolean", "deprecated": false, "autowired": false, "secret": false, "description": "Whether the response has more results (is truncated).", "constantName": "org.apache.camel.component.aws2.cw.Cw2Constants#IS_TRUNCATED" } }, "bodyTypes": { - "producerIn": { "carriers": [ "any" ], "description": "putMetricData sends a body that is a MetricDatum of the AWS SDK or a collection or an array of them, and one with another element fails; with another body the metric comes from the CamelAwsCwMetric* headers or the name, value and unit options. The other operations do not use the body" }, + "producerIn": { "carriers": [ "any" ], "description": "putMetricData sends a body that is a MetricDatum of the AWS SDK or a collection or an array of them, and an empty one or one with another element fails; with another body the metric comes from the CamelAwsCwMetric* headers or the name, value and unit options. The other operations do not use the body" }, "producerOut": { "carriers": [ "unchanged" ], "byOption": [ { "option": "operation", "value": "listMetrics", "carriers": [ "pojo" ], "javaType": "software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse" }, { "option": "operation", "value": "describeAlarms", "carriers": [ "pojo" ], "javaType": "software.amazon.awssdk.services.cloudwatch.model.DescribeAlarmsResponse" }, { "option": "operation", "value": "describeAlarmsForMetric", "carriers": [ "pojo" ], "javaType": "softwa [...] }, "properties": { diff --git a/components/camel-aws/camel-aws2-cw/src/main/docs/aws2-cw-component.adoc b/components/camel-aws/camel-aws2-cw/src/main/docs/aws2-cw-component.adoc index 750e27f1805f..7250186284fe 100644 --- a/components/camel-aws/camel-aws2-cw/src/main/docs/aws2-cw-component.adoc +++ b/components/camel-aws/camel-aws2-cw/src/main/docs/aws2-cw-component.adoc @@ -180,10 +180,11 @@ exchange.getIn().setHeader(Cw2Constants.METRIC_UNIT, "Count"); === The metric data of putMetricData The `putMetricData` operation sends the body when it is a `MetricDatum`, or a collection or an array of `MetricDatum` -(each element must be a `MetricDatum`, otherwise the exchange fails). For any other body, the metric is built from the -headers and the options: `CamelAwsCwMetricName` or the `name` option, `CamelAwsCwMetricValue` or the `value` option -(1 by default), `CamelAwsCwMetricUnit` or the `unit` option (`Count` by default), and `CamelAwsCwMetricTimestamp` or -the `timestamp` option. +(each element must be a `MetricDatum`, otherwise the exchange fails; an empty collection or array fails the exchange +too, as there is no metric to put). For any other body, the metric is built from the headers and the options: +`CamelAwsCwMetricName` or the `name` option, `CamelAwsCwMetricValue` or the `value` option (1 by default), +`CamelAwsCwMetricUnit` or the `unit` option (`Count` by default), and `CamelAwsCwMetricTimestamp` or the `timestamp` +option. The unit is the name of a `StandardUnit` of the AWS SDK, such as `Count`, `Bytes` or `Kilobytes/Second`, in any case; an unknown unit fails the exchange. The timestamp header is an `Instant` or a `java.util.Date`. diff --git a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Endpoint.java b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Endpoint.java index 22200d679b6b..85912a42505c 100644 --- a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Endpoint.java +++ b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Endpoint.java @@ -41,9 +41,10 @@ import software.amazon.awssdk.services.cloudwatch.CloudWatchClient; producerOnly = true, category = { Category.CLOUD, Category.MONITORING }, headersClass = Cw2Constants.class) @BodyTypes(producerIn = @BodyType(value = Carrier.ANY, description = "putMetricData sends a body that is a MetricDatum of the AWS SDK or a" - + " collection or an array of them, and one with another element fails; with" - + " another body the metric comes from the CamelAwsCwMetric* headers or the" - + " name, value and unit options. The other operations do not use the body"), + + " collection or an array of them, and an empty one or one with another" + + " element fails; with another body the metric comes from the" + + " CamelAwsCwMetric* headers or the name, value and unit options. The other" + + " operations do not use the body"), producerOut = @BodyType(value = Carrier.UNCHANGED, byOption = { @BodyType.When(option = "operation", value = "listMetrics", carriers = Carrier.POJO, diff --git a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Producer.java b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Producer.java index e1443c6ba2f3..b9ccd0ac7da6 100644 --- a/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Producer.java +++ b/components/camel-aws/camel-aws2-cw/src/main/java/org/apache/camel/component/aws2/cw/Cw2Producer.java @@ -217,10 +217,10 @@ public class Cw2Producer extends DefaultProducer { return Arrays.asList(metricDatum); } if (body instanceof Collection<?> collection) { - return toMetricData(collection); + return toMetricData(collection, "collection"); } if (body instanceof Object[] array) { - return toMetricData(Arrays.asList(array)); + return toMetricData(Arrays.asList(array), "array"); } MetricDatum.Builder metricDatum = MetricDatum.builder().metricName(determineName(exchange)) @@ -230,7 +230,12 @@ public class Cw2Producer extends DefaultProducer { return Arrays.asList(metricDatum.build()); } - private static List<MetricDatum> toMetricData(Collection<?> elements) { + private static List<MetricDatum> toMetricData(Collection<?> elements, String kind) { + if (elements.isEmpty()) { + // fail here, as what the service does with no metric data differs (an emulator accepts it and puts nothing) + throw new IllegalArgumentException( + "The body is an empty " + kind + ": there is no MetricDatum to put"); + } List<MetricDatum> metricData = new ArrayList<>(elements.size()); for (Object element : elements) { if (element instanceof MetricDatum metricDatum) { diff --git a/components/camel-aws/camel-aws2-cw/src/test/java/org/apache/camel/component/aws2/cw/CwPutMetricDataTest.java b/components/camel-aws/camel-aws2-cw/src/test/java/org/apache/camel/component/aws2/cw/CwPutMetricDataTest.java index 6477dbb80345..7f38fa42dd4e 100644 --- a/components/camel-aws/camel-aws2-cw/src/test/java/org/apache/camel/component/aws2/cw/CwPutMetricDataTest.java +++ b/components/camel-aws/camel-aws2-cw/src/test/java/org/apache/camel/component/aws2/cw/CwPutMetricDataTest.java @@ -32,6 +32,7 @@ import software.amazon.awssdk.services.cloudwatch.model.StandardUnit; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -94,6 +95,24 @@ public class CwPutMetricDataTest extends CamelTestSupport { assertTrue(cause.getMessage().contains("java.lang.String"), cause.getMessage()); } + @Test + public void emptyListFails() { + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.sendBody("direct:start", List.of())); + IllegalArgumentException cause = assertInstanceOf(IllegalArgumentException.class, e.getCause()); + assertEquals("The body is an empty collection: there is no MetricDatum to put", cause.getMessage()); + assertNull(cloudWatchClient.getLastPutMetricDataRequest(), "putMetricData must not be called"); + } + + @Test + public void emptyArrayFails() { + CamelExecutionException e = assertThrows(CamelExecutionException.class, + () -> template.sendBody("direct:start", new MetricDatum[0])); + IllegalArgumentException cause = assertInstanceOf(IllegalArgumentException.class, e.getCause()); + assertEquals("The body is an empty array: there is no MetricDatum to put", cause.getMessage()); + assertNull(cloudWatchClient.getLastPutMetricDataRequest(), "putMetricData must not be called"); + } + @Test public void otherBodyTakesMetricFromHeadersAndOptions() { template.sendBodyAndHeader("direct:start", "some text", Cw2Constants.METRIC_VALUE, 5d);
