Skip to content

Commit b677196

Browse files
committed
fix: reflect the internal models to asset models from param and return types.
1 parent 1909fd0 commit b677196

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

dao-api/src/main/java/com/linkedin/metadata/dao/utils/ModelUtils.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -498,23 +498,27 @@ public static <ASSET extends RecordTemplate, ASPECT_UNION extends UnionTemplate>
498498
}
499499
RecordUtils.setRecordTemplatePrimitiveField(asset, URN_FIELD, urn);
500500

501+
final Map<String, String> paramToAssetMethod = new HashMap<>();
502+
for (final Method assetMethod : assetClass.getDeclaredMethods()) {
503+
if (assetMethod.getName().startsWith("set") && assetMethod.getParameterTypes().length > 0) {
504+
paramToAssetMethod.put(assetMethod.getParameterTypes()[0].getName(), assetMethod.getName());
505+
}
506+
}
507+
501508
for (final ASPECT_UNION aspect : aspects) {
502-
final Field[] aspectUnionFields = aspect.getClass().getDeclaredFields();
503-
final Field[] assetFields = asset.getClass().getDeclaredFields();
504-
for (final Field aspectUnionField : aspectUnionFields) {
505-
if (aspectUnionField.getName().startsWith(MEMBER_FIELD_PREFIX)) {
506-
final String aspectFieldName = aspectUnionField.getName().substring(MEMBER_FIELD_PREFIX.length());
507-
for (final Field assetField : assetFields) {
508-
if (assetField.getName().startsWith(FIELD_FIELD_PREFIX) && assetField.getName()
509-
.substring(FIELD_FIELD_PREFIX.length())
510-
.equals(aspectFieldName)) {
511-
final Object aspectValue = aspect.getClass().getMethod("get" + aspectFieldName).invoke(aspect);
512-
if (aspectValue != null) {
513-
asset.getClass()
514-
.getMethod("set" + aspectFieldName, aspectValue.getClass())
515-
.invoke(asset, aspectValue);
516-
}
517-
}
509+
final Map<String, String> returnToUnionMethod = new HashMap<>();
510+
for (final Method aspectUnionMethod : aspect.getClass().getMethods()) {
511+
if (aspectUnionMethod.getName().startsWith("get")) {
512+
returnToUnionMethod.put(aspectUnionMethod.getReturnType().getName(), aspectUnionMethod.getName());
513+
}
514+
}
515+
for (final String param : paramToAssetMethod.keySet()) {
516+
if (returnToUnionMethod.containsKey(param)) {
517+
Object aspectValue = aspect.getClass().getMethod(returnToUnionMethod.get(param)).invoke(aspect);
518+
if (aspectValue != null) {
519+
asset.getClass()
520+
.getMethod(paramToAssetMethod.get(param), aspectValue.getClass())
521+
.invoke(asset, aspectValue);
518522
}
519523
}
520524
}

dao-api/src/test/java/com/linkedin/metadata/dao/utils/ModelUtilsTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ public void testGetAspectsFromAsset() {
319319
EntityAsset asset = new EntityAsset();
320320
AspectFoo expectedAspectFoo = new AspectFoo().setValue("foo");
321321
AspectBar expectedAspectBar = new AspectBar().setValue("bar");
322-
asset.setAspectFoo(expectedAspectFoo);
323-
asset.setAspectBar(expectedAspectBar);
322+
asset.setFoo(expectedAspectFoo);
323+
asset.setBar(expectedAspectBar);
324324

325325
List<? extends RecordTemplate> aspects = ModelUtils.getAspectsFromAsset(asset);
326326

@@ -601,10 +601,10 @@ public void testNewAsset() {
601601
ModelUtils.newAsset(EntityAsset.class, expectedUrn, Lists.newArrayList(aspectUnion1, aspectUnion2));
602602

603603
assertEquals(asset.getUrn(), expectedUrn);
604-
assertTrue(asset.hasAspectFoo());
605-
assertTrue(asset.hasAspectBar());
606-
assertEquals(asset.getAspectFoo(), expectedFoo);
607-
assertEquals(asset.getAspectBar(), expectedBar);
604+
assertTrue(asset.hasFoo());
605+
assertTrue(asset.hasBar());
606+
assertEquals(asset.getFoo(), expectedFoo);
607+
assertEquals(asset.getBar(), expectedBar);
608608
assertFalse(asset.hasAspectAttributes());
609609
}
610610

@@ -626,10 +626,10 @@ public void testConvertSnapshotToAsset() {
626626
EntityAsset entityAsset = ModelUtils.convertSnapshotToAsset(EntityAsset.class, snapshot);
627627

628628
assertEquals(entityAsset.getUrn(), expectedUrn);
629-
assertTrue(entityAsset.hasAspectFoo());
630-
assertTrue(entityAsset.hasAspectBar());
631-
assertEquals(entityAsset.getAspectFoo(), expectedFoo);
632-
assertEquals(entityAsset.getAspectBar(), expectedBar);
629+
assertTrue(entityAsset.hasFoo());
630+
assertTrue(entityAsset.hasBar());
631+
assertEquals(entityAsset.getFoo(), expectedFoo);
632+
assertEquals(entityAsset.getBar(), expectedBar);
633633
assertFalse(entityAsset.hasAspectAttributes());
634634
}
635635

@@ -651,9 +651,9 @@ public void testConvertInternalSnapshotToAsset() {
651651
EntityAsset entityAsset = ModelUtils.convertSnapshotToAsset(EntityAsset.class, internalEntitySnapshot);
652652

653653
assertEquals(entityAsset.getUrn(), expectedUrn);
654-
assertEquals(entityAsset.getAspectFoo(), expectedFoo);
654+
assertEquals(entityAsset.getFoo(), expectedFoo);
655655
assertEquals(entityAsset.getAspectFooBar(), expectedFooBar);
656-
assertFalse(entityAsset.hasAspectBar());
656+
assertFalse(entityAsset.hasBar());
657657
assertFalse(entityAsset.hasAspectAttributes());
658658
}
659659

@@ -665,8 +665,8 @@ public void testConvertAssetToInternalSnapshot() {
665665
AspectBar expectedBar = new AspectBar().setValue("bar");
666666
AspectFooBar expectedFooBar = new AspectFooBar().setBars(new BarUrnArray(new BarUrn(2)));
667667
asset.setUrn(expectedUrn);
668-
asset.setAspectFoo(expectedFoo);
669-
asset.setAspectBar(expectedBar);
668+
asset.setFoo(expectedFoo);
669+
asset.setBar(expectedBar);
670670
asset.setAspectFooBar(expectedFooBar);
671671

672672
InternalEntitySnapshot internalEntitySnapshot =

testing/test-models/src/main/pegasus/com/linkedin/testing/EntityAsset.pdl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@ record EntityAsset {
1515
/**
1616
* For unit tests
1717
*/
18-
aspectFoo: optional AspectFoo,
18+
foo: optional AspectFoo,
19+
1920
/**
2021
* For unit tests
2122
*/
22-
aspectBar: optional AspectBar,
23+
bar: optional AspectBar,
2324

2425
/**
2526
* For unit tests
2627
*/
2728
aspectFooEvolved: optional AspectFooEvolved,
29+
2830
/**
2931
* For unit tests
3032
*/
3133
aspectFooBar: optional AspectFooBar,
34+
3235
/**
3336
* For unit tests
3437
*/

0 commit comments

Comments
 (0)