Skip to content

Commit

Permalink
fix regression from redundant String mapping in SecretResponse getter
Browse files Browse the repository at this point in the history
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: f3e1f01
  • Loading branch information
stklcode committed Jun 16, 2023
1 parent f3e1f01 commit 1195b44
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ public final <C> C get(final String key, final Class<C> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
);
}


Expand Down

0 comments on commit 1195b44

Please sign in to comment.