Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make chain resolution via null collections safe #718

Merged
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 @@ -151,6 +151,41 @@ void testCollectionPropertyChain() throws IOException, ReflectiveOperationExcept
"nested-entity-string-2" );
}

@Test
void testCollectionPropertyChainWithNullCollectionInBetween() throws IOException, ReflectiveOperationException {
final TestAspect aspect = TestAspect.ASPECT_WITH_NESTED_ENTITY_LIST;
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );

final Class<?> aspectClass = findGeneratedClass( result, "AspectWithNestedEntityList" );
final Class<?> entityClass = findGeneratedClass( result, "TestFirstEntity" );
final Class<?> metaAspectClass = findGeneratedClass( result, "MetaAspectWithNestedEntityList" );
final Class<?> metaEntity = findGeneratedClass( result, "MetaTestFirstEntity" );
final Class<?> metaNestedEntity = findGeneratedClass( result, "MetaTestSecondEntity" );

final StaticContainerProperty<Object, Object, List<Object>> listProperty =
(StaticContainerProperty<Object, Object, List<Object>>) metaAspectClass.getField( "TEST_LIST" ).get( null );

final StaticContainerProperty<Object, Object, List<Object>> nestedListProperty =
(StaticContainerProperty<Object, Object, List<Object>>) metaEntity.getField( "TEST_SECOND_LIST" ).get( null );

final StaticProperty<Object, Object> nestedEntityStringProperty =
(StaticProperty<Object, Object>) metaNestedEntity.getField( "RANDOM_VALUE" ).get( null );

final ContainerPropertyChain<Object, List<Object>, Object> entityStringCollectionChain = PropertyChain.fromCollection( listProperty )
.via( nestedListProperty )
.to( nestedEntityStringProperty );

final Object entityInstance = ConstructorUtils.invokeConstructor( entityClass, "Some Text", 123, 123.0f, null );
final Object aspectInstance = ConstructorUtils.invokeConstructor( aspectClass, List.of( entityInstance ) );

assertThat( entityStringCollectionChain.getProperties() ).hasSize( 3 );
assertThat( entityStringCollectionChain.getFirstProperty() ).isEqualTo( listProperty );
assertThat( entityStringCollectionChain.getLastProperty() ).isEqualTo( nestedEntityStringProperty );
assertThat( entityStringCollectionChain.getPropertyType() ).isEqualTo( List.class );
assertThat( entityStringCollectionChain.getContainingType() ).isEqualTo( aspectClass );
assertThat( entityStringCollectionChain.getValue( aspectInstance ) ).isEmpty();
}

@Test
void testSinglePropertyPredicates() throws IOException, ReflectiveOperationException {
final TestAspect aspect = TestAspect.ASPECT_WITH_NESTED_ENTITY_LIST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Object getValue( final Collection<Object> currentValue,
return nextCollection.stream();
}

return Stream.of( nextValue );
return Stream.ofNullable( nextValue );
} ).toList();
}
}
Loading