Skip to content

Commit 7122b92

Browse files
authored
Merge pull request #718 from bci-oss/bugfix/717-collection-chain-not-null-safe
Make chain resolution via null collections safe
2 parents 2ea8c35 + 6e402da commit 7122b92

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

core/esmf-aspect-model-java-generator/src/test/java/org/eclipse/esmf/aspectmodel/java/ExtendedStaticMetaModelFunctionalityTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,41 @@ void testCollectionPropertyChain() throws IOException, ReflectiveOperationExcept
151151
"nested-entity-string-2" );
152152
}
153153

154+
@Test
155+
void testCollectionPropertyChainWithNullCollectionInBetween() throws IOException, ReflectiveOperationException {
156+
final TestAspect aspect = TestAspect.ASPECT_WITH_NESTED_ENTITY_LIST;
157+
final StaticClassGenerationResult result = TestContext.generateStaticAspectCode().apply( getGenerators( aspect ) );
158+
159+
final Class<?> aspectClass = findGeneratedClass( result, "AspectWithNestedEntityList" );
160+
final Class<?> entityClass = findGeneratedClass( result, "TestFirstEntity" );
161+
final Class<?> metaAspectClass = findGeneratedClass( result, "MetaAspectWithNestedEntityList" );
162+
final Class<?> metaEntity = findGeneratedClass( result, "MetaTestFirstEntity" );
163+
final Class<?> metaNestedEntity = findGeneratedClass( result, "MetaTestSecondEntity" );
164+
165+
final StaticContainerProperty<Object, Object, List<Object>> listProperty =
166+
(StaticContainerProperty<Object, Object, List<Object>>) metaAspectClass.getField( "TEST_LIST" ).get( null );
167+
168+
final StaticContainerProperty<Object, Object, List<Object>> nestedListProperty =
169+
(StaticContainerProperty<Object, Object, List<Object>>) metaEntity.getField( "TEST_SECOND_LIST" ).get( null );
170+
171+
final StaticProperty<Object, Object> nestedEntityStringProperty =
172+
(StaticProperty<Object, Object>) metaNestedEntity.getField( "RANDOM_VALUE" ).get( null );
173+
174+
final ContainerPropertyChain<Object, List<Object>, Object> entityStringCollectionChain = PropertyChain.fromCollection( listProperty )
175+
.via( nestedListProperty )
176+
.to( nestedEntityStringProperty );
177+
178+
final Object entityInstance = ConstructorUtils.invokeConstructor( entityClass, "Some Text", 123, 123.0f, null );
179+
final Object aspectInstance = ConstructorUtils.invokeConstructor( aspectClass, List.of( entityInstance ) );
180+
181+
assertThat( entityStringCollectionChain.getProperties() ).hasSize( 3 );
182+
assertThat( entityStringCollectionChain.getFirstProperty() ).isEqualTo( listProperty );
183+
assertThat( entityStringCollectionChain.getLastProperty() ).isEqualTo( nestedEntityStringProperty );
184+
assertThat( entityStringCollectionChain.getPropertyType() ).isEqualTo( List.class );
185+
assertThat( entityStringCollectionChain.getContainingType() ).isEqualTo( aspectClass );
186+
assertThat( entityStringCollectionChain.getValue( aspectInstance ) ).isEmpty();
187+
}
188+
154189
@Test
155190
void testSinglePropertyPredicates() throws IOException, ReflectiveOperationException {
156191
final TestAspect aspect = TestAspect.ASPECT_WITH_NESTED_ENTITY_LIST;

core/esmf-aspect-static-meta-model-java/src/main/java/org/eclipse/esmf/staticmetamodel/propertychain/CollectionPropertyChainElementAccessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Object getValue( final Collection<Object> currentValue,
3838
return nextCollection.stream();
3939
}
4040

41-
return Stream.of( nextValue );
41+
return Stream.ofNullable( nextValue );
4242
} ).toList();
4343
}
4444
}

0 commit comments

Comments
 (0)