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,8 +83,12 @@ public abstract class SecretResponse extends VaultDataResponse {
return type.cast(rawValue); return type.cast(rawValue);
} else { } else {
var om = new ObjectMapper(); var om = new ObjectMapper();
if (rawValue instanceof String) {
return om.readValue((String) rawValue, type);
} else {
return om.readValue(om.writeValueAsString(rawValue), type); return om.readValue(om.writeValueAsString(rawValue), type);
} }
}
} catch (IOException e) { } catch (IOException e) {
throw new InvalidResponseException("Unable to parse response payload: " + e.getMessage()); throw new InvalidResponseException("Unable to parse response payload: " + e.getMessage());
} }

View File

@ -119,7 +119,8 @@ class PlainSecretResponseTest extends AbstractModelTest<PlainSecretResponse> {
" \"" + complexKey + "\": {" + " \"" + complexKey + "\": {" +
" \"field1\": \"" + complexVal.field1 + "\",\n" + " \"field1\": \"" + complexVal.field1 + "\",\n" +
" \"field2\": " + complexVal.field2 + "\n" + " \"field2\": " + complexVal.field2 + "\n" +
" }\n" + " },\n" +
" \"" + complexKey + "Json\": \"" + objectMapper.writeValueAsString(complexVal).replace("\"", "\\\"") + "\"\n" +
" }\n" + " }\n" +
"}", "}",
PlainSecretResponse.class PlainSecretResponse.class
@ -169,6 +170,11 @@ class PlainSecretResponseTest extends AbstractModelTest<PlainSecretResponse> {
() -> res.get(complexKey, Integer.class), () -> res.get(complexKey, Integer.class),
"getting complex type as integer should fail" "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"
);
} }