Prevent potential NPE on SecretResponse getter

This commit is contained in:
Stefan Kalscheuer 2017-08-18 16:58:33 +02:00
parent c1d519826c
commit 7ac550230f
2 changed files with 10 additions and 6 deletions

View File

@ -4,7 +4,7 @@
<groupId>de.stklcode.jvault</groupId>
<artifactId>connector</artifactId>
<version>0.6.1</version>
<version>0.6.2-SNAPSHOT</version>
<packaging>jar</packaging>

View File

@ -55,7 +55,7 @@ public class SecretResponse extends VaultDataResponse {
* Get a single value for given key.
*
* @param key the key
* @return the value or NULL if absent
* @return the value or {@code null} if absent
* @since 0.4.0
*/
public final Object get(final String key) {
@ -73,9 +73,10 @@ public class SecretResponse extends VaultDataResponse {
*/
@Deprecated
public final String getValue() {
if (get("value") == null)
Object value = get("value");
if (value == null)
return null;
return get("value").toString();
return value.toString();
}
/**
@ -99,13 +100,16 @@ public class SecretResponse extends VaultDataResponse {
* @param key the key
* @param type Class to parse response
* @param <T> Class to parse response
* @return Parsed object
* @return Parsed object or {@code null} if absent
* @throws InvalidResponseException on parsing error
* @since 0.4.0
*/
public final <T> T get(final String key, final Class<T> type) throws InvalidResponseException {
try {
return new ObjectMapper().readValue(get(key).toString(), type);
Object rawValue = get(key);
if (rawValue == null)
return null;
return new ObjectMapper().readValue(rawValue.toString(), type);
} catch (IOException e) {
throw new InvalidResponseException("Unable to parse response payload: " + e.getMessage());
}