-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fingerprint method for null default values (#733)
* 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
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
.../src/test/java/com/hortonworks/registries/schemaregistry/avro/AvroSchemaProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |