Skip to content

Commit

Permalink
spring-projectsGH-1729: Fix JSON Regression
Browse files Browse the repository at this point in the history
Resolves spring-projects#1279

Regression: spring-projects#1215

Test for abstract class should not be applied to container types, which can
be abstract.

**cherry-pick to 2.2.x**
  • Loading branch information
garyrussell authored and artembilan committed Dec 7, 2020
1 parent 18bd707 commit 90aee11
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public JavaType toJavaType(MessageProperties properties) {
}

private boolean canConvert(JavaType inferredType) {
if (inferredType.isAbstract()) {
if (inferredType.isAbstract() && !inferredType.isContainerType()) {
return false;
}
if (inferredType.isContainerType() && inferredType.getContentType().isAbstract()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,47 @@ void cantDeserializeFizListUseHeaders() throws Exception {
assertThat(((Buz) buzs.get(0)).getField()).isEqualTo("foo");
}

@Test
void concreteInListRegression() throws Exception {
byte[] bytes = "[{\"name\":\"bar\"}]".getBytes();
MessageProperties messageProperties = new MessageProperties();
messageProperties.setInferredArgumentType(getClass().getDeclaredMethod("fooLister").getGenericReturnType());
messageProperties.setHeader("__TypeId__", List.class.getName());
messageProperties.setHeader("__ContentTypeId__", Object.class.getName());
Message message = new Message(bytes, messageProperties);
Jackson2JsonMessageConverter j2Converter = new Jackson2JsonMessageConverter();
@SuppressWarnings("unchecked")
List<Foo> foos = (List<Foo>) j2Converter.fromMessage(message);
assertThat(foos).hasSize(1);
assertThat(foos.get(0).getName()).isEqualTo("bar");
}

@Test
void concreteInMapRegression() throws Exception {
byte[] bytes = "{\"test\":{\"field\":\"baz\"}}".getBytes();
MessageProperties messageProperties = new MessageProperties();
messageProperties.setInferredArgumentType(getClass().getDeclaredMethod("stringQuxLister").getGenericReturnType());
messageProperties.setHeader("__TypeId__", Map.class.getName());
messageProperties.setHeader("__KeyTypeId__", String.class.getName());
messageProperties.setHeader("__ContentTypeId__", Object.class.getName());
Message message = new Message(bytes, messageProperties);
Jackson2JsonMessageConverter j2Converter = new Jackson2JsonMessageConverter();

@SuppressWarnings("unchecked")
Map<String, Qux> foos = (Map<String, Qux>) j2Converter.fromMessage(message);
assertThat(foos).hasSize(1);
assertThat(foos.keySet().iterator().next()).isEqualTo("test");
assertThat(foos.values().iterator().next().getField()).isEqualTo("baz");
}

public List<Foo> fooLister() {
return null;
}

public Map<String, Qux> stringQuxLister() {
return null;
}

public List<Baz> bazLister() {
return null;
}
Expand Down Expand Up @@ -507,6 +548,9 @@ public static class Qux implements Baz {

private String field;

public Qux() {
}

public Qux(String field) {
this.field = field;
}
Expand Down

0 comments on commit 90aee11

Please sign in to comment.