diff --git a/src/main/java/de/stklcode/jvault/connector/model/response/SecretResponse.java b/src/main/java/de/stklcode/jvault/connector/model/response/SecretResponse.java index 55d79a9..c50a301 100644 --- a/src/main/java/de/stklcode/jvault/connector/model/response/SecretResponse.java +++ b/src/main/java/de/stklcode/jvault/connector/model/response/SecretResponse.java @@ -83,7 +83,11 @@ public final C get(final String key, final Class type) throws InvalidResp return type.cast(rawValue); } else { var om = new ObjectMapper(); - return om.readValue(om.writeValueAsString(rawValue), type); + if (rawValue instanceof String) { + return om.readValue((String) rawValue, type); + } else { + return om.readValue(om.writeValueAsString(rawValue), type); + } } } catch (IOException e) { throw new InvalidResponseException("Unable to parse response payload: " + e.getMessage()); diff --git a/src/test/java/de/stklcode/jvault/connector/model/response/PlainSecretResponseTest.java b/src/test/java/de/stklcode/jvault/connector/model/response/PlainSecretResponseTest.java index 5a69ed9..af9fb00 100644 --- a/src/test/java/de/stklcode/jvault/connector/model/response/PlainSecretResponseTest.java +++ b/src/test/java/de/stklcode/jvault/connector/model/response/PlainSecretResponseTest.java @@ -119,7 +119,8 @@ void testGetter() { " \"" + complexKey + "\": {" + " \"field1\": \"" + complexVal.field1 + "\",\n" + " \"field2\": " + complexVal.field2 + "\n" + - " }\n" + + " },\n" + + " \"" + complexKey + "Json\": \"" + objectMapper.writeValueAsString(complexVal).replace("\"", "\\\"") + "\"\n" + " }\n" + "}", PlainSecretResponse.class @@ -169,6 +170,11 @@ void testGetter() { () -> res.get(complexKey, Integer.class), "getting complex type as integer should fail" ); + assertEquals( + complexVal, + assertDoesNotThrow(() -> res.get(complexKey + "Json", ComplexType.class), "getting complex type from JSON string failed"), + "unexpected value for complex type from JSON string" + ); }