gnodet-bot commented on code in PR #28956:
URL: https://github.com/apache/camel/pull/28956#discussion_r4446266991


##########
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:
   💬 **Nit:** The comment example `"object without properties"` is slightly off 
— `new Object()` actually serializes to `{}` (no throw). The 
`IllegalArgumentException` is thrown for types Jackson fundamentally can't 
serialize (e.g. self-referencing objects, `java.time` types without the JSR-310 
module). A more accurate example: `"an object Jackson cannot convert to a tree 
(e.g. self-referencing, or a type without a serializer)"`.



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