Skip to content

Commit

Permalink
Fix fingerprint method for null default values (#733)
Browse files Browse the repository at this point in the history
* Fix default toString for JsonProperties.NULL_VALUE

The normalization of the Schema is inconsistent across restarts due to object's JsonProperties.NULL_VALUE lack of override toString.

This fixes that and allows deterministic creation for fingerprints.

* Assert that fingerprint hash remains the same across executions

Added test case to assess that generated fingerprint is always the same
between multiple executions, to prevent using an invalid stringified
version of Object.

Same Schema string must return same fingerprint, at least as long as
normalize function is not changed. Forced MD5, just in case in future
releases, the default hashing algorithm changes.
  • Loading branch information
aartigao authored May 20, 2021
1 parent 15d9cd9 commit 8e86df1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.hortonworks.registries.schemaregistry.SchemaFieldInfo;
import com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException;
import com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException;
import org.apache.avro.JsonProperties;
import org.apache.avro.Schema;

import java.io.IOException;
Expand Down Expand Up @@ -180,7 +181,11 @@ private static Appendable build(Map<String, String> env,
// handle default value
Object defaultValue = field.defaultVal();
if (defaultValue != null) {
appendable.append(defaultValue.toString());
if (defaultValue == JsonProperties.NULL_VALUE) {
appendable.append("null");
} else {
appendable.append(defaultValue.toString());
}
}

build(env, field.schema(), appendable).append("}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.hortonworks.registries.schemaregistry.avro;

import com.hortonworks.registries.schemaregistry.SchemaProvider;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;

import static org.hamcrest.core.IsEqual.equalTo;

public class AvroSchemaProviderTest {

private String schemaWithNullDefaults = "{\n" +
" \"type\" : \"record\",\n" +
" \"namespace\" : \"com.hortonworks.registries\",\n" +
" \"name\" : \"trucks\",\n" +
" \"fields\" : [\n" +
" { \"name\" : \"driverId\" , \"type\" : [\"null\", \"int\"], \"default\": null }" +
" ]\n" +
"}\n";

@Test
public void testGetFingerprintNullTypeDeterminism() throws Exception {
AvroSchemaProvider avroSchemaProvider = new AvroSchemaProvider();
avroSchemaProvider.init(Collections.singletonMap(SchemaProvider.HASH_FUNCTION_CONFIG, "MD5"));

String withNullDefaultsFingerprintHex = bytesToHex(avroSchemaProvider.getFingerprint(schemaWithNullDefaults));

Assert.assertThat(withNullDefaultsFingerprintHex, equalTo("e1e17a3aef8c728c131204bbf49046b2"));
}

private static String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}

}

0 comments on commit 8e86df1

Please sign in to comment.