Fix issue with deserializing nested Results from JSON#31
Conversation
stevie400
left a comment
There was a problem hiding this comment.
The fix seems reasonable, but I'm curious to hear more about the case where you're constructing nested results. My hunch is that a different pattern might serve you better.
| } | ||
|
|
||
| @Test | ||
| public void itDeserializesNestedOkOk() throws Exception { |
There was a problem hiding this comment.
Should there also be serialization tests?
In this situation, I have an operation that does a batch of work (rendering a single email template for a batch of contacts). The batch can fail due to an issue with the template or with fetching the list of contacts (which are failures for the entire batch), or individual emails in the batch can fail (due to issues with one of the individual contacts) while others succeed. The type I'm using here is something like: Result<Map<Contact, Result<ContactSuccess, ContactFailure>>, TemplateFailure> |
| private static final Result<String, NullValue> NULL_ERR = Result.nullErr(); | ||
| private static final String NULL_ERR_JSON = "{\"@error\":null,\"@result\":\"ERR\"}"; | ||
|
|
||
| private static final Result<Map<String, Result<TestBean, TestError>>, Map<String, Result<TestBean, TestError>>> NESTED_OK_OK = |
There was a problem hiding this comment.
Can you please simplify these types? Combining the maps and results this way really makes it hard to grok this stuff.
If a JSON body had a type structure with nested
Results, we would sometimes fail to parse it. This was because the deserializer looked for the first@resultfield it found, but it did this using Jackson'sfindValue(), which essentially does a depth-first search through the JSON and would sometimes find the@resultof an inner nested result rather than the outer result.For example:
When parsing the outer
Result, the deserializer would find the inner"@result": "ERR"rather than the outer"@result": "OK".This PR fixes that by using Jackson's
get()instead offindValue(), which should only find immediate children of the JSON node (without recursively searching down the tree).I haven't been able to think of a case where something would be depending on the previous
findValue()behavior, but I guess it's possible. If someone thinks there could be cases that this change would break, I could update it to do afirstNonNull(get(...), findValue(...))to try to maintain better backwards compatibility./cc @stevegutz @jhaber @szabowexler