Skip to content

Commit

Permalink
Avoid NPE in DefaultTransientPropertyResolverService
Browse files Browse the repository at this point in the history
Existing code caused NPE when type did not extend Object class.
This is the case with interfaces, such as java.util.Map.
  • Loading branch information
tmarmilic committed Oct 3, 2024
1 parent 6b885a8 commit ca331e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public List<String> resolveTransientPropertyList(Class<?> type) {
List<String> transientPropertyList = new ArrayList<>();
Class<?> currentType = type;

while (currentType != Object.class) {
while (currentType != null && currentType != Object.class) {
List<String> currentTransientPropertyList = Arrays.stream(currentType.getDeclaredFields())
.filter(this::includeField)
.map(Field::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;

import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

@SpringJUnitWebConfig(WebmvcTestConfiguration.class)
class DefaultTransientPropertyResolverServiceTest {
Expand All @@ -44,4 +46,13 @@ void shouldResolveTransientPropertyList() {
// then
assertThat(resultList).containsExactlyInAnyOrder("value", "anotherValue");
}

@Test
void shouldNotFailWhenResolvingTransientPropertyListFromInteface() {
// when
Throwable thrown = catchThrowable(() -> transientPropertyResolverService.resolveTransientPropertyList(Map.class));

// then
assertThat(thrown).isNull();
}
}

0 comments on commit ca331e3

Please sign in to comment.