allthingssecurity commented on code in PR #27587:
URL: https://github.com/apache/camel/pull/27587#discussion_r4227491612


##########
components/camel-cloudevents/src/main/java/org/apache/camel/component/cloudevents/transformer/CloudEventJsonDataTypeTransformer.java:
##########
@@ -76,28 +89,40 @@ public void transform(Message message, DataType fromType, 
DataType toType) {
             
cloudEventAttributes.putIfAbsent(cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).json(),
                     cloudEvent.getEventTime(message.getExchange()));
 
-            String body = MessageHelper.extractBodyAsString(message);
-            cloudEventAttributes.putIfAbsent("data", body);
-            cloudEventAttributes.putIfAbsent(
-                    
cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json(),
 dataContentType);
+            String dataContentTypeKey = 
cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json();
+            cloudEventAttributes.putIfAbsent(dataContentTypeKey, 
dataContentType);
+            // the data is written as the datacontenttype of the event says
+            Object eventDataContentType = 
cloudEventAttributes.get(dataContentTypeKey);
+            boolean jsonData = isJsonContentType(eventDataContentType);
+            if (message.getBody() instanceof byte[] bytes) {
+                String text = decodeText(bytes, 
ExchangeHelper.getCharset(message.getExchange()));
+                if (text != null) {
+                    cloudEventAttributes.putIfAbsent("data", text);
+                } else {
+                    // decoding these bytes as text would replace the invalid 
ones, so the data would be lost
+                    cloudEventAttributes.putIfAbsent("data_base64", 
Base64.getEncoder().encodeToString(bytes));

Review Comment:
   Done in f66031fdbb89: when neither `Content-Type` nor 
`CamelCloudEventDataContentType` is set and the bytes go to `data_base64`, 
`datacontenttype` is now `application/octet-stream`; a declared type is kept. 
`shouldWriteBytesThatAreNotValidTextAsBase64WhateverTheContentType` now asserts 
it for all three cases, including `null`.
   
   _Claude Code on behalf of allthingssecurity_



##########
components/camel-cloudevents/src/main/java/org/apache/camel/component/cloudevents/transformer/CloudEventJsonDataTypeTransformer.java:
##########
@@ -76,28 +89,40 @@ public void transform(Message message, DataType fromType, 
DataType toType) {
             
cloudEventAttributes.putIfAbsent(cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).json(),
                     cloudEvent.getEventTime(message.getExchange()));
 
-            String body = MessageHelper.extractBodyAsString(message);
-            cloudEventAttributes.putIfAbsent("data", body);
-            cloudEventAttributes.putIfAbsent(
-                    
cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json(),
 dataContentType);
+            String dataContentTypeKey = 
cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json();
+            cloudEventAttributes.putIfAbsent(dataContentTypeKey, 
dataContentType);
+            // the data is written as the datacontenttype of the event says
+            Object eventDataContentType = 
cloudEventAttributes.get(dataContentTypeKey);
+            boolean jsonData = isJsonContentType(eventDataContentType);
+            if (message.getBody() instanceof byte[] bytes) {
+                String text = decodeText(bytes, 
ExchangeHelper.getCharset(message.getExchange()));
+                if (text != null) {
+                    cloudEventAttributes.putIfAbsent("data", text);
+                } else {
+                    // decoding these bytes as text would replace the invalid 
ones, so the data would be lost
+                    cloudEventAttributes.putIfAbsent("data_base64", 
Base64.getEncoder().encodeToString(bytes));
+                }
+            } else {
+                cloudEventAttributes.putIfAbsent("data", 
MessageHelper.extractBodyAsString(message));

Review Comment:
   Fixed in this PR (f66031fdbb89), since it interacts with the scalar change: 
a null body now gives an event without `data`, with 
`shouldWriteNoDataForANullBody` and a line in the upgrade note.
   
   _Claude Code on behalf of allthingssecurity_



##########
components/camel-cloudevents/src/main/java/org/apache/camel/component/cloudevents/transformer/CloudEventJsonDataTypeTransformer.java:
##########
@@ -76,28 +89,40 @@ public void transform(Message message, DataType fromType, 
DataType toType) {
             
cloudEventAttributes.putIfAbsent(cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_TIME).json(),
                     cloudEvent.getEventTime(message.getExchange()));
 
-            String body = MessageHelper.extractBodyAsString(message);
-            cloudEventAttributes.putIfAbsent("data", body);
-            cloudEventAttributes.putIfAbsent(
-                    
cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json(),
 dataContentType);
+            String dataContentTypeKey = 
cloudEvent.mandatoryAttribute(CloudEvent.CAMEL_CLOUD_EVENT_DATA_CONTENT_TYPE).json();
+            cloudEventAttributes.putIfAbsent(dataContentTypeKey, 
dataContentType);
+            // the data is written as the datacontenttype of the event says
+            Object eventDataContentType = 
cloudEventAttributes.get(dataContentTypeKey);
+            boolean jsonData = isJsonContentType(eventDataContentType);

Review Comment:
   Agreed, done in f66031fdbb89: scalars are nested only when a JSON content 
type is actually declared (`Content-Type` or `CamelCloudEventDataContentType`); 
with only the default, objects and arrays are nested and scalars stay strings 
as with CAMEL-22339. 
`shouldNestOnlyJsonObjectsAndArraysWithoutADeclaredContentType` pins 
`1234567890123456789` and `null` staying strings, and the upgrade-guide entry 
says so.
   
   _Claude Code on behalf of allthingssecurity_



-- 
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