fix regression from redundant String mapping in SecretResponse getter
All checks were successful
continuous-integration/drone/push Build is passing

Mapping a JSON string into String using a JSON parser will fail, so we
should use the string directly instead of applying double conversion.

Fixes: f3e1f01e38aa74ed20a8ca382e6821b540eb475c
This commit is contained in:
Stefan Kalscheuer 2023-06-16 18:18:11 +02:00
parent f3e1f01e38
commit 1195b447a2
Signed by: stefan
GPG Key ID: 3887EC2A53B55430
2 changed files with 12 additions and 2 deletions

View File

@ -83,7 +83,11 @@ public abstract class SecretResponse extends VaultDataResponse {
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());

View File

@ -119,7 +119,8 @@ class PlainSecretResponseTest extends AbstractModelTest<PlainSecretResponse> {
" \"" + 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 @@ class PlainSecretResponseTest extends AbstractModelTest<PlainSecretResponse> {
() -> 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"
);
}