oscerd commented on code in PR #28956:
URL: https://github.com/apache/camel/pull/28956#discussion_r4446653725


##########
components/camel-azure/camel-azure-key-vault/src/main/java/org/apache/camel/component/azure/key/vault/KeyVaultProducer.java:
##########
@@ -66,11 +80,54 @@ private void createSecret(Exchange exchange) throws 
InvalidPayloadException {
             throw new IllegalArgumentException(MISSING_SECRET_NAME);
         }
         KeyVaultSecret p = getEndpoint().getSecretClient()
-                .setSecret(new KeyVaultSecret(secretName, 
exchange.getMessage().getMandatoryBody(String.class)));
+                .setSecret(new KeyVaultSecret(secretName, 
getSecretValue(exchange.getMessage())));
         Message message = getMessageForResponse(exchange);
         message.setBody(p);
     }
 
+    /**
+     * The value of the secret to create. Text, bytes, a stream or a file is 
read as text. A Map, a List or a POJO is
+     * stored as JSON, of which the azure properties function can read a 
field. Another value, such as a number, a UUID
+     * or an object that Jackson cannot serialize, is read as text, as before.
+     */
+    private static String getSecretValue(Message message) throws 
InvalidPayloadException {
+        final Object body = message.getBody();
+        if (body instanceof Map || body instanceof Collection) {
+            try {
+                return JSON_MAPPER.writeValueAsString(body);
+            } catch (JsonProcessingException e) {
+                throw new IllegalArgumentException(
+                        "The " + body.getClass().getName() + " body of 
createSecret cannot be stored as JSON", e);
+            }
+        }
+        if (body != null && !isPayload(body)) {
+            final JsonNode json = toJson(body);
+            // Jackson writes a value, such as a UUID, as a JSON string: it is 
stored as its text
+            if (json != null && json.isContainerNode()) {
+                return json.toString();
+            }
+        }
+        return message.getMandatoryBody(String.class);
+    }
+
+    /**
+     * Whether the body is a payload, which is read as text: text, bytes, a 
stream or a file.
+     */
+    private static boolean isPayload(Object body) {
+        return body instanceof CharSequence || body instanceof byte[]
+                || body instanceof InputStream || body instanceof Reader || 
body instanceof StreamCache
+                || body instanceof File || body instanceof Path || body 
instanceof WrappedFile;
+    }
+
+    private static JsonNode toJson(Object body) {
+        try {
+            return JSON_MAPPER.valueToTree(body);
+        } catch (IllegalArgumentException e) {
+            // not a POJO that Jackson can serialize, such as an object 
without properties

Review Comment:
   Thanks for checking this. I ran it against the Jackson that Camel manages 
(jackson-databind 2.22.4), with the same default `ObjectMapper` the producer 
uses: `valueToTree(new Object())` throws an `IllegalArgumentException` ("No 
serializer found for class java.lang.Object and no properties discovered to 
create BeanSerializer"), because `SerializationFeature.FAIL_ON_EMPTY_BEANS` is 
on by default. So an object without properties is one of the cases this `catch` 
handles, and I kept the comment as it is.
   
   You are right that it is not the only case: a `java.time` value without the 
JSR-310 module lands in the same `catch` ("Java 8 date/time type 
`java.time.LocalDate` not supported by default"), and so does a 
self-referencing object. All of them are stored as their text, as before.
   
   _Claude Code on behalf of Andrea Cosentino (oscerd)_
   



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