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


##########
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:
   A follow-up to my note above: since the latest commit registers the 
`JavaTimeModule` on this mapper (from Claus's review), a `java.time` value no 
longer lands in this `catch`, it is written as ISO-8601 text. An object without 
properties and a self-referencing object still do, so the comment holds.
   
   _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