diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/SimpleRecordSchema.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/SimpleRecordSchema.java index ba507e9602..7f7844896a 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/SimpleRecordSchema.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/SimpleRecordSchema.java @@ -40,6 +40,7 @@ public class SimpleRecordSchema implements RecordSchema { private final SchemaIdentifier schemaIdentifier; private String schemaName; private String schemaNamespace; + private volatile int hashCode; public SimpleRecordSchema(final List fields) { this(fields, createText(fields), null, false, SchemaIdentifier.EMPTY); @@ -171,7 +172,12 @@ public class SimpleRecordSchema implements RecordSchema { @Override public int hashCode() { - return 143 + 3 * fields.hashCode(); + int computed = this.hashCode; + if (computed == 0) { + computed = this.hashCode = 143 + 3 * fields.hashCode(); + } + + return computed; } private static String createText(final List fields) { diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/type/RecordDataType.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/type/RecordDataType.java index fc6993fb89..f7e963101e 100644 --- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/type/RecordDataType.java +++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/type/RecordDataType.java @@ -42,7 +42,7 @@ public class RecordDataType extends DataType { @Override public int hashCode() { - return 31 + 41 * getFieldType().hashCode() + 41 * (childSchema == null ? 0 : childSchema.hashCode()); + return 31 + 41 * getFieldType().hashCode(); } @Override diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java index 5a61275ae1..204cb67013 100644 --- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java +++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/TestSimpleRecordSchema.java @@ -17,15 +17,18 @@ package org.apache.nifi.serialization; +import org.apache.nifi.serialization.record.RecordField; +import org.apache.nifi.serialization.record.RecordFieldType; +import org.apache.nifi.serialization.record.SchemaIdentifier; +import org.junit.Assert; +import org.junit.Test; + import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; -import org.apache.nifi.serialization.record.RecordField; -import org.apache.nifi.serialization.record.RecordFieldType; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.Assert.assertTrue; public class TestSimpleRecordSchema { @@ -68,6 +71,25 @@ public class TestSimpleRecordSchema { } } + @Test + public void testHashCodeAndEqualsWithSelfReferencingSchema() { + final SimpleRecordSchema schema = new SimpleRecordSchema(SchemaIdentifier.EMPTY); + + final List personFields = new ArrayList<>(); + personFields.add(new RecordField("name", RecordFieldType.STRING.getDataType())); + personFields.add(new RecordField("sibling", RecordFieldType.RECORD.getRecordDataType(schema))); + + schema.setFields(personFields); + + schema.hashCode(); + assertTrue(schema.equals(schema)); + + final SimpleRecordSchema secondSchema = new SimpleRecordSchema(SchemaIdentifier.EMPTY); + secondSchema.setFields(personFields); + assertTrue(schema.equals(secondSchema)); + assertTrue(secondSchema.equals(schema)); + } + private Set set(final String... values) { final Set set = new HashSet<>(); for (final String value : values) {