fix(ingest): Call validator on the base urn as well as aspect components when ingesting (#8250)

Co-authored-by: Indy Prentice <indy@Indys-MacBook-Pro.local>
This commit is contained in:
Indy Prentice 2023-06-16 11:47:19 -05:00 committed by GitHub
parent d1ff9024b9
commit e35ac44f58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 9 deletions

View File

@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.JsonPatch; import com.github.fge.jsonpatch.JsonPatch;
import com.github.fge.jsonpatch.JsonPatchException; import com.github.fge.jsonpatch.JsonPatchException;
import com.github.fge.jsonpatch.Patch; import com.github.fge.jsonpatch.Patch;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
@ -24,6 +25,7 @@ import com.linkedin.common.urn.UrnUtils;
import com.linkedin.common.urn.VersionedUrnUtils; import com.linkedin.common.urn.VersionedUrnUtils;
import com.linkedin.data.schema.RecordDataSchema; import com.linkedin.data.schema.RecordDataSchema;
import com.linkedin.data.schema.TyperefDataSchema; import com.linkedin.data.schema.TyperefDataSchema;
import com.linkedin.data.schema.validation.ValidationResult;
import com.linkedin.data.schema.validator.Validator; import com.linkedin.data.schema.validator.Validator;
import com.linkedin.data.template.DataTemplateUtil; import com.linkedin.data.template.DataTemplateUtil;
import com.linkedin.data.template.RecordTemplate; import com.linkedin.data.template.RecordTemplate;
@ -563,10 +565,11 @@ public class EntityService {
} }
private void validateAspect(Urn urn, RecordTemplate aspect, Validator validator) { private void validateAspect(Urn urn, RecordTemplate aspect, Validator validator) {
RecordTemplateValidator.validate(aspect, validationResult -> { Consumer<ValidationResult> resultFunction = validationResult -> {
throw new IllegalArgumentException("Invalid format for aspect: " + aspect + " for entity: " + urn + "\n Cause: " throw new IllegalArgumentException("Invalid format for aspect: " + aspect + " for entity: " + urn + "\n Cause: "
+ validationResult.getMessages()); + validationResult.getMessages()); };
}, validator); RecordTemplateValidator.validate(buildKeyAspect(urn), resultFunction, validator);
RecordTemplateValidator.validate(aspect, resultFunction, validator);
} }
/** /**
* Checks whether there is an actual update to the aspect by applying the updateLambda * Checks whether there is an actual update to the aspect by applying the updateLambda
@ -697,6 +700,7 @@ public class EntityService {
return systemMetadata; return systemMetadata;
} }
@VisibleForTesting
static void validateUrn(@Nonnull final Urn urn) { static void validateUrn(@Nonnull final Urn urn) {
if (urn.toString().trim().length() != urn.toString().length()) { if (urn.toString().trim().length() != urn.toString().length()) {

View File

@ -1215,12 +1215,9 @@ abstract public class EntityServiceTest<T_AD extends AspectDao, T_RS extends Ret
} }
// Urn purely too long // Urn purely too long
StringBuilder buildStringTooLong = new StringBuilder(); String stringTooLong = "a".repeat(510);
for (int i = 0; i < 510; i++) {
buildStringTooLong.append('a');
}
Urn testUrnTooLong = new Urn("li", "testType", new TupleKey(buildStringTooLong.toString())); Urn testUrnTooLong = new Urn("li", "testType", new TupleKey(stringTooLong));
try { try {
EntityService.validateUrn(testUrnTooLong); EntityService.validateUrn(testUrnTooLong);
Assert.fail("Should have raised IllegalArgumentException for URN too long"); Assert.fail("Should have raised IllegalArgumentException for URN too long");