Skip to content

[AVRO] Make RecordVisitor _avroSchema and _fields properties final #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ public class RecordVisitor
*/
private final Schema _typeSchema;

// !!! 19-May-2025: TODO: make final in 2.20
protected Schema _avroSchema;
protected final Schema _avroSchema;

// !!! 19-May-2025: TODO: make final in 2.20
protected List<Schema.Field> _fields = new ArrayList<>();
protected final List<Schema.Field> _fields = new ArrayList<>();

public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperImpl visitorWrapper)
{
Expand All @@ -82,12 +80,11 @@ public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperIm
_typeSchema = null;
} else {
// If Avro schema for this _type results in UNION I want to know Avro type where to assign fields
_avroSchema = AvroSchemaHelper.initializeRecordSchema(bean);
_typeSchema = _avroSchema;
_typeSchema = AvroSchemaHelper.initializeRecordSchema(bean);
_overridden = false;
AvroMeta meta = bean.getClassInfo().getAnnotation(AvroMeta.class);
if (meta != null) {
_avroSchema.addProp(meta.key(), meta.value());
_typeSchema.addProp(meta.key(), meta.value());
}

List<NamedType> subTypes = getProvider().getAnnotationIntrospector().findSubtypes(bean.getClassInfo());
Expand Down Expand Up @@ -126,10 +123,12 @@ public RecordVisitor(SerializerProvider p, JavaType type, VisitorFormatWrapperIm
unionSchemas.add(subTypeSchema);
}
}
_avroSchema = Schema.createUnion(new ArrayList<>(unionSchemas));
} catch (JsonMappingException jme) {
throw new RuntimeJsonMappingException("Failed to build schema", jme);
}
_avroSchema = Schema.createUnion(new ArrayList<>(unionSchemas));
} else {
_avroSchema = _typeSchema;
}
}
_visitorWrapper.getSchemas().addSchema(type, _avroSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public void testSchemaCreation_withLogicalTypesDisabled_onBigDecimalWithAvroDeci
// because logical types are disabled by default.
final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

System.out.println(BigDecimalWithAvroDecimalAnnotationWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getField("bigDecimalValue")).isNotNull();
Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema();
Expand All @@ -59,8 +57,6 @@ public void testSchemaCreation_withLogicalTypesEnabled_onBigDecimalWithAvroDecim
MAPPER.acceptJsonFormatVisitor(BigDecimalWithAvroDecimalAnnotationWrapper.class, gen);
final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

System.out.println(BigDecimalWithAvroDecimalAnnotationWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getField("bigDecimalValue")).isNotNull();
Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema();
Expand Down Expand Up @@ -90,8 +86,6 @@ public void testSchemaCreation_withLogicalTypesEnabled_onBigDecimalWithAvroDecim
MAPPER.acceptJsonFormatVisitor(BigDecimalWithAvroDecimalAnnotationToFixedWrapper.class, gen);
final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

System.out.println(BigDecimalWithAvroDecimalAnnotationToFixedWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getField("bigDecimalValue")).isNotNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public void testSchemaCreation(Class<?> testClass,
mapper.acceptJsonFormatVisitor(testClass, gen);
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

// System.out.println(testClass.getName() + " schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo(expectedType);
assertThat(actualSchema.getProp(LogicalType.LOGICAL_TYPE_PROP)).isEqualTo(expectedLogicalType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public void builtAvroSchemaTest(Class<?> testClass,
// WHEN
Schema actualSchema = dateTimeVisitor.builtAvroSchema();

// System.out.println(testClass.getName() + " schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo(expectedAvroType);
assertThat(actualSchema.getProp(LogicalType.LOGICAL_TYPE_PROP)).isEqualTo(expectedLogicalType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public void testJavaEnumToAvroEnum_test() throws JsonMappingException {
MAPPER.acceptJsonFormatVisitor(NumbersEnum.class , gen);
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

System.out.println("schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo( Schema.Type.ENUM);
assertThat(actualSchema.getEnumSymbols()).containsExactlyInAnyOrder("ONE", "TWO", "THREE");
Expand All @@ -44,8 +42,6 @@ public void testJavaEnumToAvroString_test() throws JsonMappingException {
MAPPER.acceptJsonFormatVisitor(NumbersEnum.class , gen);
Schema actualSchema = gen.getGeneratedSchema().getAvroSchema();

System.out.println("schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo( Schema.Type.STRING);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public void subclasses_of_interface_test() throws Exception {
// WHEN
Schema actualSchema = MAPPER.schemaFor(AnimalInterface.class).getAvroSchema();

// System.out.println("Animal schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
// Because AnimalInterface is interface and AbstractMammal is abstract, they are not expected to be among types in union
Expand Down Expand Up @@ -82,8 +80,6 @@ public void jsonSubTypes_on_concrete_class_test() throws Exception {
// WHEN
Schema actualSchema = MAPPER.schemaFor(Fruit.class).getAvroSchema();

// System.out.println("Fruit schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(fruitItselfSchema, appleSchema, pearSchema);
Expand Down Expand Up @@ -142,8 +138,6 @@ public void jsonSubTypes_of_jsonSubTypes_test() throws Exception {
// WHEN
Schema actualSchema = MAPPER.schemaFor(Vehicle.class).getAvroSchema();

// System.out.println("Vehicle schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(
Expand Down Expand Up @@ -187,8 +181,6 @@ public void class_is_referenced_twice_in_hierarchy_test() throws Exception {
// WHEN
Schema actualSchema = MAPPER.schemaFor(ElementInterface.class).getAvroSchema();

// System.out.println("ElementInterface schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
// ElementInterface and AbstractGas are not concrete classes they are not expected to be among types in union
Expand Down Expand Up @@ -223,8 +215,6 @@ public void base_class_explicitly_in_JsonSubTypes_annotation_test() throws Excep
// WHEN
Schema actualSchema = MAPPER.schemaFor(Image.class).getAvroSchema();

// System.out.println("Image schema:\n" + actualSchema.toString(true));

// THEN
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.UNION);
assertThat(actualSchema.getTypes()).containsExactlyInAnyOrder(imageItselfSchema, jpegSchema, pngSchema);
Expand Down