From 40b2233e202c0ac195a05bb6da32c0b9b3bc7200 Mon Sep 17 00:00:00 2001 From: Zihan Li Date: Wed, 6 Nov 2024 15:42:51 -0800 Subject: [PATCH 1/2] enable default value fill in for all update behavior --- .../linkedin/metadata/dao/BaseLocalDAO.java | 4 +-- .../metadata/dao/utils/RecordUtils.java | 28 +++---------------- .../metadata/dao/utils/RecordUtilsTest.java | 2 +- .../metadata/dao/EbeanLocalAccess.java | 2 +- 4 files changed, 8 insertions(+), 28 deletions(-) diff --git a/dao-api/src/main/java/com/linkedin/metadata/dao/BaseLocalDAO.java b/dao-api/src/main/java/com/linkedin/metadata/dao/BaseLocalDAO.java index ba2d96093..c0f9c582a 100644 --- a/dao-api/src/main/java/com/linkedin/metadata/dao/BaseLocalDAO.java +++ b/dao-api/src/main/java/com/linkedin/metadata/dao/BaseLocalDAO.java @@ -818,8 +818,8 @@ public ASPECT add(@Nonnull URN urn, AspectUpdate maxTransactionRetry); // skip MAE producing and post update hook in test mode or if the result is null (no actual update with addCommon) - return updateLambda.getIngestionParams().isTestMode() ? result.newValue - : result == null ? null : unwrapAddResult(urn, result, auditStamp, trackingContext); + return result == null ? null : (updateLambda.getIngestionParams().isTestMode() ? result.newValue + : unwrapAddResult(urn, result, auditStamp, trackingContext)); } /** diff --git a/dao-api/src/main/java/com/linkedin/metadata/dao/utils/RecordUtils.java b/dao-api/src/main/java/com/linkedin/metadata/dao/utils/RecordUtils.java index c1be38582..723d3c488 100644 --- a/dao-api/src/main/java/com/linkedin/metadata/dao/utils/RecordUtils.java +++ b/dao-api/src/main/java/com/linkedin/metadata/dao/utils/RecordUtils.java @@ -74,30 +74,10 @@ static String capitalizeFirst(@Nonnull String name) { @Nonnull public static String toJsonString(@Nonnull RecordTemplate recordTemplate) { try { - return DATA_TEMPLATE_CODEC.mapToString(recordTemplate.data()); - } catch (IOException e) { - throw new ModelConversionException("Failed to serialize RecordTemplate: " + recordTemplate.toString()); - } - } - - /** - * Serializes a {@link RecordTemplate} to JSON string. - * Also take test mode as input to control the default value fill in strategy - * @param recordTemplate the record template to serialize - * @return the JSON string serialized using {@link JacksonDataTemplateCodec}. - */ - //Todo: we will remove this method once we verify it works and does not bring too much degrade in test mode. - @Nonnull - public static String toJsonString(@Nonnull RecordTemplate recordTemplate, boolean isTestMode) { - if (isTestMode) { - try { - DataMap dataWithDefaultValue = (DataMap) ResponseUtils.fillInDataDefault(recordTemplate.schema(), recordTemplate.data()); - return DATA_TEMPLATE_CODEC.mapToString(dataWithDefaultValue); - } catch (RestLiServiceException | IOException e) { - throw new ModelConversionException("Failed to serialize RecordTemplate: " + recordTemplate.toString(), e); - } - } else { - return toJsonString(recordTemplate); + DataMap dataWithDefaultValue = (DataMap) ResponseUtils.fillInDataDefault(recordTemplate.schema(), recordTemplate.data()); + return DATA_TEMPLATE_CODEC.mapToString(dataWithDefaultValue); + } catch (RestLiServiceException | IOException e) { + throw new ModelConversionException("Failed to serialize RecordTemplate: " + recordTemplate.toString(), e); } } diff --git a/dao-api/src/test/java/com/linkedin/metadata/dao/utils/RecordUtilsTest.java b/dao-api/src/test/java/com/linkedin/metadata/dao/utils/RecordUtilsTest.java index 387e01041..358da32b2 100644 --- a/dao-api/src/test/java/com/linkedin/metadata/dao/utils/RecordUtilsTest.java +++ b/dao-api/src/test/java/com/linkedin/metadata/dao/utils/RecordUtilsTest.java @@ -63,7 +63,7 @@ public void testToJsonStringWithDefault() throws IOException { String expected = loadJsonFromResource("defaultValueAspect.json").replaceAll("\\s+", "").replaceAll("\\n", "").replaceAll("\\r", ""); - String actual = RecordUtils.toJsonString(defaultValueAspect, true); + String actual = RecordUtils.toJsonString(defaultValueAspect); assertEquals(actual, expected); } diff --git a/dao-impl/ebean-dao/src/main/java/com/linkedin/metadata/dao/EbeanLocalAccess.java b/dao-impl/ebean-dao/src/main/java/com/linkedin/metadata/dao/EbeanLocalAccess.java index 5cc8a40cc..0092d8633 100644 --- a/dao-impl/ebean-dao/src/main/java/com/linkedin/metadata/dao/EbeanLocalAccess.java +++ b/dao-impl/ebean-dao/src/main/java/com/linkedin/metadata/dao/EbeanLocalAccess.java @@ -138,7 +138,7 @@ public int addWithOptimisticLocking( } AuditedAspect auditedAspect = new AuditedAspect() - .setAspect(RecordUtils.toJsonString(newValue, isTestMode)) + .setAspect(RecordUtils.toJsonString(newValue)) .setCanonicalName(aspectClass.getCanonicalName()) .setLastmodifiedby(actor) .setLastmodifiedon(new Timestamp(timestamp).toString()) From 41c96eb65f42ebac0a0e4812c96949e2fa4d3c9e Mon Sep 17 00:00:00 2001 From: Zihan Li Date: Wed, 6 Nov 2024 16:00:45 -0800 Subject: [PATCH 2/2] fix unit test --- .../java/com/linkedin/metadata/dao/utils/EBeanDAOUtilsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dao-impl/ebean-dao/src/test/java/com/linkedin/metadata/dao/utils/EBeanDAOUtilsTest.java b/dao-impl/ebean-dao/src/test/java/com/linkedin/metadata/dao/utils/EBeanDAOUtilsTest.java index b441e3f88..2d2ee7c3c 100644 --- a/dao-impl/ebean-dao/src/test/java/com/linkedin/metadata/dao/utils/EBeanDAOUtilsTest.java +++ b/dao-impl/ebean-dao/src/test/java/com/linkedin/metadata/dao/utils/EBeanDAOUtilsTest.java @@ -504,7 +504,7 @@ public void testToAndFromJson() throws NoSuchMethodException, InvocationTargetEx Method extractAspectJsonString = EBeanDAOUtils.class.getDeclaredMethod("extractAspectJsonString", String.class); extractAspectJsonString.setAccessible(true); - assertEquals("{\"lastmodifiedon\":\"1\",\"aspect\":{\"value\":\"test\"},\"lastmodifiedby\":\"0\"}", toJson); + assertEquals("{\"lastmodifiedon\":\"1\",\"lastmodifiedby\":\"0\",\"aspect\":{\"value\":\"test\"}}", toJson); assertNotNull(RecordUtils.toRecordTemplate(AspectFoo.class, (String) extractAspectJsonString.invoke(EBeanDAOUtils.class, toJson))); }