Skip to content

Commit

Permalink
Enable default value fill in for all update behavior (#469)
Browse files Browse the repository at this point in the history
* enable default value fill in for all update behavior

* fix unit test

---------

Co-authored-by: Zihan Li <zihli@zihli-mn2.linkedin.biz>
  • Loading branch information
ZihanLi58 and Zihan Li authored Nov 7, 2024
1 parent 5d60c0c commit a96897d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,8 @@ public <ASPECT extends RecordTemplate> 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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public <ASPECT extends RecordTemplate> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down

0 comments on commit a96897d

Please sign in to comment.