Skip to content

Commit

Permalink
TypeDescriptor properly narrows ResolvableType for non-typed collecti…
Browse files Browse the repository at this point in the history
…on elements

Issue: SPR-14971
(cherry picked from commit 442d8a6)
  • Loading branch information
jhoeller committed Dec 19, 2016
1 parent 2de488e commit bcdda91
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public TypeDescriptor narrow(Object value) {
return this;
}
ResolvableType narrowed = ResolvableType.forType(value.getClass(), this.resolvableType);
return new TypeDescriptor(narrowed, null, this.annotations);
return new TypeDescriptor(narrowed, value.getClass(), this.annotations);
}

/**
Expand Down Expand Up @@ -445,7 +445,10 @@ private TypeDescriptor narrow(Object value, TypeDescriptor typeDescriptor) {
if (typeDescriptor != null) {
return typeDescriptor.narrow(value);
}
return (value != null ? new TypeDescriptor(this.resolvableType, value.getClass(), this.annotations) : null);
if (value != null) {
return narrow(value);
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,35 @@ public void convertArrayToArrayAssignable() {
assertEquals(3, result[2]);
}

@Test
public void convertListOfNonStringifiable() {
List<Object> list = Arrays.asList(new TestEntity(1L), new TestEntity(2L));
assertTrue(conversionService.canConvert(list.getClass(), String.class));
try {
conversionService.convert(list, String.class);
}
catch (ConversionFailedException ex) {
assertTrue(ex.getMessage().contains(list.getClass().getName()));
assertTrue(ex.getCause() instanceof ConverterNotFoundException);
assertTrue(ex.getCause().getMessage().contains(TestEntity.class.getName()));
}
}

@Test
public void convertListOfStringToString() {
List<String> list = Arrays.asList("Foo", "Bar");
assertTrue(conversionService.canConvert(list.getClass(), String.class));
String result = conversionService.convert(list, String.class);
assertEquals("Foo,Bar", result);
}

@Test
public void convertListOfListToString() {
List<String> list1 = Arrays.asList("Foo", "Bar");
List<String> list2 = Arrays.asList("Baz", "Boop");
List<List<String>> list = Arrays.asList(list1, list2);
assertTrue(conversionService.canConvert(list.getClass(), String.class));
String result = conversionService.convert(list, String.class);
assertNotNull(result);
assertEquals("Foo,Bar,Baz,Boop", result);
}

Expand Down

0 comments on commit bcdda91

Please sign in to comment.