Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ResultDeserializer(JavaType valueType) {
DeserializationContext ctxt) throws IOException {
ObjectCodec codec = p.getCodec();
ObjectNode node = codec.readTree(p);
JsonNode caseNode = node.findValue(CASE_FIELD_NAME);
JsonNode caseNode = node.get(CASE_FIELD_NAME);

if (caseNode == null) {
throw new JsonMappingException(p, String.format("Could not deserialize input as a Result. The required %s field is missing.", CASE_FIELD_NAME));
Expand All @@ -56,7 +56,7 @@ private static Object deserializeValue(
String fieldName,
JavaType type
) throws IOException {
JsonNode valueNode = node.has(fieldName) ? node.findValue(fieldName) : node;
JsonNode valueNode = node.has(fieldName) ? node.get(fieldName) : node;
if (type.getRawClass() == NullValue.class && valueNode.isNull()) {
// Our version of Jackson doesn't allow custom deserialization of null
return NullValue.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ public class ResultModuleTest {
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<Result<TestBean, TestError>, Result<TestBean, TestError>> NESTED_OK_OK =
Result.ok(Result.ok(new TestBean("test")));
private static final String NESTED_OK_OK_JSON = "{\"@ok\":{\"value\":\"test\",\"@result\":\"OK\"},\"@result\":\"OK\"}";

private static final Result<Result<TestBean, TestError>, Result<TestBean, TestError>> NESTED_OK_ERR =
Result.ok(Result.err(TestError.ERROR));
private static final String NESTED_OK_ERR_JSON = "{\"@ok\":{\"name\":\"ERROR\",\"@result\":\"ERR\"},\"@result\":\"OK\"}";

private static final Result<Result<TestBean, TestError>, Result<TestBean, TestError>> NESTED_ERR_OK =
Result.err(Result.ok(new TestBean("test")));
private static final String NESTED_ERR_OK_JSON = "{\"@error\":{\"value\":\"test\",\"@result\":\"OK\"},\"@result\":\"ERR\"}";

private static final Result<Result<TestBean, TestError>, Result<TestBean, TestError>> NESTED_ERR_ERR =
Result.err(Result.err(TestError.ERROR));
private static final String NESTED_ERR_ERR_JSON = "{\"@error\":{\"name\":\"ERROR\",\"@result\":\"ERR\"},\"@result\":\"ERR\"}";

private static ObjectMapper objectMapper;

@BeforeClass
Expand Down Expand Up @@ -176,6 +192,26 @@ public void itSerializesNullErr() throws Exception {
itSerializes(NULL_ERR, NULL_ERR_JSON);
}

@Test
public void itSerializesNestedOkOk() throws Exception {
itSerializes(NESTED_OK_OK, NESTED_OK_OK_JSON);
}

@Test
public void itSerializesNestedOkErr() throws Exception {
itSerializes(NESTED_OK_ERR, NESTED_OK_ERR_JSON);
}

@Test
public void itSerializesNestedErrOk() throws Exception {
itSerializes(NESTED_ERR_OK, NESTED_ERR_OK_JSON);
}

@Test
public void itSerializesNestedErrErr() throws Exception {
itSerializes(NESTED_ERR_ERR, NESTED_ERR_ERR_JSON);
}

@Test
public void itDeserializesBeanOk() throws Exception {
itDeserializes(
Expand Down Expand Up @@ -341,6 +377,42 @@ public void itDeserializesNullErr() throws Exception {
);
}

@Test
public void itDeserializesNestedOkOk() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there also be serialization tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

itDeserializes(
NESTED_OK_OK_JSON,
new TypeReference<Result<Result<TestBean, TestError>, Result<TestBean, TestError>>>(){},
NESTED_OK_OK
);
}

@Test
public void itDeserializesNestedOkErr() throws Exception {
itDeserializes(
NESTED_OK_ERR_JSON,
new TypeReference<Result<Result<TestBean, TestError>, Result<TestBean, TestError>>>(){},
NESTED_OK_ERR
);
}

@Test
public void itDeserializesNestedErrOk() throws Exception {
itDeserializes(
NESTED_ERR_OK_JSON,
new TypeReference<Result<Result<TestBean, TestError>, Result<TestBean, TestError>>>(){},
NESTED_ERR_OK
);
}

@Test
public void itDeserializesNestedErrErr() throws Exception {
itDeserializes(
NESTED_ERR_ERR_JSON,
new TypeReference<Result<Result<TestBean, TestError>, Result<TestBean, TestError>>>(){},
NESTED_ERR_ERR
);
}

private void itSerializes(Result<?, ?> result, String expectedJson) throws JsonProcessingException {
assertThat(objectMapper.writeValueAsString(result)).isEqualTo(expectedJson);
}
Expand Down